How to programmatically add a button in VB.NET?

Add the button:
Dim btnAdd As New Button
btnAdd.Text = "Text"
AddHandler btnAdd.Click, AddressOf Me.btnAdd_Click
Me.Controls.Add(btnAdd)

Button click event:
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim clickButton As Button = CType(sender, System.Windows.Forms.Button)
        MsgBox(clickButton.Text)
    End Sub

No comments:

Post a Comment