ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

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.

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.

  1. 'SET THE CLASS VARIABLE TO BE USED DURING THE FORM
  2. Private inc_control As Integer = 0
  3. Private loc_control As New Point(10, 50)

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

  1. Private Sub AddButton()
  2. 'INCREMENT THE INC_CONTROL.
  3. inc_control += 1
  4. 'CHECKING IF THE CONTROL[BUTTON] THAT HAS BEEN CREATED REACHES TO 5 OR NOT
  5. If inc_control <= 5 Then
  6. 'SET A NEW CONTROL[BUTTON]
  7. Dim BTN As New Button
  8. 'SET THE PROPERTIES OF THE CONTROL[BUTTON]
  9. BTN.Name = "Button" + inc_control.ToString()
  10. BTN.Text = "Button" + inc_control.ToString()
  11. BTN.Location = New Point(loc_control.X + 10, _
  12. loc_control.Y)
  13. BTN.Width = 180
  14. loc_control.Y += BTN.Height + 10
  15. 'CREATE THE EVENT HANDLER
  16. AddHandler BTN.Click, AddressOf myButtonHandler_Click
  17. 'ADD THE NEW CONTROL[BUTTON] TO THE COLLECTION OF CONTROLS
  18. Controls.Add(BTN)
  19. Else
  20.  
  21. 'CHECKING IF YOU WANT TO CLEAR OR NOT THE CONTROLS[BUTTON] THAT YOU HAVE ADDED.
  22. If MessageBox.Show("You've reached 5 controls. Do you want to clear controls to start again?", _
  23. "Proceed", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) _
  24. = Windows.Forms.DialogResult.OK Then
  25.  
  26. Controls.Clear() 'CLEARING THE CONTROL[BUTTON]
  27. inc_control = 0 'SET THE INC_CONTROL TO ITS DEFAULT VALUE
  28. loc_control = New Point(10, 50) 'SET A NEW POINT OF THE CONTROLS[BUTTON]
  29. AddButton() 'PUT A CONTROL[BUTTON] SO THAT YOU CAN ADD ANOTHER CONTROLS[BUTTON]
  30. End If
  31. End If
  32. End Sub

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

  1. Private Sub myButtonHandler_Click(ByVal sender As Object, ByVal e As EventArgs)
  2. 'VERIFYING THE CONTROLS[BUTTON]
  3. If TypeOf sender Is Button Then
  4. AddButton() 'CREATE A NEW CONTROL[BUTTON]
  5. End If
  6. End Sub

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

  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. 'ADDING THE CONTROL[BUTTON] TO THE FIRST LOAD OF THE FORM.
  3. AddButton()
  4. End Sub

Press F5 on the keyboard to run your project.

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

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!