Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AddHandlers in VB.NET

Tags:

vb.net

I'm trying to dynamically creating dropdownList boxes, and I want trying to add AddHandlers to them so that when an item is selected in them, it fires an event, but also need to pass another variable, and I don't know what to put as the parameter for system.EventArgs. Please look at the code below to see the problem I'm having.

AddHandler inputDrop.SelectedIndexChanged, AddressOf selOption(inputDrop, ???, var1)

Protected Sub selOption(ByVal sender As Object, ByVal e As System.EventArgs, ByVal tableCount As String)

End Sub

What do I put (???) right here.

The error:

is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

like image 837
Will Avatar asked Dec 06 '22 05:12

Will


1 Answers

In addition what Mike C already explained, if the signature of the event handler does not match the event, you can always wrap the event handler in another method, for example an anonymous one:

Protected Sub selOption(ender As Object, e As System.EventArgs, somestring As String)

End Sub

...

For i = 1 To 10
    Dim cbox = new ComboBox()
    Dim number = i ' local copy to prevent capturing of i '
    AddHandler cbox.SelectedIndexChanged, Sub(s, e) selOption(s, e, "Hi! I am Number " & number)
Next

Now, when the index of the last ComboBox changes, the somestring parameter passed to selOption will be Hi! I am Number 10, while it will be Hi! I am Number 1 for the first ComboBox etc.

like image 200
sloth Avatar answered Jan 05 '23 20:01

sloth