ARTICLE

Creating a Dynamic Button in VB.NET

Posted by Mahesh Chand Articles | Windows Forms VB.NET May 13, 2010
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.
 
Reader Level:

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 Object, ByVal 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.

Login to add your contents and source code to this article
share this article :
post comment
 

Hiiii

Mahesh

I got the previous problem's Answer. Can you help me out creating MDI application form. I created to menue File< new<Notepad<Textpad etc. and save save as etc. When i run this application and when i click on file<New<Notepad, it should open the notepad.

Suggest me with the coding for this and how should i implement this.

Thanks and Regard
Mithil

Posted by mithil Malhotra May 21, 2010

Mithil.
I am not sure what exactly do you mean. Please feel free to post your question on the forums :)

Cheers!
Mahesh

Posted by Mahesh Chand May 19, 2010

hey would you suggest me, when i enter text in input box, and i click ok. it should remain on the same form, but coz of link i have given to another form, its showing the result on another form. Will you send me the code for dat.

Regard and Love
Mithil Malhotra

Posted by mithil Malhotra May 18, 2010

This is a test comment

Posted by Mahesh Chand May 16, 2010
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor