Adding a Button to a Form
 

Click on the Button tool in the toolbox

Draw A Button

Click on the Button to highlight it
Click on Text in the Property Box
Click in the box next to the word "Text"
Delete the word "Button 1"
Type "Add two numbers"
Click back on the Form


To get our first look at the code window, double click your Button control. The code window will appear:


The part to concentrate on for the moment is where your cursor is flashing on and off. Because you double-clicked the Button control, the cursor will be flashing between the lines Private Sub … and End Sub.

Here's the part we're concentrating on:

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click


End Sub

the underscore character ( _ ) has been used to spread the code over more than one line.

Private
Private means that no other part of the programme can see this code except for our button

Sub
Short for Subroutine. The "Sub" word tells VB that some code follows, and that it needs to be executed

Button1
This is the name of our button.

_Click ( )
This is Click Event. when the button is clicked, the Click Event will fire, and the code we're going to write will be executed

End Sub
The subroutine ends right here.

Add following code

 

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click

Dim number1 As Integer
Dim number 2 As Integer
Dim answer As Integer


number1 = 3
number2 = 5


answer = number1 + number2


MsgBox answer

End Sub

Then Run (F5)

 

Return to Design

 


 

 


So delete the line: MsgBox answer. Type the word Textbox1, then type a full stop. You should see a drop-down box appear. This is a list of the Properties and Methods that the Textbox can use.



 

VB is Smart

 

 

 

(c) Shilpa Sayura Foundation 2006-2017