1

I'm adding buttons to form at runtime using the following code :

Private Sub AddNewButton(ByVal btnName As String, ByVal btnText As String)
    Dim myButtons As New VIBlend.WinForms.Controls.vButton

    With myButtons
        .Size = New Size(200, 60)
        .Visible = True
        .Location = New Point(55, 33)
        .Text = btnText
        .Name = btnName
        .Font = New Font("Times New Roman", 12, FontStyle.Bold, GraphicsUnit.Point)
    End With

    AddHandler myButtons.Click, AddressOf btnName & "_Click"

    btnsPanel.Controls.Add(myButtons)

End Sub

Code works fine but Vb.net doesn't allow me to assign AddressOf a sub procedure using variable AddressOf btnName & "_Click" says AddressOf operand must be the name of method, the problem is I don't know which button is going to be created and which procedure is it going to call, I want to assign AddressOf procedure using variable Is it possible or do I have any other choice ?

Ahmed
  • 651
  • 17
  • 42

3 Answers3

6

You can (and should) use the same event handler for all of the buttons.

AddHandler myButtons.Click, AddressOf btn_Click

You can get the name in the handler in this way:

Private Sub btn_Click(sender As Object, e As EventArgs)
    Dim button = DirectCast(sender, Button)
    Dim name As String = button.Name ' you can also use the Tag property
End Sub
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
3

You could also use a closure, which I Always find very flexible and readable.

Imports VIBlend.WinForms.Controls

Private Sub AddNewButton(ByVal btnName As String, ByVal btnText As String)
    Dim myButton = New vButton() With
                    {
                        .Size = New Size(200, 60),
                        .Visible = True,
                        .Location = New Point(55, 33),
                        .Text = btnText,
                        .Name = btnName,
                        .Font = New Font("Times New Roman", 
                                 12, FontStyle.Bold, GraphicsUnit.Point)
                    }

    AddHandler myButton.Click, Sub(s, e)
                                   'Do something with Name
                                   MessageBox.Show(myButton.Name)
                                   'Or do something in a instance method
                                   Me.HandleButtonClick(myButton)
                               End Sub

    btnsPanel.Controls.Add(myButton)
End Sub

Private Sub HandleButtonClick(myButton As vButton)
    ' DO something her with myButton
End Sub
Ric .Net
  • 5,540
  • 1
  • 20
  • 39
2

The question, though old, lacks a direct and complete answer. Here it is, a fully dynamicall handler of a (dynamic) button:

    Dim SubName As String = dr("SubToRun").ToString
    Dim Pareameter1 As String = dr("Pareameter1").ToString
    Dim Pareameter2 As String = dr("Pareameter2").ToString
    Dim Pareameter3 As Int32 = ValPareameter3
    AddHandler btn.Click, Sub(s, e)
                              CallByName(Me, SubName, CallType.Method, Pareameter1, Pareameter2, Pareameter1)
                          End Sub
Oak_3260548
  • 1,882
  • 3
  • 23
  • 41