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
 
 
 
 
 
TitleMake a button that creates more buttons when clicked in Visual Basic .NET
DescriptionThis example shows how to make a button that creates more buttons when clicked in Visual Basic .NET.
Keywordsbutton, replicating button, VB.NET
CategoriesMiscellany, Puzzles and Games
 
The form's BackColor and TransparencyKey properties are both set to red so the form is invisible. It is displayed maximized so its buttons can appear anywhere on the screen.

When the user clicks a button, the event handler creates a new button, positions it randomly, and adds the same event handler to it so it can create new buttons.

 
Private Sub btnClickMe_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnClickMe.Click
    Dim btn As New Button
    Dim rand As New Random
    Dim x As Integer = rand.Next(0, Me.ClientSize.Width - _
        btnClickMe.Width)
    Dim y As Integer = rand.Next(0, Me.ClientSize.Height - _
        btnClickMe.Height)
    btn.Location = New Point(x, y)

    btn.Text = btnClickMe.Text
    btn.Size = btnClickMe.Size
    btn.BackColor = btnClickMe.BackColor

    AddHandler btn.Click, AddressOf btnClickMe_Click

    Me.Controls.Add(btn)
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated