Professional Documents
Culture Documents
Programming - 1
A Specialised Training Course
Contents
INTRODUCTION ................................................................................................................... 2
What is Visual Basic? ...................................................................................................... 2
Why Visual Basic? ........................................................................................................... 2
How is Visual Basic so Effective? .................................................................................... 3
Is Visual Basic Object Oriented? ..................................................................................... 3
What are Objects?............................................................................................................ 3
So where does ‘Event Driven Code’ fit in? ...................................................................... 4
Isn’t Basic old-fashioned? ............................................................................................... 4
WINDOWS TERMINOLOGY .............................................................................................. 5
A ‘WINDOW’ .......................................................................................................................... 5
INFORMATION BOXES ............................................................................................................ 7
Dialog Box ....................................................................................................................... 7
Message box..................................................................................................................... 7
CONTROLS ............................................................................................................................. 8
Buttons ............................................................................................................................. 8
Text Boxes ........................................................................................................................ 8
Option Buttons ................................................................................................................. 8
Check Boxes ..................................................................................................................... 8
List Boxes ......................................................................................................................... 8
Pictures / Images.............................................................................................................. 8
A LOOK AT THE CONTROLS.................................................................................................... 9
OTHER FEATURES .................................................................................................................. 9
THE VISUAL BASIC IDE ................................................................................................... 10
WHAT’S ON SCREEN ............................................................................................................ 10
VISUAL BASIC APPLICATION WINDOW................................................................................ 11
Title Bar ......................................................................................................................... 11
Menu Bar ....................................................................................................................... 11
The Toolbar.................................................................................................................... 11
THE FORM WINDOW ............................................................................................................ 12
THE TOOLBOX ..................................................................................................................... 12
PROJECT WINDOW ............................................................................................................... 13
PROPERTIES WINDOW.......................................................................................................... 13
EDIT WINDOW ...................................................................................................................... 13
THE PROJECT ..................................................................................................................... 14
Forms ............................................................................................................................. 14
Code Modules ................................................................................................................ 14
Class Modules................................................................................................................ 15
Custom Controls ............................................................................................................ 15
Resource Files / Binary Data......................................................................................... 15
PLANNING YOUR PROJECT ON PAPER AND OTHER GOOD PRACTICES .................................... 15
MAKE AN EXE FILE ............................................................................................................. 15
EXERCISES .......................................................................................................................... 16
A ‘window’
To avoid confusion I will refer throughout to Windows (3.x, 95/98
or NT), the software package, with a capital W and in italics. An
individual window will be in normal text.
A ‘window’ is a rectangular frame that can contain anything from
a complete application to a group of icons to a picture document.
Your work happens inside these windows. You can have several
of these windows open simultaneously, each running a different
application. By switching the focus from window to window you
can move between them, effectively running multiple applications.
A window containing a running application is an application
window. An application window may contain several child
windows. These are otherwise known as document windows. In
Windows a document can be more than a text file. It can be a
picture, a list of filenames, a group of icons (like within Program
Manager) or another type of document.
Child
Window
Toolbar
Border
Component Description
Title Bar The strip along the top of each window containing the title
of that window.
Border Defines the boundary of a window and allows resizing of
the window with a mouse.
Minimize To the right of the title bar, this reduces the window to an
button icon.
Maximize To the right of the minimize button, this enlarges to window
button to fill the screen when it is replaced by the Restore button.
Close Button Closes window / application
Restore button Replaces the Maximize button when the window is
maximised. It resizes the window to it’s previous size and
the button changes back to the maximize button.
Menu Bar The horizontal strip just below the title bar. It contains the
titles of the menus supported by that window.
Scroll Bars These control which part of the document you view in the
window if the document is larger than the viewing window.
Icon These represent windows that have been minimized. You
can restore the window by double clicking).
Toolbar A single click on one of these generally performs an action
similar to a menu selection.
Dialog Box
A dialog box prompts the user to supply information needed by
the program to continue with the application. There are standard
common dialog boxes used in many Windows applications for
example for file-specification.
A dialog box will usually consist of some input controls (text
boxes, list boxes etc.) and at least two buttons. An OK button is
used to accept the input and proceed. A Cancel button is used to
back-out of the dialog box without accepting any of the input.
Message box
A message box appears when the program or the system needs to
inform you about a specific situation. This may be a warning or
error message.
To indicate the message type it is standard practice to include one
of four icons...
• Exclamation mark
• Question mark
• Stop sign
• Information sign
There should be at least one button to select depending on the
choices available.
Buttons
Respond to a single mouse click. A caption or icon/picture on the
button indicates its use. Buttons generally appear three-
dimensional and sink into the screen when activated.
Text Boxes
A text box is an object containing all the functionality to enter
textual or numerical information into your application. This
functionality includes full editing (insertion, deletion etc.).
A text box may be single-line to enter a single word or number, or
multi-line to enter large blocks of text.
Option Buttons
Otherwise known as Radio-buttons, they appear in groups where
one button only is to be selected. Selecting a new button will
deactivate the previously selected button. If a single button is
required then you should use Check Boxes.
Check Boxes
Often confused with option buttons, a check box is a square which
may or may not be selected (on/off, yes/no, true/false). May
appear in groups, but unlike option buttons any combination of
selected buttons may be allowed.
List Boxes
Provides a list from which to choose one or more items.
Depending on the mode chosen you can select single or multiple
items. If the size of the list is too big for the list box then a vertical
scroll bar is added.
There are two basic types, a standard list box and a combo-box .
In the combo-box only a single selected item is usually shown, but
clicking the down arrow button to the right drops a list from which
to select.
Pictures / Images
These are graphical controls. A picture control is usually used to
display information although it is possible for them to respond to
mouse clicks etc.
Other Features
There are many other features too many to mention at this stage
but that we will cover through the course.
These include DDE (Dynamic Data Exchange) and OLE (Object
Linking and Embedding) - especially OLE, which allow us to
integrate applications. There is also the Windows API, a body of
over 800 functions that we can use to interact with the Windows
environment. This is advanced stuff, but will allow us eventually
to create powerful applications.
For the most part the complexity involved in developing Windows
applications has been dramatically reduced with Visual Basic as
most of the complex details are dealt with automatically - until we
want to take full control ourselves.
What’s on Screen
When we start-up Visual Basic, the following windows appear on
screen. We will work through the windows in turn.
Title Bar
Contains the title “Microsoft Visual Basic [mode]” where the
mode is either [design], [break] or [run].
Menu Bar
As with many Windows applications you have several ways of
selecting menu options; by using the mouse or keyboard to select
an option, or by using a keyboard shortcut. You should
familiarise yourself with the menu commands, but for now here’s
a summary of the important sections...
Menu Activities
File Opening, closing and saving projects.
Printing forms or code.
Creating an EXE file.
Edit Undoing and redoing actions.
Cut / copy / Paste.
Find / replace.
View Affects what you see in the Visual Basic IDE.
Insert For adding extra forms / modules etc.
Run Alters depending on which mode you are in, and lets you
switch between mode. Debug stepping here too.
Tools Tools and Options
Add-Ins Particularly the Data-Manager, other extras can be added
Help Used to access Visual Basic Help files. There are several files
on various topics, each of which can be accessed.
The Toolbar
This consists of a set of buttons / icons used as short-cuts for many
of the menu options.
The Toolbox
This window contains buttons to access the set of controls that you
select to include in your application. As you add or remove
modules from your default project file or your current project file
the buttons change.
The set of tools shown depends on which version and edition you
have and which custom controls you have enabled through the
Project / Components menu.
To add a control to our form we select the relevant button from the
toolbox and click on the form to place a default size control, or
click and drag on the form to place a control of a specific size.
NB. If you open up a project and you want to a control that’s not
on the toolbox then press ctrl+T to load up the tabbed components
dialogue box. During the course various controls will be needed
and you have to use this dialogue box to select the required
controls to add to the general toolbar, so that the controls can be
used in your project.
Properties Window
As you manipulate forms and the controls on those forms this
window shows the properties of the currently selected object(s).
You can change a property at design-time by selecting the
property and editing its value.
Edit window
This is where you can edit the code associated with an object.
Double clicking an object brings up the code edit window.
Forms
These are the workspaces on which we will design our windows
and dialogue boxes. An application may consist of several forms
which can be loaded, unloaded, shown and hidden by the code.
Each form is stored in its own *.FRM file and contains a
description of the form’s appearance, including the controls placed
on it. The form also contains the code for procedures and
variables associated with the form itself and the individual
controls.
Code Modules
In an application with several forms it is likely that there will be
code not specifically attached to a single form. This code
(variables, constants & procedures) is stored in a Code Module
and is available to any form. A Code module has a file extension
of *.BAS
Custom Controls
These are additional controls to the standard set that can be added
to your toolbox. The Professional Edition has many more of these
that the Standard Edition. It is also possible to obtain third-party
controls to add yourself. A custom control has the file extension
*.OCX or the older *.VBX. Active X controls can be regarded as
more complex custom controls.
2. Adjust the size of the form until it is about a quarter of the size of your
screen. Do you notice properties changing as you do this?
3. Add a simple label control. Select the label control (the toolbox button
with a capital A) and click-and-drag a rectangle in the middle of your
form. Make sure that this control is highlighted and change the
following properties. (To change the autosize property between True
and False you can either use the drop-box selector or double click the
property)
Property Old value New value
Autosize False True
Caption Label1 Welcome yourname
4. Reposition the label and resize the window if you like. When you are
happy with your layout Run your application (a grand title at this stage
I know, but from small acorns...).
5. There is not a lot we can do at this stage except play around with the
window, so stop the application.
6. Save the project.
- Select File/Save Project from the menu or click the Save button on
the toolbar.
- You are first asked to select the name and location to store any forms
or modules. The form we renamed in part 2 of this exercise already
has a suitable name (frmWELCO.FRM) . We now need to select the
E:\PROJECTS\WELCOME directory we created earlier and click OK.
- You are now asked to save the project file. PROJECT1.VBP is not
that informative, so rename it WELCOME.VBP. The location is
the F:\PROJECTS\WELCOME directory from before so click OK.
Note: The general routines described above for creating and saving new
projects will be the same except for the filenames for future projects.
2. Add a text box below the label (identify and select the correct toolbox
button, the click and drag a rectangle). Make it approximately the
same size as the label. Change the following properties for this
control...
Property Old value New value
Name Text1 txtName
Text Text1 clear the text
3. Add a command button below the text box, again about the same
width but about three times as tall. Give this button the following
properties...
Property Old value New value
Name Command1 cmdOK
Caption Command1 OK
4. Now for some code. Double-click the button to bring up the code
window. Amend the text for this event to appear as follows...
Sub cmdOK_Click ()
MsgBox "Welcome " & txtName.Text
End Sub
5. Run and use the application. Note how we already have quite a lot
going on, but we have only written a single line of code. Return to
[design] mode.
8. Test your application again. If you are happy with it then save your
project again (the filenames shouldn’t need changing).
9. Make an executable file so that we can run it outside of Visual Basic.
- Select File/Make EXE file...
- Select C:\ as the location for WELCOME.EXE
10. Quit Visual Basic (saving any changes if not done so already).
Load Welcome.exe as an icon in Program manager and run it.
Supplementary Tasks
• Run through the windows tutorial to familiarise yourself with the
terminology etc. (Accessible from the Help menu of Program Manager
or run Start/Help from Windows ‘95/98)
• Try running and investigating some of the other sample applications.