user1017477
user1017477

Reputation: 161

Event only raises once?

I have come up with a solution that did solve my problem however, I am curious as to why my initial approach failed. My scenario was as described below:

I have a form that raises the event:

Public Class frmDgvLb

    Public Delegate Sub ProfileChanged()
    Public Event UpdateProfile As ProfileChanged

    Private Sub lbDgvEdit_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lbDgvEdit.SelectedIndexChanged
        If UpdateHotlist(cmdStr) = True Then
            If DgvName = "dgvHotlist" Then
                Hotlist.LoadDGV()
            ElseIf DgvName = "dgvJobProfile" Then
                RaiseEvent UpdateProfile()
            End If
            Me.Close()
        End If
    End Sub

End Class

I have another form where the event is defined and handled:

Public Class frmGraph

    Public Sub New()
        InitializeComponent()
        AddHandler frmDgvLb.UpdateProfile, AddressOf RefreshProfiles
    End Sub

    Public Sub RefreshProfiles()
        GetProfiles(lbMach.SelectedItem, dtpJobDate.Value)
        CreateGraph(dtpJobDate.Value, _machList)
        zgc.Refresh()
    End Sub

End Class

My problem was that the RaiseEvent UpdateProfile() would only execute once. All subsequent index changes of the listbox would not raise the event? When stepping through with the debugger, when the conditional evaluated to true, the debugger would step to the RaiseEvent line and then step to the line closing the conditional statement and the RefreshProfile Sub never executes. Again, the first time the index of the listbox changes, everything functions fine. Why is this?

Upvotes: 2

Views: 645

Answers (1)

SysDragon
SysDragon

Reputation: 9888

You have to add the handler to the instance, not the class:

Public Sub New()
    InitializeComponent()
    AddHandler frmDgvLbInstance.UpdateProfile, AddressOf RefreshProfiles
End Sub

Upvotes: 1

Related Questions