Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDynamically create buttons at runtime and give them event handlers in Visual Basic 2005
DescriptionThis example shows how to dynamically create buttons at runtime and give them event handlers in Visual Basic 2005.
Keywordscontrol, Button, click, click event, AddHandler, event handler, Visual Basic 2005
CategoriesControls
 
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
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated