How to Add a Button Programmatically in VB.Net

In this tutorial I will teach you how to add a Button programmatically in VB.Net. The button will appear automatically in the Form and you don’t need to drag and drop anymore a Button on it. And if you click Button, the new Buttons will be added in chronological order. Let’s begin: Open the Microsoft Visual Studio and create a new project. First Form After that, go to the solution explorer and hit the view code. Solution Explorer Then, in the code view you have to declare the variables that are needed for counting the controls that will be added and setting up the location of the controls in the Form.
  1. 'SET A CLASS VARIABLE TO BE USED DURING THE FORM
  2. Private Count_control As Integer = 0
  3. Private Location_control As New Point(10, 50)
After that, create a method for creating a Button.
  1. Private Sub create_button()
  2.  
  3. 'INCREMENT THE COUNT_CONTROL.
  4. Count_control += 1
  5. 'CHECKING IF THE BUTTONS HAS REACHED TO 5 OR NOT
  6. If Count_control <= 5 Then
  7. 'SET A NEW BUTTON
  8. Dim new_Button As New Button
  9. 'ADD THE PROPERTIES OF THE BUTTON
  10. new_Button.Name = "Button" + Count_control.ToString()
  11. new_Button.Text = "Button" + Count_control.ToString()
  12. new_Button.Location = New Point(Location_control.X + 10, _
  13. Location_control.Y)
  14. new_Button.Width = 180
  15. Location_control.Y += new_Button.Height + 10
  16. 'CREATE THE EVENT HANDLER
  17. AddHandler new_Button.Click, AddressOf myButtonHandler_Click
  18. 'ADD THE NEW BUTTON TO THE COLLECTION OF CONTROLS
  19. Controls.Add(new_Button)
  20. Else
  21.  
  22. 'CHECKING IF YOU WANT TO CLEAR THE CONTROLS THAT YOU HAVE ADDED.
  23. If MessageBox.Show("You've reached 5 controls. Do you want to clear controls to start again?", _
  24. "Proceed", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) _
  25. = Windows.Forms.DialogResult.OK Then
  26.  
  27. Controls.Clear() 'CLEARING THE CONTROL
  28. Count_control = 0 'RETURNING THE COUNT_CONTROL TO ITS DEFAULT VALUE
  29. Location_control = New Point(10, 50) 'SET A NEW POINT OF THE CONTROLS
  30. create_button() 'PUT A CONTROL SO THAT YOU CAN ADD ANOTHER CONTROLS
  31. End If
  32. End If
  33. End Sub
Then, create a subroutine that handles the click events of the Buttons.
  1. Private Sub myButtonHandler_Click(ByVal sender As Object, ByVal e As EventArgs)
  2. 'VERIFYING THE BUTTONS
  3. If TypeOf sender Is Button Then
  4. create_button()'CREATE A NEW BUTTON
  5.  
  6. End If
  7. End Sub
Lastly, you have to put your method that you have created in the first load so that the button will appear in the Form.
  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. create_button()
  3. End Sub
Ouput: Output

Comments

Submitted byNeils (not verified)on Thu, 05/10/2018 - 07:05

Great dude. Thanks from Costa Rica
Submitted byRETHU MATHEW (not verified)on Sat, 02/05/2022 - 01:52

HOW TO ACESS THESE BUTTONS LOGICAL SO I ADD STEPS UNDER THIS BUTTONS .

Add new comment