You are on page 1of 13

1

Programming
Microsoft Visual Basic 6.0
Chapter 1
Getting Started MS VB 6.0
Running the IDE (The Integrated Development Environment)

You can choose from several ways to launch the Visual Basic IDE, as is true for any
Windows executable:
• You can run the Visual Basic 6 environment from the Start Menu; the exact
path to the menu command depends on whether you have installed Visual Basic as
part of the Microsoft Visual Studio suite.
• You can create a shortcut to the IDE on your desktop and run it by simply
double-clicking on it.

1.1 Starting MS Visual Basic 6.0


• Start >> Programs
>> Microsoft Visual Studio 6.0
>> Microsoft Visual Basic 6.0

Then you are in the VB New Project.

Choose Standard.EXE and click the Open button. Then you are in the Project1 –
Microsoft Visual Basis (design) window. See below.

Project
Explorer

Tool Box

Form Window Properties Window


2

This is the ToolBox.

Pointer PictureBox

Label Textbox

Frame CommandButton

CheckBox OptionButton

ComboBox ListBox

hsb vsb

Timer DriveListBox

DirList FileListBox

Shape Line

Image Data

OLE

Controls Contained in Visual Basic’s Toolbox


Control Name Usage Prefix
1. PictureBox Displays graphics. Can also serve as a container for pic
other controls.
2. Label Displays text that the users cannot edit. lbl
3. TextBox Displays text. Allows the users to enter and edit the txt
text.
4. Frame Serves as a container for other controls. Provides fra
grouping of controls.
5. CommandButton Allows the users to initiate actions by clicking the cmd
button.
6. CheckBox Lets the users make a true/false choice. chk
7. OptionButton Lets the users choose one option from a group of opt
items.
8. ComboBox Lets the users choose from a list of items or enter a cbo
new value.
9. ListBox Lets the users choose from a list of items. lst
10. Horizontal ScrollBar Lets the users choose a ScrollBar value based on the hsb
position of the button in the bar.
11. Vertical ScrollBar Same as Horizontal ScrollBar. vsb
12. Timer Lets the program perform functions on a timed tmr
basis.
3

13. Drive List Box Lets the users select a disk drive. drv

14. Directory List Lets the users select a Box directory or dir
folder.
15. File List Box Lets the users select a file. fil

16. Shape Displays a shape on the form. shp

17. Line Displays a line on the form. lin

18. Image Similar to a PictureBox control. Uses img


fewer system resources but doesn’t
support as many properties, events,
and methods.
19. Data Control Provides an interface between the dat
program and a data source.
20. OLE (Object Linking Provides a connection between the ole
Embedding) program and an OLE server.

1.2 Creating a Form


Choose any object from the ToolBox you want to put on the Form, and by double
clicking it, the object will be on the Form.
The following shows a Command Button placed on the form.

You have to fix the properties for the command button Command1. Two most
important properties you should not forget are Caption and Name.
• Caption : - It is to let know a user what the button intends to operate (event).
4

e.g, 1.1 Greeting Program


If you want it to be an exit button, you write in Caption as Exit (or) E&xit, where &
underlines the next letter x in the form,(and it let use the alterkey+x to activate
the button).
• Name :- It is used to let computer recognize that button. Since it is a command
button, you give name as cmdExit with prefix cmd.
In the following form, you have 3 CommandButtons and 1 TextBox. Their properties
are shown below.

Properties Setting

Object Property Value


Form - (Name) - frmGreeting
- Caption - Greeting Program

TextBox - (Name) - txtGreet


- Text - <blank>
CommandButton - (Name) - cmdGreet
- Caption - &Greet Me
CommandButton - (Name) - cmdClear
- Caption - &Clear
CommandButton - (Name) - cmdExit
- Caption - E&xit

 Double Click on the Objects ( Command Buttons ) to


write the following coding in the Code Window .
 You can change to Code Window from View Menu.
Coding :
Private Sub cmdExit_Click ()
End
End sub

Private Sub cmdGreet_Click ()


txtGreet.Text = "Hello, How are you getting on ? "
End Sub

Private Sub cmdClear_Click ()


txtGreet.Text = ""
End Sub

After you have written the coding , Save your Project like as frmGreeting
for Project Name and Form Name . Then , Click on RunButton OR
Press F5 to run the program .
5

1.3 Creating an executable file


• On the file menu , click Make < Project >
• Name the application frmGreeting.exe , and Save it .
• To compile the application , click OK .
1.4 Using Label

e.g.1.2 Display today’s Date and Time on a 4 label controls on a form.


• Open a project.
• Add four label controls to the form by dragging and placing the controls on the
form.
6

• In the form load event , write the following code.


Private sub Form_Load()
lblDate.Caption=Date
lblTime.Caption=Time
End Sub
Private sub cmdClose_Click()
End
End Sub

Save the Project : ( frmDateTime)


• Press F5 to run the program.

Properties Setting
Object Property Value
Form - (Name) - frmDateTime
- Caption - Date & Time Program

Label Control - (Name) - lblDate


- Caption - <blank>
Label Control -(Name) - lblTime
-- Caption - <blank>
Label Control - (Name) - Labl 1
- Caption -Date :
Label Control - (Name) - Label 2
- Caption -Time :
CommandButton - (Name) - cmdClose
- Caption -Close

1.5 Using Frame


e.g. 1.3 A frame should have a picture box , two labels and a text box . While
loading the form a picture ( convo.jpg) should be loaded in the picture box and
today’s Date and Time should be displayed on the two label controls. Also the textbox
should accept only characters.
• Open a project.
• Add a text box control , 2 labels and a picture box control on the frame.
• Write the following code.
Private Sub Form_Load()
Picture1.Picture=LoadPicture (“C:\ convo.jpg “)
Label1.Caption=Date
Label2.Caption=Time
End sub Object Property Value
Form - (Name) - frmFrame
- Caption - Using Frame
TextBox1 - (Name) - Text1
Picture Box - (Name) - Picture1
Label 1 - (Name) - Label1
-Caption - < Blank >
Label 2 - (Name) - Label2
- Caption - < Blank >
Frame1 -(Name) -Frame1
-Caption -Frame1
7

1.6 Using List Box


e.g. 1.4 An application should have a text box , a list box and a command
button . When the command button is clicked, the content of the text box should be
added to the list box.
• Open a project.
• Add a text box control , list box and a command button on the form.
• Write the following code.
Private Sub cmdAddList_Click()
List1.AddItem Text1.Text
Text1.SetFocus
End Sub
Private Sub Form_Activate()
Text1.SetFocus
End Sub

• Save your Project and form like as “frmAddingList” .


• Press F5 to run the program.
Properties Setting
Object Property Value

Form - (Name) - frmListBox


- Caption - Adding Text to List Box

TextBox1 - (Name) -Text1


- Text -<Blank>
List Box - (Name) - List1

Command - (Name) - cmdAddList


Button - Caption - - Add List

1.7 Using Shape Control


e.g. 1.5 Write an application having a form , a text box , a command and a
shape control on the form . The user can type value 0 to 5 (only ) in the text box. On
clicking the command button , the different shapes should be loaded on the form.
• Open a project.
• Add a text box , a command button and a shape control to the form by
dragging and placing controls on the form.
Caption = "Changing Shapes"
Name = "frmChangingShape"
• Write the following code.
Private Sub cmdChange_Click()
Shape1.Shape=Val(Text1.Text)
End Sub
Save your project like as “ frmChangingShape” .
• Press F5 to run the program.
8

1.8 Using Horizontal Scroll Bar – hsb


e.g. 1.6 The following example contains how hsb can be used.
• Create the form shown with a label, and
• a horizontal scroll bar hsb.
Object Property Value

Form - (Name) - frmCircle


- - True
AutoRe
draw

HSBScroll - (Name) - hsbRadius


Bar - Max - 1250
- Min -1
- Value - 500
Label - (Name) - lblArea
- - 1-Fixed Single
BorderS
tyle

Coding Me.Circle (CX, CY), Radius


‘--- Declaration Section ‘ To draw circle, centre and radius’
------------------------
Option Explicit lblArea.Caption = CStr(Radius *
Const PI As Single = 3.14159 Radius * PI)
Dim Radius As Single End Sub
‘---------------------------------------------- ‘----------------------------------------------
-- --
Private Sub hsbRadius_Change() Private Sub hsbRadius_Scroll()
Dim CY As Long Call hsbRadius_Change
Dim CX As Long End Sub
‘----------------------------------------------
CX = Me.ScaleWidth \ 2 --
CY = (Me.ScaleHeight - 600) \ 2 Private Sub Form_Activate()
Call hsbRadius_Change
Radius = hsbRadius.Value End Sub
Me.Cls

1.9 Using Timer


e.g. 1.7 Clock Program
• Create the form shown with a label, 2 command buttons and a timer.
9

Object Property Value Object Property Value


Form Name frmClock Label Name lblTime
Caption Clock Program Caption <Blank>
Command Name cmdResume Command Name cmdPause
Button Button
Timer Name tmrTimer
Interval 1000

Coding Private Sub cmdResume_Click()


Private Sub tmrTimer_Timer() tmrTimer.Enabled = True
lblTime.Caption = Time cmdResume.Enabled = False
End Sub cmdPause.Enabled = True
‘----------------------------------- End Sub
Private Sub cmdPause_Click() ‘-----------------------------------
tmrTimer.Enabled = False Private Sub Form_Load()
cmdPause.Enabled = False lblTime.Caption = Time
cmdResume.Enabled = True cmdResume.Enabled = False
End Sub End Sub
‘-----------------------------------
Function :
Time Function - Sets or Returns the current system time.

1.10 Message Box :


Syntax: MsgBox (Prompt, vbicon+vbbutton, Title)

1.11 InputBox :
Syntax : InputBox (Prompt,[Title], [ Default] ,[XPos], [YPos],
[ Helpfile],[Context] ) As String
10

1.12 Using Variables and Data Types

Data Type Summary


Data Type Storage Size Range
Byte 1 byte 0 to 255
Boolean 2 bytes True or False
Integer 2 bytes - 32,768 to 32,767
Long 4 bytes - 2,147,483,684 to 2,147,483,647
Single 4 bytes - 3.402823E38 to – 1.401298E-45 for negative values
1.401298E-45 to 3.402823E38 for positive values
Double 8 bytes - 1.79769313486232E308 to 4.94065645841247E324
for negative values and 4.94065645841247E-324 to
1.79769313486232E308 for positive values
Currency 8 bytes - 922,337,203,685,477.5808 to 922,337,203,685,477.5807
Decimal 14 bytes +/- 79,228,162,514,264,337,593,543,950,335 with no de. pt.
+/- 79,228,162,514,264,337,593,543,950,335 with 28 places to the right of the
decimal.
Date 8 bytes January 1,100 to December 31, 9999
String(Fixed Length) Length of string 1 to approximately 65,400
String(Variable 10 bytes + String 0 to approximately 2 billions
Length) length
Variants (with 16 bytes
numbers)

Variant (with 22 bytes + String


characters) length

Declaring Variables
• Format : Dim (Variable Name) As (Data Type)
• Example Dim SecondNo As Double
Note :
A variable name:
 Must begin with a letter.
 Must not exceed 255 characters.
 Must be unique within the same scope.
 Must not be a Visual Basic reserved word.

Type Conversion Functions


Conversion Function Convert to
CBool Boolean
CByte Byte
CCur Currency
CDate Date
CInt Integer
CLng Long
CSng Single
CDbl Double
CStr String
CVar Variant
CDec Decimal
11

Operators and their precedence

Arithmetic Operators Comparison Operators Logical Operators

^ (Exponentiation) = (Equality) Not

- (Negation) <> (Inequality) And

*,/(Multiplication andDivision) < (Lessthan) Or

\ (Integer division) > (Greater than) Xor

Mod(Modulusarithmetic) <= (Lessthanor equal to) Eqv

+,-(Addtionandsubtraction) >= (Greater thanor equal to) Imp

& (Stringconcatenation) Is &

Exercises
1.1
The form should have one textbox and a command button. The user can type a string
in the textbox. Whenever the command button is clicked , the caption of the form
should be changed to the string typed in the textbox.
• Open a project.
• Add a textbox control and a command button on the form.
• Write the following code.
Private Sub cmdChange_Click()
cmdChange .Caption=Text1.Text
End Sub
• Save the project and form sub .
(FrmChanging)
• Press F5 to run the program.

1.2
• Create the following form with one CommandButton, and 4 TextBoxes to find
the saving of a deposit 200,000 Kyats after 5 years, where given that interest is 15 %
per year. Properties setting is given below.
• (Extra properties are used in this exercise, such as alignments for text
boxes all of same size; Height 400, Width 1225 and Left 1320).
Coding
Private Sub cmdCalculate_Click()
Deposit = Val(txtDeposite.Text)
IR = Val(txtInterest.Text) / 100
yr = Val(txtYear.Text)
TotalAmount = Deposit * (1 + yr * IR)
txtTotal.Text = Format(TotalAmount,
"Fixed")
End Sub
12

1.3
Create the Form shown here to calculate
• the sum of two numbers. Numbers should be Floats.
• All fonts in Times New Roman, Bold, Size 12.

Coding
Private Sub cmdSum_Click()
Dim FirstNo As Double
Dim SecondNo As Double
Dim Result As Double
FirstNo = CDbl(txtFirstNo.Text)
SecondNo = Val(txtSecondNo.Text)
Result = FirstNo + SecondNo
lblResult . Caption = Format(Result)
End Sub

The first three lines declare variables. Note carefully ‘ Dim, Double, CDbl(), Val(),
Format()..etc.
You can use Val() for the function CDbl() and also Str() for Format().

1.4 Write a program which creation for back color changing .


You can use given coding as follows.
Private Sub Form_Load()
lblDate.Caption = Date
lblTime.Caption = Time
End Sub
Private Sub Timer1_Timer()
Dim Col As Byte
Col = (14 * Rnd + 1)
lblDate.BackColor = QBColor(Col)
lblTime.ForeColor = QBColor(Col)
End Sub
Private Sub Timer2_Timer()
Col = (10 * Rnd + 1)
Me.BackColor = QBColor(Col)
End Sub
Summary:
• Up to now, we have learnt the followings:-
• There are 2 main parts for VB Programming.
– Creating a FORM with objects placed on it.
– Write a CODING ,i.e. a Visual Basic Programming.
Keyword :
• Me Keyword – A way to refer to the specific instance of the class where the
code is executing.
Function :
• Circle Function - Draws a circle, ellipse, or arc on an object.
• Syntax: Circle [Step] (x, y), radius, [color, start, end, aspect]
13

You might also like