Results 1 to 7 of 7

Thread: object reference error

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    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

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    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

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: object reference error

    So would it be better if I had a right click menu on the RTB?

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: object reference error

    Quote Originally Posted by stanav View Post
    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

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    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
  •  



Click Here to Expand Forum to Full Width