删除带有附加参数的 sub 的处理程序
remove handler of a sub with additional arguments
我尝试删除有关包含附加参数的子程序的处理程序,但它不起作用并且生成警告:
“'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
不幸的是,处理程序没有被正确删除,当组合框被填充时,执行通过我的子传递。
所以我已经完成了建议的消息,并且我使用了一个变量来分配 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
检查您是否确实删除了所需的处理程序。我的意思是,有时人们会那样做
RemoveHandler ComboBox_MFC.SelectedIndexChanged, ComboboxesEventAddress
...
AddHandler ComboBox_MFC.SelectedValueChanged, ComboboxesEventAddress
其中删除 SelectedIndexChanged
而不是 SelectedValueChanged
;-)
我尝试删除有关包含附加参数的子程序的处理程序,但它不起作用并且生成警告:
“'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
不幸的是,处理程序没有被正确删除,当组合框被填充时,执行通过我的子传递。
所以我已经完成了建议的消息,并且我使用了一个变量来分配 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
检查您是否确实删除了所需的处理程序。我的意思是,有时人们会那样做
RemoveHandler ComboBox_MFC.SelectedIndexChanged, ComboboxesEventAddress
...
AddHandler ComboBox_MFC.SelectedValueChanged, ComboboxesEventAddress
其中删除 SelectedIndexChanged
而不是 SelectedValueChanged
;-)