使用其他参数删除sub的处理程序

时间:2015-05-26 11:27:08

标签: vb.net compact-framework addressof

我尝试删除关于包含附加参数的子的处理程序,但它不起作用并且它会生成警告:
"' AddressOf'表达式在此上下文中没有任何效果,因为' AddressOf'的方法参数需要轻松转换为事件的委托类型。分配' AddressOf'表达式到变量,并使用该变量添加或删除方法作为处理程序 这是我的分:

Private Sub ComboBox_MFC_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs, Optional ByVal SingleCase As Int16 = -1) Handles ComboBox_MFC.SelectedValueChanged
...
End Sub

以下是我删除此处理程序的初始代码:

Private Sub Me_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
        ...
        RemoveHandler ComboBox_MFC.SelectedValueChanged, AddressOf ComboBox_MFC_SelectedValueChanged
        'TODO: This line of code loads data into the 'MyDataSet1.MFC' table. You can move, or remove it, as needed.
        Me.MFCTableAdapter.Fill(Me.MyDataSet1.MFC)
        ComboBox_MFC.DataSource = New DataView(Me.MyDataSet1.MFC, "IsPresent = True", "Name", DataViewRowState.CurrentRows)
        ComboBox_MFC.ValueMember = "Name"
        ComboBox_MFC.DisplayMember = "Name"
        ...
        AddHandler ComboBox_MFC.SelectedValueChanged, AddressOf ComboBox_MFC_SelectedValueChanged
        ...
End Sub

不幸的是,当组合框被填满时,处理程序没有被正确删除并且执行通过我的sub。

所以我已经完成了建议做的消息,并且我已经使用变量来分配addressOf表达式。
这是我尝试过的:

Private Sub Me_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
        ...
        Dim ComboboxesEventAddress As New EventHandler(AddressOf ComboBoxes_SelectedValueChanged)
        RemoveHandler ComboBox_MFC.SelectedValueChanged, ComboboxesEventAddress
        ...
        AddHandler ComboBox_MFC.SelectedValueChanged, ComboboxesEventAddress
End Sub

警告信息消失但问题仍然存在。
PS:我使用Compact Framework

1 个答案:

答案 0 :(得分:0)

检查您是否确实删除了所需的处理程序。我的意思是,有时人们这样做

SelectedIndexChanged

删除SelectedValueChanged而不是{{1}}; - )