remove handler of a sub with additional arguments

1.1k Views Asked by At

I try to remove an handler regarding a sub that contains an additional argument, but it doesn't work and it generates a warning :
"The 'AddressOf' expression has no effect in this context because the method argument to 'AddressOf' requires a relaxed conversion to the delegate type of the event. Assign the 'AddressOf' expression to a variable, and use the variable to add or remove the method as the handler.
Here is my sub :

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

Here is my initial code to remove this handler:

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

Unfortunately the handler is not removed properly and the execution pass through my sub when combobox is filled.

So I've done what message advised to do and I've used a variable to assign the addressOf expression.
Here is what I've tried :

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

Warning message disapears but issue remains.
PS: I use Compact Framework

1

There are 1 best solutions below

0
On BEST ANSWER

Check if you actually remove the handler that you wanted. I mean, sometimes people do that

RemoveHandler ComboBox_MFC.SelectedIndexChanged, ComboboxesEventAddress
...
AddHandler ComboBox_MFC.SelectedValueChanged, ComboboxesEventAddress

where the remove the SelectedIndexChanged instead of the SelectedValueChanged ;-)