Example 2.1.1
Private Sub Form_Load
( )
Form1.show
Print “Welcome to Visual Basic tutorial”
End Sub
|
Figure 2.4 : The output of example 2.1.1
|
Example 2.1.2
Private Sub
Form_Activate ( )
Print 20 + 10
Print 20 - 10 Print 20 * 10 Print 20 / 10
End Sub
|
Figure 2.5: The output of example 2.1.2
|
You can also use the + or the & operator to join two or more texts (string)
together like in example 2.1.4 (a) and (b)
Example 2.1.4(a)
Private Sub
A = "Tom"
B = “likes" C = “to" D = “eat" E = “burger" Print A + B + C + D + E
End Sub
|
Example 2.1.4(b)
Private Sub
A = "Tom"
B = “likes" C = “to" D = “eat" E = “burger" Print A & B & C & D & E
End Sub
|
|
Example: Program to
change background color
Private Sub
Form_Load()
Form1.Show
Form1.BackColor = &H000000FF&
End Sub
Example:Program to
change shape
This example is to change the control Shape
to a particular shape at runtime by writing the following code. This code
will change the shape to a circle at runtime.
Private Sub Form_Load()
Shape1.Shape = 3
End Sub
|
Text Box
Private
Sub Command1_Click()
‘To add the values in text box 1 and text box 2
Sum = Val(Text1.Text) + Val(Text2.Text)
‘To display the answer on label 1
Label1.Caption = Sum
End
Sub
|
Output
|
Label
Private Sub Command1_Click ()
label.Caption
= “bharti college”
End Sub
|
Command Button
Private Sub Command1_Click ()
Statements
End Sub
|
Picture Box
Picture1.Picture=LoadPicture("C:\VBprogram\Images\grape.gif")
|
Image Box
Image1.Picture= LoadPicture("C:\VBprogram\Images\grape.gif")
|
List Box
Private Sub Form_Load ( )
List1.AddItem “Lesson1”
List1.AddItem “Lesson2”
List1.AddItem “Lesson3”
List1.AddItem “Lesson4”
End Sub
|
Combo Box
Private Sub Form_Load ( )
Combo1.AddItem “Item1”
Combo1.AddItem “Item2”
Combo1.AddItem “Item3”
Combo1.AddItem “Item4”
End Sub
|
Check Box
Private Sub Command1_Click()
If Check1.Value = 1 And
Check2.Value = 0 Then
MsgBox "Apple is selected" ElseIf Check2.Value = 1 And Check1.Value = 0 Then MsgBox "Orange is selected" Else MsgBox "All are selected" End If
End Sub
|
Option Box
Private Sub Option1_Click ( )
Shape1.Shape = 0
End Sub
Private Sub Option2_Click()
Shape1.Shape = 1
End Sub
Private Sub Option3_Click()
Shape1.Shape = 2
End Sub
Private Sub Option4_Click()
Shape1.Shape = 3
End Sub
Private Sub Option5_Click()
Shape1.Shape = 4
End Sub
Private Sub Option6_Click()
Shape1.Shape = 5
End Sub
|
Object visible
Example 4.1
Private
Sub Command1_click
Label1.Visible=false
Label2.Visible=True
Text1.Text=”You are correct!”
End sub
Assigning Values to
Variables
firstNumber=100
secondNumber=firstNumber-99 userName="John Lyan" userpass.Text = password |
Object visible
Example 4.2
Private
Sub Command1_click
Label1.Caption=” Welcome”
Image1.visible=true
End sub
Assigning Values to
Variables
Label1.Visible = True
Command1.Visible = false Label4.Caption = textbox1.Text ThirdNumber = Val(usernum1.Text) total = firstNumber + secondNumber+ThirdNumber |
Assigning
Values to String Variables
Private Sub Command1_Click()
Dim firstName As
String
Dim secondName As String Dim yourName As String
firstName =
Text1.Text
secondName = Text2.Text yourName = secondName + " " + firstName Label1.Caption = yourName
End Sub
|
Assigning
Values to numeric Variables
Dim number1, number2, number3 as Integer
Dim total, average as variant
Private sub Form_Click
number1=val(Text1.Text)
number2=val(Text2.Text) number3= val(Text3.Text)
Total=number1+number2+number3
Average=Total/5
Label1.Caption=Total
Label2.Caption=Average
End Sub
|