Adding a Button Control in Windows Form
In creating Visual Basic.Net application, one of the most common control used is a Button. Using Visual Basic, it is important to use a button control, because Button is used to initiate actions, usually by clicking on it. Using visual Basic.Net, there are many ways on how to add a button tool to a form. In our case, we’re not going to double click the button tool in the toolbox to add the control to the form. But instead, with your Form displayed in Visual Basic Design Environment, follow the steps:
1.On the Toolbox, choose the button tool and using your mouse left click, click once.
2.Then press the left click and drag your mouse to a control form, and release it.
3.Next, as you can observe the mouse pointer will turn into a cross
4.Press and hold down the left mouse button
5.Drag across the form with the button held down
6.Then, let go of the mouse button when you're satisfied with the size
7.And finally, a Button is drawn
The steps above can be used to draw most of the controls onto the form such as: labels, textboxes, combobox, etc.
Same with the other controls, Button control has a list of properties,two of these properties is a Name property and a Text property. Currently the default settings of our button control are set to “Button1” both Name and Text property. You can modify it to anything you like and you can do this by following these steps:
1.Click the button to highlight it.
2.Then using the property box change the Text property to “Compute”
3. And “btncompute” for the Name property.
4.Add a label, change the Name property to “lblans”
5.Same with the Text property as “Answer”.
Your form should now look something like this:
And double click the button and add the following code:
- Private Sub bntcompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntcompute.Click
- lblans.Text = 7 + 8
- End Sub
Then you can test your program by pressing “F5”.
When the user click the button, the “Answer” label will change into “15”, because 7 plus 8 is equal to 15 and result value has been assigned to the text property of the label.