Creating a Dynamic Button in VB.NET

Creating a Button control at run-time is merely a work of creating an instance of Button class, set its properties and add Button class to the Form controls.
  • 20774

Creating a Button Dynamically

Creating a Button control at run-time is merely a work of creating an instance of Button class, set its properties and add Button class to the Form controls. 

The first step is to create an instance of Button class. The following code snippet creates a Button control object. 


Dim
 dynamicButton As New Button

Next step, you need to set Button class properties.  You need to make sure to specify the Location, Width, Height or Size properties. The default location of Button is left top corner of the Form. The Location property takes a Point that specifies the starting position of the Button on a Form. The Size property specifies the size of the control. We can also use Width and Height property instead of Size property. The following code snippet sets Location, Width, and Height properties of a Button control. 

 

dynamicButton.Location = new Point(20, 150)
dynamicButton.Height = 40
dynamicButton.Width = 300

In the next step, you may set more properties of the Button control. The following code snippet sets background color, foreground color, Text, Name, and Font properties of a Button. 

 
dynamicButton.BackColor = Color.Red
dynamicButton.ForeColor = Color.Blue
          
dynamicButton.Text = "I am Dynamic Button"
dynamicButton.Name = "DynamicButton"
dynamicButton.Font = new Font("Georgia", 16)


A Button control is used to process the button click event. We can attach a button click event handler at run-time by setting its Click event to an EventHandler obect. The EventHandler takes a parameter of an event handler. The Click event is attached in the following code snippet. 

 

AddHandler dynamicButton.Click, AddressOf DynamicButton_Click

The signature of Button click event handler is listed in the following code snippet. 


Private 
Sub DynamicButton_Click(ByVal sender As ObjectByVal e As System.EventArgs) 
End Sub 
Now the last step is adding a Button control to the Form. The Form.Controls.Add method is used to add a control to a Form. The following code snippet adds a Button control to the current Form. 

 
Controls.Add(dynamicButton)

The complete code is listed in Listing 1, where CreateDynamicButton methods creates a Button control to a Form at run-time, attaches a click event handler of the button and adds Button control to the Form by calling Form.Controls.Add() method.

 

Sub CreateDynamicButton()
' Create a Button object 
Dim dynamicButton As New Button
' Set Button properties
dynamicButton.Location = New Point(20, 150)
dynamicButton.Height = 40
dynamicButton.Width = 300
' Set background and foreground
dynamicButton.BackColor = Color.Red
dynamicButton.ForeColor = Color.Blue
dynamicButton.Text = "I am Dynamic Button"
dynamicButton.Name = "DynamicButton"
dynamicButton.Font = New Font("Georgia", 16)
AddHandler dynamicButton.Click, AddressOf DynamicButton_Click
' Add Button to the Form. Placement of the Button
' will be based on the Location and Size of button
Controls.Add(dynamicButton)
End Sub
 
Private Sub DynamicButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show("Dynamic Button is clicked.")
End Sub

  

Listing 1


You need to make sure to call CreateDynamicButton() method on the Form load event handler, listed as following.

 


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CreateDynamicButton()
End Sub

Summary

In this article, you saw how to create a Windows Forms Button controly, set its properties and add a click event handler at run-time. 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.