|
-
Aug 26th, 2011, 08:33 AM
#1
Thread Starter
Frenzied Member
object reference error
I have this bit of code
Code:
Private Sub SpellChkMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SpellChkMenuItem.Click
Dim activeRichTextBox = TryCast(Me.ActiveControl, RichTextBox)
'clsFS.CheckSpelling(activeRichTextBox)
'clsFS.SpellAndGrammarCheck(activeRichTextBox)
If Len(activeRichTextBox.Text) > 0 Then
SpellChecker.Text = activeRichTextBox.Text
SpellChecker.SpellCheck()
Else
MsgBox("Text box is empty")
End If
End Sub
When I am in the rtb and click on spell check button it tells me the error
"Object reference not set to an instance of an object."
On this line
SpellChecker.Text = activeRichTextBox.Text
But it is suppose to be getting the focused rtb correct? So why is it not referenced
-
Aug 26th, 2011, 09:00 AM
#2
Re: object reference error
Try the following
Code:
Private Sub demo()
Dim activeRichTextBox As New RichTextBox
If TypeOf Me.ActiveControl Is RichTextBox Then
activeRichTextBox = DirectCast(Me.ActiveControl, RichTextBox)
If activeRichTextBox.Text.Length > 0 Then
' assumes SpellChecker exists already and has been properly created
SpellChecker.Text = activeRichTextBox.Text
SpellChecker.SpellCheck()
Else
MsgBox("Text box is empty")
End If
Else
Console.Write("Nope")
End If
End Sub
-
Aug 26th, 2011, 03:39 PM
#3
Re: object reference error
Doesn't the menuitem become the activecontrol when you click on it? And obviously a menuitem cannot be cast to a richtextbox, so TryCast(me.activecontrol, richtextbox) returns Nothing therefore you get a null reference exception when you try to treat Nothing as a richtextbox.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Aug 29th, 2011, 07:34 AM
#4
Thread Starter
Frenzied Member
Re: object reference error
So would it be better if I had a right click menu on the RTB?
-
Aug 29th, 2011, 07:50 AM
#5
Re: object reference error
Possibly not... as then the menu would become the active control.
the way I'd deal with it (and admittedly it is far less than ideal) is to create a form level object of type RTB... then as an RTB gets focus (I'm assuming you have more than one) ... set the form-level object equal to the RTB that just got focus... then use the form-level object in your code above.
-tg
-
Aug 29th, 2011, 08:07 AM
#6
Re: object reference error
 Originally Posted by stanav
Doesn't the menuitem become the activecontrol when you click on it? And obviously a menuitem cannot be cast to a richtextbox, so TryCast(me.activecontrol, richtextbox) returns Nothing therefore you get a null reference exception when you try to treat Nothing as a richtextbox.
Yes the menu item becomes the active control but the ContextMenu SourceControl property indicates which control invoked the menu item as shown below.
Code:
Private Sub DemoToolStripMenuItem_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles DemoToolStripMenuItem.Click
If ContextMenuStrip1.SourceControl IsNot Nothing Then
If TypeOf ContextMenuStrip1.SourceControl Is RichTextBox Then
Dim activeRichTextBox As New RichTextBox
activeRichTextBox = DirectCast(ContextMenuStrip1.SourceControl, RichTextBox)
activeRichTextBox.AppendText("Hello")
End If
End If
End Sub
-
Aug 29th, 2011, 08:09 AM
#7
Thread Starter
Frenzied Member
Re: object reference error
Thanks techgnome, I will give that a shot
And thank Kevin, I will look over that as well.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|