|
|
Title | Make a button that creates more buttons when clicked in Visual Basic .NET |
Description | This example shows how to make a button that creates more buttons when clicked in Visual Basic .NET. |
Keywords | button, replicating button, VB.NET |
Categories | Miscellany, 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
|
|
|
|
|
|