Home > OS >  How do I call a click event with the name of the click event as string
How do I call a click event with the name of the click event as string

Time:06-20

I have a listbox with names of click events.
I'd like to loop through the listbox and execute each of these events. I have tried the following, but this does not work.

Private Sub btnRunSelectedRoutines_Click(sender As Object, e As EventArgs) Handles btnRunSelectedRoutines.Click
    For i As Integer = 0 To ListBoxSelectedButtons.Items.Count - 1
        ListBoxSelectedButtons.Items(i).ToString_Click(sender, New System.EventArgs)
    Next
End Sub

I call click events in the following way, which works without problem.

Private Sub btnRunSelectedRoutines_Click(sender As Object, e As EventArgs) Handles btnRunSelectedRoutines.Click
    btnA_Click(sender, New System.EventArgs)
    btnB_Click(sender, New System.EventArgs)
    btnC_Click(sender, New System.EventArgs)
End Sub

The names in the listbox are as in the example above "btnA, btnB, btnC"

My question is:
Is there a way of calling a click event with values from a listbox (strings)?

CodePudding user response:

You can add any type of item to a ListBox, not just strings. Let's create a class for this purpose:

Private Class EventHandlerDisplay
    Public ReadOnly Property Text As String
    Public ReadOnly Property Click As EventHandler

    Public Sub New(text As String, click As EventHandler)
        Me.Text = text
        Me.Click = click
    End Sub

    Public Overrides Function ToString() As String
        Return Text
    End Function
End Class

It has two properties. One for a text and one for an event handler. The text is also returnd in ToString.

Now, we can add our event handlers to the listbox like this

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ListBoxSelectedButtons.Items.
        Add(New EventHandlerDisplay("BtnA_Click", AddressOf BtnA_Click))
    ListBoxSelectedButtons.Items.
        Add(New EventHandlerDisplay("BtnB_Click", AddressOf BtnB_Click))
    ListBoxSelectedButtons.Items.
        Add(New EventHandlerDisplay("BtnC_Click", AddressOf BtnC_Click))
End Sub

We can then call the selected click routines like this

Private Sub BtnRunSelectedRoutines_Click(sender As Object, e As EventArgs) Handles btnRunSelectedRoutines.Click
    For Each item As EventHandlerDisplay In ListBoxSelectedButtons.SelectedItems
        item.Click.Invoke(Me, EventArgs.Empty)
    Next
End Sub

Note: by default, the ListBox displays items by calling their ToString method. This is why we have overridden ToString above.

We are storing the Click routines as Delegates. Therefore, we do not have to call the routines by name or to use Reflection. We can call the delegates directly.

CodePudding user response:

Yes, you can use reflection:

Imports System.Reflection

[...]

Dim mInfo As MethodInfo

For i As Integer = 0 To ListBoxSelectedButtons.Items.Count - 1
    mInfo = GetType(ContainerClass).GetMethod(ListBoxSelectedButtons.Items(i).ToString())
    mInfo.Invoke()
Next

read some documentation in : https://docs.microsoft.com/en-us/dotnet/api/system.type.getmethod?view=net-6.0

And of course you can filter the Controls array in the form and perform a click on the desired ones

(remember to Import System.Link)

Dim cButtons = Controls.OfType(Of Button).Where(Function(c) ListBoxSelectedButtons.Items.Contains(c.Name))

For Each bt In cButtons 
    bt.PerformClick()
Next
  • Related