Reputation: 3
I have a problem removing my Handlers
I load data into several textboxes. When loading is done I call this public Sub:
AddDirtyEventSixH5Why(Group6H5Why)
Public Sub AddDirtyEventSixH5Why(ByVal ctrl As Control)
For Each c As Control In ctrl.Controls
If TypeOf c Is KryptonTextBox Then
Dim tb As KryptonTextBox = CType(c, KryptonTextBox)
If tb.Tag <> "NoCheck" Then AddHandler tb.TextChanged, AddressOf SetSixH5WhyDirty
End If
If c.Controls.Count > 0 Then
AddDirtyEventSixH5Why(c)
End If
Next
End Sub
Private Sub SetSixH5WhyDirty(ByVal sender As System.Object, ByVal e As System.EventArgs)
With FrmMainPage
If vLoadingSystem = False And vLoadingActivity = False Then
.TxtHiddenSixH5WhyStatus.Text = 1
End If
End With
End Sub
After working with data I want to remove the handles using a similar procedure.
I do this, but is does not work
Public Sub RemoveDirtyEventSixH5Why(ByVal ctrl As Control)
For Each c As Control In ctrl.Controls
If TypeOf c Is KryptonTextBox Then
Dim tb As KryptonTextBox = CType(c, KryptonTextBox)
RemoveHandler tb.TextChanged, AddressOf UnSetSixH5WhyDirty
End If
If c.Controls.Count > 0 Then
RemoveDirtyEventSixH5Why(c)
End If
Next
End Sub
Private Sub UnSetSixH5WhyDirty(ByVal sender As System.Object, ByVal e As System.EventArgs)
With FrmMainPage
.TxtHiddenSixH5WhyStatus.Text = 0
End With
End Sub
Is there any hope for me? :-)
Upvotes: 0
Views: 50
Reputation: 108995
AddHandler tb.TextChanged, AddressOf SetSixH5WhyDirty
… RemoveHandler tb.TextChanged, AddressOf UnSetSixH5WhyDirty
You can only remove handlers you have added. As you've never added UnSetSixH5WhyDirty
you cannot remove that handler.
Try
RemoveHandler tb.TextChanged, AddressOf SetSixH5WhyDirty
Upvotes: 1
Reputation: 2750
You need to remove the same method you add, like this:
RemoveHandler tb.TextChanged, AddressOf SetSixH5WhyDirty
You also need to be careful that you only add the handler once. If there is a risk of adding multiple identical handlers then you should (possibly) remove the handler before adding it, in the following manner:
If tb.Tag <> "NoCheck" Then
RemoveHandler tb.TextChanged, AddressOf SetSixH5WhyDirty
AddHandler tb.TextChanged, AddressOf SetSixH5WhyDirty
End If
Upvotes: 0