如何删除EventHandler

时间:2018-01-12 14:07:28

标签: vb.net

删除处理程序时遇到问题

我将数据加载到几个文本框中。加载完成后,我称之为公共子:

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

使用数据后,我想使用类似的过程删除句柄。

我这样做,但是不起作用

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

我有什么希望吗? : - )

2 个答案:

答案 0 :(得分:1)

AddHandler tb.TextChanged, AddressOf SetSixH5WhyDirty
     

...       RemoveHandler tb.TextChanged,AddressOf UnSetSixH5WhyDirty

您只能删除已添加的处理程序。由于您从未添加UnSetSixH5WhyDirty,因此无法删除该处理程序。

尝试

RemoveHandler tb.TextChanged, AddressOf SetSixH5WhyDirty

答案 1 :(得分:0)

您需要删除添加的相同方法,例如:

* main-repository-private
    /folder-for-open-source-edition
    /folder-enterprise-edition

* open-source-repository-public --> mirrors "main-repository-private/folder-for-open-source-edition"

您还需要注意,您只需添加一次处理程序。如果存在添加多个相同处理程序的风险,那么您应该(可能)在添加之前删除处理程序,方法如下:

RemoveHandler tb.TextChanged, AddressOf SetSixH5WhyDirty