How to Create a Button Programmatically in VB.Net

How to Create a Button Programmatically in VB.Net

This tutorial is all about How to Create a Button Programmatically in VB.Net.
In this tutorial I will teach you how to create a Button programmatically in VB.Net. With this, it will automatically appear in the Form without dragging it. When you click the Button, it will add new Buttons in chronological order.

What is Visual Basic’s purpose?

The third-generation programming language was created to aid developers in the creation of Windows applications. It has a programming environment that allows programmers to write code in.exe or executable files. They can also utilize it to create in-house front-end solutions for interacting with huge databases. Because the language allows for continuing changes, you can keep coding and revising your work as needed.

However, there are some limits to the Microsoft Visual Basic download. If you want to make applications that take a long time to process, this software isn’t for you. That implies you won’t be able to use VB to create games or large apps because the system’s graphic interface requires a lot of memory and space. Furthermore, the language is limited to Microsoft and does not support other operating systems.

What are the most important characteristics of Visual Basic?

Microsoft Visual Basic for Applications Download, unlike other programming languages, allows for speedier app creation. It has string processing capabilities and is compatible with C++, MFC, and F#. Multi-targeting and the Windows Presentation Framework are also supported by the system, allowing developers to create a variety of Windows apps, desktop tools, metro-style programs, and hardware drivers.

Let’s begin:
Open the Microsoft Visual Basic and create a new Windows Form application.
addbuttonprogform1
After creating a Windows Form application, go to the solution explorer and hit the view code.
addbuttonprogform2
Then in the code view, you have to declare the variables that are needed for incrementing the number of the controls that will be added and setting up the location of the controls in the Form.

[vbnet]
‘SET THE CLASS VARIABLE TO BE USED DURING THE FORM
Private inc_control As Integer = 0
Private loc_control As New Point(10, 50)
[/vbnet]

After declaring a variable, create a method for creating a control which is the Button.

[vbnet]
Private Sub AddButton()
‘INCREMENT THE INC_CONTROL.
inc_control += 1
‘CHECKING IF THE CONTROL[BUTTON] THAT HAS BEEN CREATED REACHES TO 5 OR NOT
If inc_control <= 5 Then
‘SET A NEW CONTROL[BUTTON]
Dim BTN As New Button
‘SET THE PROPERTIES OF THE CONTROL[BUTTON]
BTN.Name = “Button” + inc_control.ToString()
BTN.Text = “Button” + inc_control.ToString()
BTN.Location = New Point(loc_control.X + 10, _
loc_control.Y)
BTN.Width = 180
loc_control.Y += BTN.Height + 10
‘CREATE THE EVENT HANDLER
AddHandler BTN.Click, AddressOf myButtonHandler_Click
‘ADD THE NEW CONTROL[BUTTON] TO THE COLLECTION OF CONTROLS
Controls.Add(BTN)
Else

‘CHECKING IF YOU WANT TO CLEAR OR NOT THE CONTROLS[BUTTON] THAT YOU HAVE ADDED.
If MessageBox.Show(“You’ve reached 5 controls. Do you want to clear controls to start again?”, _
“Proceed”, MessageBoxButtons.OKCancel, MessageBoxIcon.Information) _
= Windows.Forms.DialogResult.OK Then

Controls.Clear() ‘CLEARING THE CONTROL[BUTTON]
inc_control = 0 ‘SET THE INC_CONTROL TO ITS DEFAULT VALUE
loc_control = New Point(10, 50) ‘SET A NEW POINT OF THE CONTROLS[BUTTON]
AddButton() ‘PUT A CONTROL[BUTTON] SO THAT YOU CAN ADD ANOTHER CONTROLS[BUTTON]
End If
End If
End Sub
[/vbnet]

Then, create a method that handles the click events of the controls[Button].

[vbnet]
Private Sub myButtonHandler_Click(ByVal sender As Object, ByVal e As EventArgs)
‘VERIFYING THE CONTROLS[BUTTON]
If TypeOf sender Is Button Then
AddButton() ‘CREATE A NEW CONTROL[BUTTON]
End If
End Sub
[/vbnet]

Lastly, you have to put your sub procedure that you have created in the first load of the Form.

[vbnet]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
‘ADDING THE CONTROL[BUTTON] TO THE FIRST LOAD OF THE FORM.
AddButton()
End Sub
[/vbnet]

Press F5 on the keyboard to run your project.

Download the Complete Source Code and run it on your computer.

Readers might read also:

Leave a Comment