Delegate Problem: Object Reference not set to an instance of an Object

Requirement:
I am building a custom listbox control and I need to send notification to one control, from another when the Mouse Wheel is used.

Implementation:
I am new to delegates but I thought I had understood them this way:
If Class A needs to tell Class B the mouse wheel was used then...

Class A
protected m_Notify As OnThisMouseWheel

Public Delegate Sub OnThisMouseWheel(Str)

Public Sub NotifyOnMouseWheel(ByVal value As OnThisMouseWheel)
    m_Notify = value
End Sub

Private Sub MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ...

    Try
        m_Notify.Invoke("BOo!")
    Catch ex As Exception

    End Try

End Sub


Class B

Private Sub AddItem(ByVal ob As CustomControl)
    ob.NotifyOnMouseWheel(New CustomControl.OnThisMouseWheel(AddressOf NotifyMe))

    ...
End Sub

Public Sub NotifyMe(ByVal s As String)
    Debug.WriteLine(s)
End Sub


If I invoke m_Notify from NotifyOnMouseWheel in Class A right after assigning it, then it works. The object assignment is working and the delegate communicates to the other control. But after that the m_Notify variable equals Nothing. I Imagine it has something to do with the Subroutine that sets up the controls and creates the delegate but I've tried a couple different ways to pass the variable and I still get the same result. Is this thread related?

It is rather urgent so high points.



MattCoatneyAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
How about...

Public Class myListBox
    Inherits ListBox

    Protected m_Notify As OnThisMouseWheel

    Public Delegate Sub OnThisMouseWheel(ByVal Str As String)

    Public Sub NotifyOnMouseWheel(ByVal value As OnThisMouseWheel)
        m_Notify = value
    End Sub

    Protected Overrides Sub OnMouseWheel(ByVal e As System.Windows.Forms.MouseEventArgs)
        If Not (m_Notify Is Nothing) Then
            m_Notify("Boo!")
        End If
    End Sub

End Class


Public Class myListBoxSubscriber

    Public Sub NotifyMe(ByVal s As String)
        Debug.WriteLine(s)
    End Sub

End Class


Public Class Form1
    Inherits System.Windows.Forms.Form

    Private mlbs As myListBoxSubscriber

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim mlb As New myListBox
        mlb.Location = New Point(0, 0)
        mlb.Size = New Size(50, 200)
        For i As Integer = 1 To 100
            mlb.Items.Add("Item" & i)
        Next
        Me.Controls.Add(mlb)

        mlbs = New myListBoxSubscriber
        mlb.NotifyOnMouseWheel(New myListBox.OnThisMouseWheel(AddressOf mlbs.NotifyMe))
    End Sub

End Class
0
 
MattCoatneyAuthor Commented:
To be very clear, the thread question is a subquestion. The Points will be awarded for helping me get the mousewheel event notification to the container control even if that means we don't use delegates.

Thanks in advance.
0
 
MattCoatneyAuthor Commented:
Thanks a bundle.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.