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

Posted on 2006-05-16
Last Modified: 2012-06-27
I am building a custom listbox control and I need to send notification to one control, from another when the Mouse Wheel is used.

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 ...

    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)
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.

Question by:MattCoatney

    Author Comment

    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.
    LVL 85

    Accepted Solution

    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
            End If
        End Sub

    End Class

    Public Class myListBoxSubscriber

        Public Sub NotifyMe(ByVal s As String)
        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)

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

    End Class

    Author Comment

    Thanks a bundle.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now