You are on page 1of 26

Introduction to Visual Basic 6.

0
The Visual Basic Desktop

Program Development Cycle

1. Decide what you want the computer to do. 2. Decide how you want your program to look:

Interface design (drawing buttons, forms etc.) 3. Define the properties of these objects: name, colour, size and appearance. 4. Write and attach VB code (event procedures) to each object. 5. Run and test your program.

Program Development Cycle


The code responds to the user interacting with the program through various events: moving the mouse, clicking the mouse or pressing a key. Design before you code. Have a clear idea of what you want your program to do before going to the PC.

The VB Desktop
Toolbox Pull-down menus Tool bar Properties window Project window Form

Immediate window

Form layout window

The VB Desktop
There are 8 main parts to the VB desktop: 1. Pull-down menus - provides access to commands used to build application. 2. Toolbar - provides quick access to commonly used commands in the programming environment. 3. Toolbox - provides a set of tools used at design time to place controls on a form. 4. Project explorer - lists all the files that make up a single VB program.

The VB Desktop

5. Properties window - used to set the properties of forms and controls at design time. 6. Form layout window - enables you to arrange the location of where your forms appear on the screen. 7. Form - provides a window where you can draw objects for the user interface. 8. Immediate window - enables you to debug your VB program.

Pointer Label Frame Check Box Combo Box Horizontal Scroll bar Timer Directory list box Shape Image OLE

Picture Box Text Box Command Button Option Button List Box Vertical Scroll Bar Drive List Box File List Box Line Data Control

The VB Toolbox
This is used to draw the objects on the user interface. Objects include: Pointer - used to select and edit objects. Picture box - display device which can contain bit-mapped pictures, text and line drawings. Label - used to display text that cannot be changed. Text box - a text input device which accepts keyboard input and supports editing. Frame - an object which allows other controls to be arranged in logical groups.

The VB Toolbox
Command button - operates like a push button, which is pressed by clicking the mouse on it. Check box - displays an on or off value. Useful to display or enter data which can be one of two values. Option button - used in a group of similar controls to select between a number of mutually exclusive options. Combo box - can be used to enter or select data. It is essentially a single text box attached to a list. Can be used to input data or select existing data. List box - just like the list part of a combo box.

The VB Toolbox
Horizontal Scroll Bar - control used to set a position or a level of a quantity. Vertical Scroll bar - as above. Timer - operates by running a piece of program code at pre-set intervals. Drive list box - used to select and display from a range of disk drives. Directory list box - used to display files in a specified directory. File list box - used to select a list of files by their attributes.

The VB Toolbox
Shape - used to add objects such as circles, rectangles and rounded boxes to a form. Lines - lines, like graphics can be used to embellish a form. Image - similar to picture box with less events. Used to display bitmaps. Data control - used to access DBs. OLE - used to access other windows programs.

Creating Applications in VB
A VB interface consists of forms and objects. A form is a window that appears on the screen. Most programs have at least one form, although it is common to have many. Objects are items that appear on forms. Objects enable the program to interact with the user.

Creating Applications in VB
The whole purpose of VB code is to tell objects what to do when the user does something i.e. Event-driven. Any time a user presses a key, moves the mouse, or clicks the mouse button, such an action is called an event. Whenever an event occurs VB commands tell the computer that something has just happened.

Objects
Each control has a known set of characteristics: properties that define the objects appearance and behaviour. methods that the program can call to perform specific actions on the object. events that can occur around the object, trigerring automatic calls to specific event procedures in the code.

Objects
Properties locations, colour, values OBJECTS

Events MouseMove, KeyPress

Methods move, clear, show

Managing Projects
The first form in a project is called the startup form. The program loads this form first and executes the code in the Form_Load event. The statement that stops a program when it is running is the End statement. The following code would be attached to an exit button. Sub cmdExit_Click() End End Sub

Defining Properties
After a form is created and some objects are drawn, the next step is to define the properties. An objects properties determine the objects
name, colour, size and general appearance.

Once an object is placed on a form VB assigns default properties - these should always be customised.

Properties
To access a property you can write the object name, a dot and then the name of the property: From1.Backcolor = 0 Text1.Text = Hello Label1.Caption = Text1.Text To find out what properties an object has you can highlight the object on the screen and press the F1 Key. Alternatively use the HELP.

Properties
Properties can be set at design time using the property window. Properties can be set at Run Time using program code. The outside world changes properties automatically.

Events
Planning a VB program is focused on events expected to take place while the program is running. For each object in VB there is a list of pre-defined events to which the object can respond. For example command buttons have several events: Click MouseDown MouseUp MouseMove

These can all be viewed in the code window once the object has been selected.

Events
Most events correspond to actions that may be performed by the person who is using the program/application. e.g. the user clicks a button, selects an option etc. all events are formally defined and recognised by VB. The application code contains blocks of code, known as event procedures. These procedures are designed to respond to specific events.
Event procedure

Private Sub cmdMessage_Click() End Sub


Object name Event

Methods
A method is a built-in procedure that performs an operation on a specific control. For example, list boxes have methods called AddItem, RemoveItem, and Clear, for maintaining list contents. For example:
If KeyAscii = 13 Then List1.AddItem (Text1.Text) End If

Method

Properties, Events and Methods


To summarise: Properties are items of information that describe a particular object. Methods are built-in procedures that take some action on an object. Events are user actions for which customised procedures may be written.

Control Object Naming conventions


Without assigning names to objects in large programs their meanings can often be confusing e.g. Command1, Command2 etc. Microsoft have produced the following convention to reduce confusion and promote standardisation.
Object Form Check Box Combo Box Command Button Data Directory List Box Drive List Box File List Box Frame Grid Horizontal Scroll Bar Prefix frm chk cbo cmd dat dir drv fil fra grd hsb Example frmFileOpen chkReadOnly cboEnglish cmdExit dataBiblio dirSource drvTarget filSource fraLanguage grdPrices hsbVolume

Writing code for applications


The code window is where you will write the VB code for you application. To enter the window double click anywhere on the form. The code window includes the following elements:
object box - displays the name of the selected object. procedure box - lists the procedures (events) for an object.

Writing code for applications


Each object must have code associated with it and an event (e.g. mouse click). Select the required event for the object from the procedure box. Each object has the following event procedure structure:Name of object This is created automatically Sub cmdRectangle_click() ?????? ?????? End Sub Event This is where the code is written

You might also like