VB is not..
• Vendor independent • Platform independent • Based on a constant language definition • Separated definition and IDE implementation • Well documented • (IMO) suitable for very large projects which must be maintained over a long period of time.
1. Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Semester: FOURTH Semester
Name of the Subject:
INTRODUCTION TO VB
PROGRAMMING
Semester: FOURTH Semester
Name of the Subject:
INTRODUCTION TO VB
PROGRAMMING
INTRODUCTION TO
VISUAL BASIC
ENVIRONMENT
2. Hello World in VB
• Start VB
• New Project – Standard .exe
• Click the Button control on the ToolBox
and drag in the form
• Double click the new button to invoke the
code editor
• Enter code:
• Click the Run button
Private Sub Command1_Click()
MsgBox ("Hello world")
End Sub
Exercise – try this out
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
3. What is Visual Basic?
• Kemeny and Kurtz – Dartmouth
College 1964
• For students – simple interpreted
• Many versions since
• MS VB versions – more power
not so simple
• VBScript VBA .NET framework
• RAD especially of user interface
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
4. A very early version
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
5. VB is not..
• Vendor independent
• Platform independent
• Based on a constant language definition
• Separated definition and IDE implementation
• Well documented
• (IMO) suitable for very large projects which must
be maintained over a long period of time
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
6. VB is ..
• easy to use
• suitable for RAD
• very marketable
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
7. Building an application - steps
• Commercial – data driven – waterfall
model – project management
• Science/engineering – underlying data
and physical model, algorithms, testing
• In VB – RAD – focus on user interface
prototyping and review.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
8. Building an application - forms
• VB uses 'form' to mean Window
• Info on form stored in a .frm file
• VB system draws form based on that info
• Forms can be treated like classes in OOP
- later
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
9. Building an application - controls
• Buttons, text boxes, labels, check boxes..
• VB 'control' = user interface widget
• Some invisible – timer
• Controls have properties eg background color
• Three kinds –
– standard
– non-standard MS controls (common dialog, tab) and
3rd party
– ActiveX controls written in-house
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
10. Building an application - modularity
• Spaghetti programming, structured
programming, OOP = increasing modularity
• In VB application constructed from modules =
files in project-
• Form modules
• BASIC modules
• Class modules
• Private and public control interaction
between modules
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
11. Building an application - objects
• Some OOP in VB – not pure OOP
• objects = things eg a form
• class = type of object eg a form design
• property = data value associated with
object
• method = something the object can do
12. Building an application – example of OOP
Dim f As Form2
Set f = New Form2
f.Show
f.BackColor = RGB(255, 0, 0)
Form2 is a class
f is an object – an instance of
class Form2
the Form2 class has a method called show
It has a property called
BackColor
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
13. Event-driven programming
• Standard approach for GUIs
• Contrast with old character interfaces – program
determines what happens
• In GUI, the user triggers what application does
(mostly)
• Event examples are key press, mouse move, timer
timeouts
• Correspond to native Windows Messages (next
slide)
• Event handler = a subroutine which will execute
when that event happens
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
14. The File menu, shown below, will have the following level-two items below
it: New, Open, Save, Save As, Print, and Exit. Note that separator bars appear
above the Save, Print, and Exit items
.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
15. The Help menu contains just one level-two item below it, About.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
16. To build a menu for use with your VB program, you use the Menu Editor, which
appears as an icon in the toolbar of the VB IDE. It is the circled item in the screen
shot below:
Alternatively, you can invoke the Menu Editor from the Tools menu item as
shown below:
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
17. 1. Start a new VB project and invoke the Menu Editor using either method shown
above (click the Menu Editor toolbar icon or select the Menu Editor option from the
Tools menu). The Menu Editor screen appears, as shown below:
2. For "Caption", type &File (by placing the ampersand to the left of the "F", we
establish "F" as an access key for the File item it enables the user to drop down the
File menu by keying "Alt+F" on the keyboard in addition to clicking the "File" item with
the mouse).
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
18. For "Name", type mnuFile.
Your Menu Editor screen should look like this:
Click the Next button.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
19. 3. Click the "right-arrow" button (shown circled below). A ellipsis (...) will appear as the
next item in the menu list, indicating that this item is a level-two item (below "File").
For "Caption", type &New; for "Name", type mnuNew, and for "Shortcut", select Ctrl+N. By specifying a shortcut,
you allow the user to access the associated menu item by pressing that key combination. So here, you are
providing the user three ways of invoking the "New" function: (1) clicking File, then clicking New on the menu; (2)
keying Alt+F,N (because we set up an access key for "N" by placing an ampersand to left of "N" in "New"); or (3)
keying Ctrl+N
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
20. Menu Editor screen should look like this:
Click the Next button.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
21. 4. For "Caption", type &Open; for "Name", type mnuOpen, and for "Shortcut",
select Ctrl+O. Your Menu Editor screen should look like this:
Click the Next button.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
22. 5. For "Caption", type - (a hyphen), and for "Name", type mnuFileBar1. A single hyphen
as the Caption for a menu item tells VB to create a separator bar at that location. Your
Menu Editor screen should look like this:
Click the Next button.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
23. 6. For "Caption", type &Save; for "Name", type mnuSave, and for "Shortcut",
select Ctrl+S. Your Menu Editor screen should look like this:
Click the Next button.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
24. 7. For "Caption", type Save &As ..., and for "Name", type mnuSaveAs. Your Menu
Editor screen should look like this:
Click the Next button.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
25. 8. For "Caption", type -, and for "Name", type mnuFileBar2. Your Menu Editor screen
should look like this:
Click the Next button.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
26. 9. For "Caption", type &Print;for "Name", type mnuPrint; and for "Shortcut",
select Ctrl+P. Your Menu Editor screen should look like this:
Click the Next button.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
27. 10. For "Caption", type -; and for "Name", type mnuFileBar3. Your Menu Editor screen
should look like this:
Click the Next button.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
28. 11. For "Caption", type E&xit, and for "Name", type mnuExit. Your Menu Editor screen
should look like this:
Click the Next button.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
29. 12. Click the "left-arrow" button (shown circled below). The ellipsis (...) no longer
appears, meaning we are back to the top-level items.
For "Caption", type &Help; and for "Name", type mnuHelp.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
30. 13. Click the "right-arrow" button to create a level-two item below "Help". For "Caption",
type &About; and for "Name", type mnuAbout. Your Menu Editor screen should look
like this:
14. At this point, we are done creating our menu entries, so click the OK button. That will
dismiss the menu editor and return focus to the VB IDE.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
31. 15. Back in the VB IDE, your form will now have a menu, based on what you have set up
in the Menu Editor. If you click on a top-level menu item (File for example), the level-two
menu will drop down:
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
32. 16. Click on the New menu item. The code window for the mnuFileNew_Click event
opens, as shown below. Note: Click is the only event that a menu item can respond to.
In thePlace mnuFileNew_Click event, place the code you want to execute when the
user clicks the New menu item. Since this is just a demo, we will place a simple MsgBox
statement in the event procedure:
MsgBox "Code for 'New' goes here.", vbInformation, "Menu Demo"
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
33. 17. Code similar MsgBox statements for the Open, Save, Save As, and Print menu items:
Private Sub mnuFileOpen_Click()
MsgBox "Code for 'Open' goes here.", vbInformation, "Menu Demo"
End Sub
Private Sub mnuFileSave_Click()
MsgBox "Code for 'Save' goes here.", vbInformation, "Menu Demo"
End Sub
Private Sub mnuFileSaveAs_Click()
MsgBox "Code for 'Save As' goes here.", vbInformation, "Menu Demo"
End Sub
Private Sub mnuFilePrint_Click()
MsgBox "Code for 'Print' goes here.", vbInformation, "Menu Demo"
End Sub
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
34. 18. For the Exit menu item Click event, code the statement Unload Me.
Private Sub mnuFileExit_Click()
Unload Me
End Sub
19. For the About menu item Click event, code as shown below:
Private Sub mnuHelpAbout_Click()
MsgBox "Menu Demo" & vbCrLf _
& "Copyright " & Chr$(169) & " 2004 thevbprogrammer.com", , _
"About"
End Sub
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
35. 20. Run the program. Note how the code executes when you click on the various menu
items. Also test the use of the access keys (e.g., Alt+F, N) and shortcut keys (e.g.,Ctrl-
O).
21. Save the program and exit VB.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
36. 36
The Anatomy of a VB Programme Statement
A programme statement is a valid instruction for the Visual Basic Compiler
Programme Statement
Visual Basic Compiler
Keywords properties functions operators symbols
Statement Syntax: is the rules for constructing programme statement
Example: Label1Label1.CaptionCaption = TimeTime
Object
name
VB
Function
Property
name
assignment
operator
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
37. 37
Using Variable to Store Information
VB variables follow the usual rules. There are many types of variables including Byte, Boolea
Integer, Long, Single, Double, Currency, Date, String and Objects.
Variants and sub-types
The programmer has the option of declaring variables with explicit types or as type variant.
The variant type provides dynamic typing: at run time the value has an associated tag to reco
the value currently stored.
Declarations
Dim A ‘ A as variant
Dim B As Integer
Dim C As String ‘variable length string
Dim D As String*10 ‘fixed length string
Const pi = 3.14.59 ‘pi as constant
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
38. 38
Working with Specific Data Types
Data type Size Sample usage
Integer 2 bytes Dim Birds%
Birds%=37
Long 4 bytes Dim Loan&
integer Loan&=32,000
Single precision 4 bytes Dim Price!
floating point Price!=899.98
Double precision 8 bytes Dim Pi#
floating point Pi#=3.1415926535
Currency 8 bytes Dim Credit@
Credit = 567899.90
String 1 byte per Dim Dog$
character Dog$=“Pointer”
Boolean 2 bytes Dim Flag as Boolean
Flag = True
Date 8 bytes Dim Birthday as Date
Birthday = #3-1-63#
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
39. 39
User Defined Data Types
Example: Using Type statement to define a new data type
Type Employee
Name As String
DateOfBirth As Date
HireDate As Date
End Type
Use the new data type
Dim ProductManager As Employee
ProjectManager.Name = “Erick Cody”
ProjectManager.HireData = #3-9-89#
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
40. 40
Working with Arrays of Variables (I/III)
An array is a collection of values stored under a single name
Creating an Array: Before you can use an array, you must declare it.
Declaring a Fixed-Sized Array: Syntax
Public ArrayName (Dim1Elements, Dim2Elements, ...) As DataType
•Public is the keyword that creates a global array
•ArrayName is the variable name of the array
•Dim1Elements is the number of elements in the first dimension of the array
•Dim2Elements is the number of elements in the second dimension of the array
•As DataType is a keyword corresponding to the type of data that will be include
in the array
To declare arrays locally in an event procedure, replace the public keyword with Static keyword and
place the declaration inside and event procedure. Local array can only be used inside the procedure.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
41. 41
Working with Arrays of Variables (II/III)
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
42. 42
Working with Arrays of Variables (III/III)
Creating Dynamic Array
To create a dynamic array, omitting the number of elements in the array. e.g.
Public Temperature ( ) As Variant
Actual size must be set
before use by using the keywor
ReDim
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
43. 43
Working with Visual Basic Operators
Operator Mathematical Operation
+ Addition
- Subtraction
* Multiplication
/ Division
Integer division
Mod Remainder division
^ Exponentiation (Rising to a power)
& String concatenation (Combination)
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
44. 44
Visual Basic Mathematical Functions
Abs (n)
Atn (n)
Cos (n)
Exp (n)
Rnd (n)
Sgn (n)
Sin (n)
Sqr (n)
Str (n)
Tan (n)
Val (n)
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
45. 45
Operator Precedence
Operator Order of Precedence
( ) Values within parentheses are always evaluated first
^ Exponentiation is second
- Nagetive is third
*/ Multiplication and division is fourth
Integer division is fifth
Mod Remainder division is sixth
+- Addition and subtraction is last
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
46. 46
Using Variable to Store Input
Get Input by using InputBox
Exercise: Create a user interface as shown on
the right and type in the code and run the program.
Use the online help to find out more about
InputBox function.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
47. 47
Functions
InputBox is a special Visual Basic keyword known as function.
A function is a statement that performs meaningful work, such as prompt user
for information, or calculating an equation, and then return a result to the program
The value returned by a function can be assigned to a variable, or a property,
or another statement or function
When a function uses more than one argument, the arguments are separated by
commas, the whole group of arguments is enclosed in parentheses, e.g
FullName = InputBox$(Prompt, Title)
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
48. 48
Using a Variable for Output
The MsgBox function uses text strings to display output in a dialog box, the synta
for the MsgBox function is
ButtonClicked = MsgBox(Message, NumberOfButtons, Title)
Message: is the text to be displayed on the screen
NumberOfButtons: is button style number (1 - 5)
Title: is the text displayed in the message box title bar
The variable ButtonClicked is assigned the result returned by the function,
which indicates which button the user clicked in the dialog box.
If you are just displaying a message in MsgBox, the assignment operator (=), the
ButtonClicked variable, and the NumberOfButton argument are optional.
Find out more about MsgBox in the VB online Help.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
49. 49
LAB EXERCISE
1. Practice the Lucky Seven program described in pages 12 -23 of this note.
But make sure that you use the Hungarian convention described in page 24 to name all
objects, e.g., the Spin button is a command button, you can name it as cmdSpin.
Obviously, “cmdSpin” is more meaning than “Command1”.
2. Following this example, can you create a Lucky Eight program?
3. Try and change the colour and other appearances of the objects and user interface.
Note: There is a coins.jpg image in the module’s document directory which you can copy to
your own directory and use in the program.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
50. 50
1 Create a user interface using TextBox and
CommandButton Control
TextBox
2 Set the following properties for TextBox and Command
Control Property Setting
Text1 Text (Empty)
Command1 Caption “OK”
3 Double-click the OK command button and type the
following programme statement between Private Sub and
End sub statement
Text1.Text = “Hello, World!”
4 User the Form Layout window to set the position
of the form when the programmes runs
5. Make sure to use the Hungarian convention
Can you change the appearance, font, text
size,
colour etc of “Hello World!” ?
The “Hello World” Program
LAB EXERCISE)
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
51. 51
LAB EXERCISE
Using Timer Object
A timer object is an invisible stopwatch that gives you access to the system clock from your programs.
A timer object is accurate to 1 millisecond (1/1000 second)
Creating a digital clock by using timer object
1 Open a new project and resize the form to a small window
2 Click the timer control in the toolbox
3 Create a small timer object on the left side of the form
4 Click the Label control in the tool box, and create a label in the centre of the form
that fills most of the form
5 Open the properties window, and set the following properties
Label1 Caption Empty
Font Times New Roman, Bold, 24 point
Alignment 2 - Center
Timer1 Interval 1000
Enabled True
Form1 Caption “Digital Clock”
6 Double click the timer object and type the following code in the Timer1_Timer
event procedure
Label1.Caption = Time
7 Can you display date on your digital clock as well?
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
52. 52
Use File system objects
1 Click the DriveListBox control in the toolbox
2 Click the DirListBox control in toolbox
and then add a directory list box to the form
below the drive list box
3 Add FilelistBox
4 Add Image control
5 Set object properties as follows
Object Property Setting
File1 Pattern *.bmp; *wmf;*.ico; *.jpg
Image1 Stretch True
6 Type the following code
Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Private Sub File1_Click()
SelectedFile = File1.Path & "" & File1.FileName
Image1.Picture = LoadPicture(SelectedFile)
End Sub
Try and browse images in your system
I have put some in the module’s
document directory. Try and
copy them into your local directory
and have a look at those images.
LAB EXERCISE
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
53. 53
The Visual Basic Programming Environment
Form Window
Project Container Window
Project
Window
Properties
window
Form
Layout
window
Tool bar Menu bar
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
54. 54
The User Interface Form (I/II)
A form is a window for creating the user interface of a programme
Programme User Interface
Form
A result screen
after press “Display”
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
55. 55
The User Interface Form (II/II)
A form can contain
Menus
Buttons
List Boxes
Scroll Bars
…….
The default form: Form1
A standard grid to line up elements
of programmes’ user interface
Adjust the size of form using the mouse
Add additional forms using Add Form
command on the Project menu
The run-time position of the user interface
is controlled by the Form Layout window
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
56. 56
The Properties Window
The properties window lets you change the characteristics,
or property settings, of the user interface elements on a form
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
57. 57
The Project Window
A VB programme is made up of several
files that are assembled together or
compiled when a programme is complete.
The project window lists all the files
used in the programming
process and provide access to them via
two special buttons:
View Code and View Object
The project file that maintains the
list of all the supporting files in a
programming project is called the
VB project (.vbp)
Project window displays the
components of the project
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
58. 58
Task, Specification, Algorithm
Lucky Seven: The programme should perform
the following actions
Provide a user interface that has :
Spin and End buttons
3 spinner windows,
a descriptive label,
a winner display window
Pick three random number and display them
when the user click Spin
Display a stack of coins and beep if the
number 7
appears in one of the spinner windows
Terminate when the End button is clicked
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
59. 59
Creating the User Interface (I/IV)
1 On the file menu, click the New Project
command
2 Enlarge the Form window until the scroll bar
appear
in the Project Window as shown on the right
3 Click the CommandButton control in the
toolbox,
then place the mouse pointer over the form
4 Move the mouse pointer close to the upper-left
corner of the form, hold down the left mouse
button,
then drag down and to the right. Stop dragging
and
release the mouse button until you have a button
similar to the one shown here on the right
5 Add a second CommandButton
6 Add the number Labels
7 Add an image
Command button Label Control Image Control
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
60. 60
Setting the Properties (II/IV)
Setting the CommandButton Properties
1 Click the first command button on the form
2 Double click the Properties window title bar
(the Properties window is enlarged to full size)
3 Double click the Caption property in the left column,
and change the current Caption “Command1” to
Spin
4 Open the object drop-down list box at the top of the
Properties window, a list of interface objects appears
5 Click Command2 and change its Caption to End
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
61. 61
Setting the Properties (III/IV)
Setting the Number Labels Properties
1 Click the first number label and then holding down the Shift key
click the second and third number labels.
2 Click the Alignment property, then the drop-down list box to align the
buttons to Centre
3 Click the BorderStyle property and change it to FixedSingle
4 Double-click the Font property and change the Font and Style
(e.g., Times New Roman, Bold, and 24 point size)
5 Delete the captions in the 1st, 2nd and 3rd number labels
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
62. 62
Setting the Properties (IV/IV)
Setting the Descriptive Labels Properties
1 Click the fourth label object on the form
2 Change the caption to “Lucky Seven”
3 Change the Fond property
4 Double-click the ForeColor property and change the colour of your
object
The System tab shows the current colours used for the user
interface
elements in your system, the palette shows all the available
colours
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
63. 63
Setting the Properties
Setting the Image Box Properties
1 Click the image box object on the form
2 Click the Stretch property and set it to True
3 Double-click the Picture property and load a picture using
dialog box
4 Click the Visible property, select False to make the picture
invisible when the programme starts
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
64. 64
Writing the Code
Use the Code Window
1 Double click the End command button on the form, the code window appears
VB subroutine, or
event procedure associated
with a particular object in
the interface
A body of a procedure
always fits between these
two lines and is executed
when a user activates the
interface element associated
with the procedure
The event is
a mouse click
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
65. 65
Writing the Code
2 Type End, and press the Down arrow key.
The End statement stops the execution of a programme and remove it from the screen
End is a keyword recognised by Visual Basic Compiler. VB has several hundred unique
keywords
3 Indent the End statement by placing the cursor to the beginning of the line with End state
in it, and press the spacebar 4 times (the indenting scheme is one of programming
conventions to keep your programme clear and readable, the set of conventions
regarding how programme code is organised is often referred to as programme style
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
66. 66
Writing the Code
Write Code for the Spin button
1 Open the object drop down list box in the
Code window
2 Click Command1 in the list box and write the following code
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
67. 67
Writing the Code
A Look at the Command1_Click Procedure
The Command1_Click procedure is executed when the user click the Spin button on the form
It performs 3 tasks: hides the coin stack, creates random numbers for the label windows and
displays the coin stack when the number seven appears.
Image1.Visible = False 'hide coins
Programme statement
Comment
explanatory notes included in the programme
following a single quotation mark(‘). These notes
are not processed by VB when the programme runs,
they exist only to document what the programme does.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
68. 68
Writing the Code
A Look at the Command1_Click Procedure
The Rnd function creates a random number between 0 and 1
Int (Rnd*10) to create numbers between 0 and 9 and round them to integer number
The numbers are then assigned to the Caption properties of the first three labels,
and the assignment causes the numbers to be displayed in the label windows
The last group of statements checks whether any of the random number is 7,
if one or more of them is, the stack of coin is made visible and beep
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
69. 69
Saving the Programme
1 On the File menu, click the Save Project As command
2 Select your programme file folder
3 Type the file name (e.g. MyLucky) and press enter
Run the Programme
1 Click the Start button on the toolbar
2 Click the Spin button
3 Click the Spin button 10 to 20 times, watching the results
4 Click the End button to finish
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)