Question How to dynamically specify addressof by string for AddHanlder on load

MJC22

Member
Joined
Apr 6, 2013
Messages
6
Programming Experience
Beginner
Hi,

For a given control I set the AddHandler event for a given control on the Form Load Event. Is it possible to specify the AddressOf Object by a String?

Example (simplest form):

I have a public function (TestRoutine), a control (Control) and a string (RoutineName = "TESTROUTINE").

The desire result would look something like:

AddHandler Control.Click, AddressOf RoutineName

The system spits out an error stating RoutineName needs to be an object. Is there a way of defining an Function via a string?
 
set the AddHandler event for a given control?

You use the AddHandler statement to register a handler for an event.

To answer your question, no. AddHandler requires a delegate and that's what AddressOf produces and AddressOf requires an identifier. If you can provide a FULL and CLEAR explanation of what you're trying to achieve, rather than how you're trying to achieve it, then we can probably explain the best way to achieve it. What I expect to be the case is that you need to create all the delegates you might need and then identify them some way, possibly by Strings, and then pass the appropriate one to AddHandler. For instance, let's say that you have three methods that might handle the Click event of Button1 and you want the user to select one from a ComboBox. You could do this:
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim items = {Tuple.Create("Handler 1", New EventHandler(AddressOf Button_Click_1)),
                     Tuple.Create("Handler 2", New EventHandler(AddressOf Button_Click_2)),
                     Tuple.Create("Handler 3", New EventHandler(AddressOf Button_Click_3))}

        With ComboBox1
            .DisplayMember = "Item1"
            .ValueMember = "Item2"
            .DataSource = items
        End With
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        'Remove any previously registered event handler.
        RemoveHandler Button1.Click, AddressOf Button_Click_1
        RemoveHandler Button1.Click, AddressOf Button_Click_2
        RemoveHandler Button1.Click, AddressOf Button_Click_3

        'Register the selected event handler.
        AddHandler Button1.Click, DirectCast(ComboBox1.SelectedValue, EventHandler)
    End Sub

    Private Sub Button_Click_1(sender As Object, e As EventArgs)
        MessageBox.Show("Button_Click_1")
    End Sub

    Private Sub Button_Click_2(sender As Object, e As EventArgs)
        MessageBox.Show("Button_Click_2")
    End Sub

    Private Sub Button_Click_3(sender As Object, e As EventArgs)
        MessageBox.Show("Button_Click_3")
    End Sub

End Class

You can test that example out for yourself with a form containing a ComboBox and a Button. You can select a handler in the ComboBox and then click the Button to see the corresponding message. Note that the EventHandler delegates are created ahead of time and added to the ComboBox. The user sees a String for each one and makes a selection based on that but when they select a String, the corresponding delegate is registered as an event handler. Note that, each time a handler is registered, any previous handler is removed. That's important if the registration may change because, otherwise, you'll end up with multiple registrations. You can comment out those RemoveHandler lines to see that for yourself.
 
Back
Top