AddHandlers in VB.NET

I am trying to dynamically create dropdownList fields, and I want to try to add AddHandlers to them AddHandlers that when I select an element in them, it fires an event, but I also need to pass another variable and I don’t know what to put as a parameter for system.EventArgs . Please look at the code below to see the problem that I am facing.

 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 I put (???) right here.

The error is :

is an event and cannot be called directly. Use the expression "RaiseEvent" to raise an event.

+4
source share
3 answers

In addition to what Mike C has already explained, if the signature of the event handler does not match the event, you can always transfer the event handler by another method, for example, anonymous:

 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 Hi! I am Number 10 , whereas he will be Hi! I am Number 1 Hi! I am Number 1 for the first ComboBox , etc.

+6
source

When you register an event handler, you are not specifying arguments at this time. You basically just set up a link to the delegate who will handle the event when it is raised.

 AddHandler inputDrop.SelectedIndexChanged, AddressOf selOption 

Most importantly, the signature of the event handler method exactly matches the signature of the method defined by the event. I am not sure if your method will work because you have this extra parameter, tableCount . You will need to change your method signature:

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

I base this on the definition of SelectedIndexChanged for Winforms. This event can be defined differently in another technology, such as ASP.net or WPF. Or, if it's some kind of custom class, it could be a completely different signature. However, usually most event handlers have a similar sender structure (the instance that raises the event), and some event arguments.

Then, when inputDrop fires this event (when the selected item changes), your code will be automatically called. The arguments passed to this method will be passed directly from inputDrop , you do not need to specify them.

In addition, your AddHandler must exist inside a method or code block; it cannot just live in a class definition. This is a statement that should be executed like any other piece of code, this is not a declaration.

+5
source

And there is another way to do this. Inherit this control and add a property similar to this:

 Public Class MyComboBox : Inherits ComboBox Public Property tableCount As String End Class 

Then set your custom value and add a handler, as for a regular ComboBox:

 combo.tableCount = tableCount AddHandler combo.Click, AddressOf combo_Click 

Inside combo_Click , CType sender to your inherited type and get the previously saved value:

 Private Sub combo_Click(sender As Object, e As System.EventArgs) Debug.WriteLine(CType(sender, WorkflowActionBox).tableCount) End Sub 

You will need to replace the current ComboBox settings with those specified in MyComboBox , where you want the new property to be available. Just like opening your designer file and searching / replacing.

+4
source

Source: https://habr.com/ru/post/1501478/


All Articles