|
|
Title | Dynamically create buttons at runtime and give them event handlers in Visual Basic 2005 |
Description | This example shows how to dynamically create buttons at runtime and give them event handlers in Visual Basic 2005. |
Keywords | control, Button, click, click event, AddHandler, event handler, Visual Basic 2005 |
Categories | Controls |
|
|
When you click the form's button, the following code executes. It make a new Button object, sets its text and size, and adds it to the flpButtons FlowLayoutPanel control's Children collection.
The code then uses AddHandler to make this button's Click event fire the same btnMakeButton_Click subroutine that created the button.
|
|
Private NumButtons As Integer = 1
Private Sub btnMakeButton_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
btnMakeButton.Click
Dim btn As New Button()
btn.Text = "Make Button " & NumButtons
NumButtons += 1
btn.Size = btnMakeButton.Size
flpButtons.Controls.Add(btn)
AddHandler btn.Click, AddressOf btnMakeButton_Click
End Sub
|
|
|
|
|
|