You are on page 1of 22

Review VB6.

0 Second Revised Edition

MillenniumYear®

Your Success, Our Strength ™

Second Revised E-Edition

Second Edition
Standard: MY07/11
Serial Key: 2007RVB6P1070705000107

Rohit Birla

1 MY®
Review VB6.0 Second Revised Edition

PREFACE:

VB is modified version of basic language. We have discuss here the basic visual basic version 6.0.
Questions and answers given here onemust try them on omputers. Try to beat microsoft simple projects
likenotepad, wordpad, word, excel and calculator etc… . reader must go with a basic VB book liike
mastering vb6 or peter norton to teach yourself VB or teach yourself VB in 21 days or black book and be
contineue with both book and these notes.

08 Oct,2009

Best of luck

CONTACT DETAILS:

 rohitkumarbirla@gmail.com
 Rohitbirla_rb@yahoo.com
Your suggestions are always invited.

Our projects in VB

 Visual paint 1.0,2.0,3.0


 Snake chess
 Point gain
 Cross road
 Bow and arrow
 Address book
 Phone book
 Ntepad
 Code modeule
 Editor
 Applet editor
 CBPO
 Questionarries
 SIS2007
 SIS2008 (LOC 45k)
And many more…..

# we assume, you are comfortable with ‘C’ and have gone through Review ‘C’atleast once.

2 MY®
Review VB6.0 Second Revised Edition

Intro:

Many programmers tends to dive in and start coding right off bat, but you get into habit of planning like
sit down with blank sheet of paper and sketch out what user interface you want.

- Plan program tasks


- Design user interface
- Write program’s code
- Test and debug with beta version
- Document and distribute program

VB window:

- Menu bar
- Tool bar
- Tool box
- Form window
- Code window
- Project window
- Properties window
- Form window

Contents of tool box:

1. Pointer: used for selecting object placed on form. It is always selected.


2. Label: control display text on form that user can’t edit.
3. Textbox: that user can edit.
4. Picture Box: to display image.
5. Frames: acts as container for controls like option button or check box mainly.
6. Command button: represent an action that is carried out when user click on these acess key
be attach by using ‘&’ before character which you want to be shortcut key.
7. Checkbox
8. Option button
9. Combo box : list control + text box.
10. List box: display a list of item. User can select one or more options.
11. Scrollbars: vertical & horizontal.
12. Timer: 1000 = 1 sec.
13. Drive list, Dir list, file list => used together.
14. Shape: to draw box, circle, oval, rounded box.
15. Line: to draw line.
16. Image control: as picture box. Image control not have alignment property.
17. Data: used to access records from a database.
18. OLE: used to make a two way link with an object in other windows application.
(OBJECT LINKING AND EMBEDDING)

3 MY®
Review VB6.0 Second Revised Edition

VB window:

Variables:

Varibables are used for storing data. A varible has a name and a datatype.

The variable name is used to refer to value in variable and datatype determine the type of data the
variable cans store. A varibale is capable of storing

 User supplied input


 Intermediate results
 Value returned by a function

Rules for variable name:

 Must begin with a letter


 Must not excceed 255 character
 Cant be a reserver word
 Must be unique in its scope

Declaring variables:

 Vb provide us a facility to declare or not variable before complier use them., it’s a good practise
to declare varibales in advance for compiler (explicit declaration) so that compiler produce
efficient code.
 Explicit declaration:

4 MY®
Review VB6.0 Second Revised Edition

o Dim var_name as integer


 Implicit declaration:
o When vb meets undeclare variable then it will declare it on the spot.
o To declare a variable without declaring but of specific datatype, we use suppix like $ for
string, % for integers, & for long, ! for single and # for double.
o Use defxxx statement like defint a-c. now any variable starting with a,b,c, will be of
integer type.
 To make explicit declaration, we write option explicit in first line of code window.

Scope of the variables:

 Private: declare using dim statement and use only for single procedure and scope is local to that
procedure only.
 Module: declare using private or dim, delcare at top of code window, use by all procedure in
that code window
 Public: declare using public keyword, can be use by all forms and all procedures in all modules of
project or application.

Datatypes in VB:

5 MY®
Review VB6.0 Second Revised Edition

Converting variable types:

cbool boolean
cbyte byte
ccur curency
cdate date
cdbl double
val/cint integer
clng long
csng single
cstr string
cstr string
cvar variant
cverr Error
First vb program:

1. Drag and drop three text box on your form and then four command button as shown in figure

2. Select command1 and change its caption property ot ‘+’, same with command2 ‘-‘, command3
‘*’ and command 4 ‘/’.
3. Double click on command1 and in code window write,

Text3.text=cint(text1.text) + cint(Text2.text)

Follow same for command2, write – in between, for command3, write * and command4 for /

4. Press F5, then write a number in text1, second number in text2, press +, -, *, / , to see result

6 MY®
Review VB6.0 Second Revised Edition

If-else:

Msgbox:

Print statement:

 Use for printing on form:


o print “hello”
o print a
 printing on printer
o printer.print text1.text
 printing in a file:
o print #nfile, “hello how are you”

7 MY®
Review VB6.0 Second Revised Edition

Structure of VB program:

Varible declaration

Subroutines or functions

Procedures

Built in datatype:

Like integer, string, object, boolean etc…

User defined types:

Try yourself

 Find largest in three numbers


 Accept a name from user and display it in message box
 Display grade and percentage of a student

8 MY®
Review VB6.0 Second Revised Edition

Arrays:

Occupy continuous memory. Each element in array is referenced by same name and index value.
Dim <array name> (size) as type

Ex: dim a(20)as integer

Note: these array elements are referenced as a(1),a(2) etc… and a(20) means it will save 0 to 20
elements.

1. Array with base1:

Write ‘option base 1’ between array declaration, and all arrays will start from 1 not with
zero.

2. Declaring lower and upper bound:

Dim marks (10 to 20) as integer. Now lowerbound=10 and upperbound=20

3. Checking lower and upper bound:


a. Msgbox a.lbound
b. Msgbox a.ubound
4. Declaring dynamic array:
1. Dim a() as integer
2. Redim a(20) (it will lost all data)
3. Redim preserve a(16) (to save data)

Multi dimension array:

a) Dim r(2,2) as integer


b) Dim mat(1 to 3, 1 to 4) as integer
c) Dim add(3,4,5,6) as integer
There is no limit on number of dimensions in VB

Control arrays:

1. Copy textbox (which is on form) and paste on form. It will give a message, do you want to
create control array, say yes. Now both textbox have same name ‘text1’ but differ by their
index value 0 and 1 respectively.
2. Similarly try to create control array of label, command button etc…
3. Load statement are used to create run time control arrays
a. Load text1(k)
b. Unload text1(k)

9 MY®
Review VB6.0 Second Revised Edition

Static and dim variables:

 static preserves their last values while dim not

Arithmetic operators:

^ exponential
+ add
- minus
* multiply
/ division
\ integer division
mod reminder

Boolean operators:

< less than


> greater than
<= less than equal to
>= greater than equal to
= equal to
<> non equal to

Logical operators: AND OR NOT

Concatenation operator:

Dim s as string
S=”hello”
S=s & “!” & “how are you”
Print s
S = s & “a=” & a & “b=” & b
Print s

‘ VB is case – insensitive language

10 MY®
Review VB6.0 Second Revised Edition

Try to solve:

1. Implement control arrays for run time


2. Perform arithmetic operations
3. Implement static and dim in your program and find
difference
4. Give an example for and, or and not operator

Looping:

11 MY®
Review VB6.0 Second Revised Edition

Properties of control arrays:

 a control array have at least one control element and can grow up to 32767 elements
 each control must be of same type
 each control have same property (name)
 each control is identified by unique index value
 we can remove only those controls which are added at run time
 we usually make control arrays differ by top and left properties

Subroutine and function:

 function can return a value while subroutine not


 function define with keyword function and subroutine by sub
 both makes program understandable
 both can be private or public
 both can be called by each other

12 MY®
Review VB6.0 Second Revised Edition

1. Try to pass array in a function


2. Find factorial of a number
3. Write Fibonacci sequence
4. Check built in inputbox function
5. Procedures can be private or public, describe with example
6. How to check return values of msgbox function
7. Make a function that swap two values

Events:

An event is user action directed at application like clicking with mouse. VB has given subroutines
or events handlers for handling events. The combination of control name and events name is unique.
There are many types of events like of mouse - click, dbclick, mouse down, mouse up etc…

Form events: initialize, load,activate, gotfocus, drag,drop,resize,deactivate and unload

Form methods: hide,show,unload and end

Q: try to use show and hide functioning

Properties of form: backcolor, borderstyle,caption, forecolor, control box, name, window state

Scale modes: have you noticed that scalex, scaley, scale width, scale height. Change these properties
and check difference.

With statements:

To change multiple properties of a control, we use with statement like:

With text1

.name=”text”

.caption=”hello”

.forecolor=vbred

End with

13 MY®
Review VB6.0 Second Revised Edition

Printer object:

 printer.print “hello”
 printer.print str
 printer.print text1.text
 printer.fontname=”Tahoma”
 printer.fontsize=16
 printer.font bold=true
 printer.new page
 printer.enddoc
 printer.line(x1,y1)-(x2,y2),color
 printer.circle(x,y),r,color
 check other properties like fill color, fill mode, draw mode, draw width, draw style, scale mode
etc.
 printer1.paint picture picture1.picture,x1,y1,width,height,x2,y2
 printer.copies
 printer.orientation=vbporlandscapre
 a=printer.page
 printer.port
 printer.zoom

Q: find difference between picture box and image box

Colors:

vb has some built in color like vb red,vb blue, vbblack,vbwhite

Q: tryout common dialog boxes.

[hint: goto project menu, click components, select Microsoft common dialog control, click ok.
cd1.showcolor
Cd1.showopen
cd1.filename
cd1.showprint
cd1.showsave
cd1.color etc… try these out]

14 MY®
Review VB6.0 Second Revised Edition

Picture box vs image control:

 picture box have alignment property


 size of picture is fix in picture box while image control can change sckretch property
 in picture box, we can draw graphics like line etc..but not in image control
 both are use for showing picture
 both use load picture method for loading picture

MAPI:

Mail application programming interface, you can send-receive email via your vb application, but
it is not quite easy as it seems this control is related to outlook express and it is not quite handy if you
didn’t configure your setting in outlook express or so… it is basically a universal provider for shelring bits
of functionality in email applications such as outlook and outlok express etc…MAPI has been drastically
deemphasized in windows due to the outbreak of self spreading worms (email viruses). A modern easy
to use MAPI is CO.

MAPI is a system built into Microsoft windows that enables different email applications to work
together to distribute mail. as long both applications are MAPI enables, they can share mail message
with each other.

Q: what is API, write a short note on built in APIs

Q: creating menus [hint: click on menu tool box from tool bar,

& for alt enabling

-> for submenues ]

15 MY®
Review VB6.0 Second Revised Edition

Error handling:

16 MY®
Review VB6.0 Second Revised Edition

Object oriented programming:

1. click project > add class module and create functions in it


2. call these functions by creating their objects, and that’s oops!

API examples:

17 MY®
Review VB6.0 Second Revised Edition

File handling:

18 MY®
Review VB6.0 Second Revised Edition

Currency control and locking:

It is mechanism used by DBMS for sharing of data. Atomicity, consistency and isolation are
achieved through currency control and locking. The amount of data that can be locked with single
instance or group of instance defines granularity of lock.

 Page locking: all data on specific page are locked


 Cluster locking: data cluster together and locked simultaneously
 Class or table locking: all instances of either a class or table are locked
 Object or instance locking: lock a single relation tuple in RDBMS or a single object in ODBMS

1. Try to beat Microsoft standard calculator


2. Try to implement notepad and address book
3. Make a program that run in startup and add your daily used
features in it, so no more start menu, no window, now every
thing in a lick in front of you.
4. Try to implement code module for C/C++

SDI vs MDI:

SDI have single data window ex: wordpad, notepad. You can open one document at a time. Single
document interface. Multiple document interface. One parent form and that form contain other forms
and control them known as child form ex: word

Features of MDI:

1. an application can have only one MDI parent form


2. MDI contain only those controls that supports alignment property
3. We can’t display information on MDI form
4. MDI and all child of it represent single icon on taskbar
5. When a child is minimized, all its child form minimized and also parent
6. Parent visible in taskbar
7. When a child is maximized, it cover entire area of parent form
8. Two unique properties of MDI
a. Auto show child: if true, then when parent opened, child itself displayed
b. Scroll bar: if true, then if child exceed beyond boundary of parent, then scrollbar is
shown

19 MY®
Review VB6.0 Second Revised Edition

Send keys:

Is a module for windows which can be used to send one or more keystrokes or their combination to
active window.

+ shift
^ ctrl
% alt

ctrl a given as ^a
shift abc is given as +{ABC}

{BACKSPACE}
{BREAK}
{CAP}
{DOWN}
{DEL}
{up}
{LEFT}
{RIGHT}
{HOME}
{END}
{HOME}
{LWIN}
{RWIN}
{ADD}
{MULTIPLY}
{DIVIDE}
{SCROLLLOCK}
{PRTSC}
{NUMLOCK}
{PAGEUP}
{PAGEDOWN}
{INSERT}
{ESC}
{F1}…{F24}
{TAB}

20 MY®
Review VB6.0 Second Revised Edition

Q: open notepad, write hello to it and close it without saving

Ans:
import sendkeys
Sendkeys.sendkeys { “””
{LWIN}
{PAUSE .25}
R
NOTEPAD.EXE {ENTER}
{PAUSE 1}
HELLO
{PAUSE 2}
%{F4}
N
“””}

21 MY®
Review VB6.0 Second Revised Edition

22 MY®

You might also like