SlideShare ist ein Scribd-Unternehmen logo
1 von 118
Downloaden Sie, um offline zu lesen
UNIVERSITY OF DAR ES SALAAM
COMPUTING CENTRE
Certificate in Computing and Information
Technology
PROGRAMMING WITH VISUAL BASIC 6.0
MANUAL
PAGE 2 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
© 2005 University Computing Centre ltd
University Road
P.O Box 35062
Dar es Salaam
Tanzania
Tel: +255 (022) 2410645
Fax: +255 (022) 2410690
Email: training@udsm.ac.tz
Internet: http://www.ucc.co.tz
All trademarks acknowledged. E&OE.
© University Computing Centre ltd. No part
of this document may be copied without
written permission from University
Computing Centre unless produced under
the terms of a courseware site license
agreement with University Computing
Centre.
While all reasonable precautions have been
taken in the preparation of this document,
including both technical and non-technical
proofing. University Computing Centre ltd
PAGE 3 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
and all staff assume no responsibility for
any errors or omissions. No warranties are
made, expressed or implied with regard to
these notes. University Computing Centre
ltd shall not be responsible for any direct,
incidental or consequential damages arising
from the use of any material contained in
this document. If you find any errors in
these training modules, please inform
University Computing Centre ltd. Whilst
every effort is made to eradicate typing or
technical mistakes, we apologise for any
errors you may detect. University
Computing Centre ltd manuals are updated
on a regular basis, so your feedback is both
valued by us and will help us to maintain
the highest possible standards.
PAGE 4 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
COURSE DESCRIPTION............................................. 8
COURSE OBJECTIVES............................................... 8
1. DESIGN PROGRAMS IN A VISUAL LANGUAGE. ........ 9
2. CODE PROGRAMS WITH VISUAL FEATURES IN A
WINDOWS ENVIRONMENT. ........................................ 9
3. CREATE PROGRAMS THAT ARE EVENT-DRIVEN. .... 9
4. CREATE VISUAL BASIC CONTROL AND DATA
ARRAYS. ................................................................... 9
DELIVERY METHODOLOGY........................................ 9
Chapter 1: Introduction to the Visual Basic
Environment, Forms, Controls and Properties
................................................................................. 11
1.1. STARTING VISUAL BASIC................................. 11
1.2. THE VISUAL BASIC ENVIRONMENT................... 11
1.2.1 The tool bar............................................... 12
1.2.2 The tool box.............................................. 12
1.2.3 The form designer window ...................... 13
1.2.4 The properties window............................. 14
1.2.5 The project explorer window.................... 14
1.2.6 The form layout window........................... 15
1.3. CREATING AN APPLICATION ............................. 16
1.4. DESIGNING THE FORM ..................................... 16
1.4.1 Drawing controls....................................... 17
1.4.2 Moving controls ........................................ 18
PAGE 5 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
1.4.3 Resizing controls...................................... 18
1.4.4 Resizing the form...................................... 19
1.4.5 Setting properties ..................................... 20
1.4.6 Adding the button controls....................... 22
1.5. RUNNING YOUR APPLICATION .......................... 24
1.6. SAVING YOUR APPLICATION............................. 25
EXERCISES............................................................. 26
Chapter 2. INTRODUCTION TO EVENTS AND
PROGRAMMING................................................... 29
2.1 PROGRAMMING IN VISUAL BASIC...................... 29
2.1.1 What are events? ..................................... 29
2.1.2 Opening an existing project..................... 29
You will now add code to the ‘Hello’ project,
which you created in the last chapter............... 29
2.2WRITING CODE ................................................. 30
2.3 RUNNING YOUR APPLICATION ........................... 35
2.4 HELP FOR VISUAL BASIC .................................. 35
2.5 INPUT DIALOG BOXES ....................................... 36
2.6 THE BACKCOLOR PROPERTY ........................... 38
EXERCISES............................................................. 39
Chapter 3. Text, Check Boxes and Buttons.... 42
3.1 TEXT BOXES ..................................................... 42
3.1.1 Creating a text box................................... 43
3.1.2 The Name property .................................. 44
3.2 ADDING A LABEL ............................................... 48
3.3 CHECK BOXES .................................................. 49
3.4 OPTION BUTTONS............................................. 57
3.5 FINISHING THE FORM........................................ 63
3.6 PROGRAMMING NOTES .................................... 64
PAGE 6 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
EXERCISES............................................................. 66
Chapter 4. List boxes, combo boxes and forms
................................................................................. 68
4.1 PROJECTS WITH MORE THAN ONE FORM........... 68
4.2 LIST BOXES ...................................................... 72
4.2.1......................................... What is a list box?
72
4.2.2 Creating a list box..................................... 73
4.3 COMBO BOXES................................................. 84
4.4 PROGRAMMING NOTES .................................... 85
4.4.2......................................................... Methods
86
EXERCISES............................................................. 88
Chapter 5. Multiple Document Interface (MDI)
Applications, Toolbars, Using the Clipboard,
Popup Menus........................................................ 91
5.1 MULTIPLE DOCUMENT INTERFACE APPLICATIONS
............................................................................... 91
5.2 BUILDING THE APPLICATION ............................. 91
5.2.1 Creating the MDI form.............................. 91
5.2.2 Adding the child form ............................... 92
5.2.3 Resizing the rich textbox.......................... 93
5.2.4 Setting up the menus............................... 93
5.3 CLOSING MDI APPLICATION FORMS ................. 97
5.3.1 Deciding if changes to data in a child form
have been saved................................................ 97
5.3.2 Closing the application........................... 101
5.4 USING THE CLIPBOARD .................................. 102
5.5 ADDING A TOOLBAR........................................ 104
PAGE 7 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
5.5.1 Using an ImageList control to store button
pictures for the toolbar..................................... 105
5. 5.2 Setting up the toolbar............................ 107
5. 5.3 Adding the code..................................... 109
5. 5.4 Adding an option to view the toolbar.... 111
5.6 POPUP MENUS............................................... 112
EXERCISES........................................................... 115
REFERENCES ....................................................... 117
VB Reference Books ....................................... 117
PAGE 8 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Course Description
This course guides the student through the process of
creating programs in Visual Basic. Provides a task-
driven experience to allow students to perform complex
programming tasks more easily than would-bepossible
without a visual language.
Course Objectives
The objective of this course is to give studentsa more
thorough understandingof Visual Basic so that they
may apply for a position as a Visual Basic Programmer.
At the end of the course students should be
able to
PAGE 9 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
1.Design programsin a visual language.
2.Code programs with visual features in a
Windows environment.
3.Create programs that are event-driven.
4.Create Visual Basic control and data
arrays.
Delivery Methodology
The course will be delivered in form of
lecturers…. in the classroom and in the
Computer lab accordingly. Exercise with real
life nature will be provided during and at the
end of the class. The manual is also designed
such that one can follow the course at own time
and pace….
PAGE 10 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
PAGE 11 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Chapter 1: Introduction to the Visual Basic
Environment, Forms, Controls and Properties
1.1. Starting Visual Basic
Starting Visual Basic is the same as starting any other
Windows application.
Step 1 Launch Visual Basic from the Start menu
by choosing Programs; Microsoft Visual
Studio 6.0; Microsoft Visual Basic 6.0
Depending on the environment settings, you may see
the New Project dialog box.
Figure 1-1
Step 2 If the New Project box appears, select
Standard.EXE, and then click Open.
1.2. The Visual Basic environment
The Visual Basic environment is made up of several
windows. The initial appearanceof the windows on
PAGE 12 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
your screen will depend on the way your environment
has been set up.
Figure 2-1
1.2.1 The tool bar
The Visual Basic tool bar functions like the tool bar in
any other Microsoft application. It provides shortcuts
for many of the common operatingcommands. It also
shows you the dimensions and location of the form
currentlybeing designed.
Figure 2-2
1.2.2 The tool box
The tool box gives you access to the controlsthat you
use on a form.
PAGE 13 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Figure 2-3 A control is an object such as a
button, label or grid.
Controlsare used on forms to display outputor get
input.
Each control appearsas a buttonin the tool box. If the
control you are looking for is not in the toolbox, select
Componentsfrom the Project menu.
If the tool box is not displayed on your screen, or if at
any time during the exercises you close it, choose
Toolboxfrom the View menu.
1.2.3 The form designer window
This window is where you design the forms that make
up your user interface.
PAGE 14 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Figure 2-4
If the form designer window is not displayed on your
screen, or if at any time during the exercises you close
it, choose Object from the View menu.
1.2.4 The properties window
A form, and each controlon it, has a set of properties
which controlits characteristics such as size, position
and color.
The propertieswindow lists all the properties a control
has and their value. Setting the propertyvalue using the
propertieswindow when you design your application or
changed by assigning a new value in code while your
application is runningcan change the default value of a
property.
If the propertieswindow is not displayed on your
screen, or if at any time during the exercises you close
it, choose Properties Windowfrom the View menu.
1.2.5 The project explorer window
PAGE 15 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
A project is a collectionof the forms and code that
make up an application. A file in the project explorer
window representseach form in your application.
Figure 2-5
A form file containsboth the description of the screen
layout for the form and the program code associated
with it. If the project explorerwindow is not displayed
on your screen, or if at any time during the exercises
you close it, choose Project Explorerfrom the View
menu.
1.2.6 The form layout window
Move the form in the screen in this window to set the
position of your form when your applicationis running.
Figure 2-6
You may wish to close the form layout window to
allow more space for the properties window. To
open the window again, select Form Layout
Window from the View menu.
PAGE 16 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
1.3. Creating an application
There are three steps to creating an application.
1. Design the forms by drawing controls.
2. Set initial propertyvalues for the forms and controls.
3. Write code to perform the required tasks.
In the exercise that follows you will create a single form
as shown below.
Figure 3-1
The exercise shows you how to add controlsto a form
and how to set propertiesat design time. This first
application will not actuallydo anything until you add
code as shown in chapter2.
1.4. Designing the form
The form has three controls on it. There is a label which
will display the text “Hello”and two command buttons.
A label control is used to display text.
A command buttoncontrolenables the user of your
application to click on a buttonwhen they require an
action to be carried out. You will learn later how to
write the code that will be run when the button is
clicked.
PAGE 17 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
1.4.1 Drawing controls
Drawing controlsis a two step process. First you need
to add the controlyou want by doubleclicking on it in
the tool box. Secondly, you need to position and size
the control by clicking and dragging on the form.
Firstly you will create the label which contains the text
“Hello”.
Step 3 Move the mouse over the controls in the
toolbox until the tool tips indicate that you
have found the label control.
Figure 4-1
Step 4 Double click on the label control button
and a label will appear in the middle of the
form.
PAGE 18 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Figure 4-2
1.4.2 Moving controls
Controlscan be moved around a form by dragging them
with the mouse.
Step 5 Move the cursor to the label and depress
the left mouse button. Keeping the mouse
button depressed, drag the control to its
new location, shown in Figure 4-3.
Figure 4-3
Step 6 Release the mouse button.
.
1.4.3 Resizing controls
To achieve the form design shown in figure 3-1, you
need to make the label controllarger.
PAGE 19 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Before a controlcan be resized it must be selected.
When a controlis selected black dots or handleswill
surround it.
Clicking and dragging one of these handleswill adjust
the size of the control.
Step 7 Click on the label control to select it.
Figure 4-4
Step 8 Drag the bottom right handle to enlarge
the control, then release the mouse
button.
Figure 4-5
1.4.4 Resizing the form
The form can be resized in the same way as a control.
PAGE 20 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 9 Position the mouse pointer over the right
hand edge of the form.
The mouse pointer will change to a doubleheaded
arrow.
Step 10Hold down the left mouse button and drag
the edge of the form to the left to make the
form smaller.
1.4.5 Setting properties
Once the control is positioned correctlyon the form you
then need to customise the propertiesof the control to
suit your requirements.
This application requires you to change the text that is
displayed in the label control from the default “Label1”
to “Hello”. You are also going to change the alignment
to Centre and the font to Arial with font size 16.
To change the propertiesfor a specific control,you
must have the controlselected.
Step 11 Click on the label control on your form to
select it.
Handles will appear on the label control to show it is
selected.
PAGE 21 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 12If the properties window is not visible,
choose Properties Window from the View
menu or right click on the label control and
select Properties.
Step 13 Check that the Properties window shows
the properties for the label control.
Figure 4-6
The Caption propertydetermines what text is displayed
in the label control.
Step 14If the Caption property is not highlighted,
click on it to select it. If Caption is not
visible in the list, use the scroll bar on the
right to display it.
Step 15 Type Hello in place of Label1
Step 16 Select the Alignment property and click on
the drop down arrow to reveal the choices
for this property.
PAGE 22 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 17 Choose „2 - Centre‟.
Step 18 Select the Font property and click the
dialog button (…) to bring up the font
window. Set the font values to Arial,
Regular, 16.
Figure 4-7
1.4.6 Adding the button controls
You can use the same process to add two buttons to the
form.
Step 19 Double click on the command button
control in the tool box.
PAGE 23 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Figure 4-8
A command buttoncontrolwill appear in the middle of
the form.
Step 20 Move the button so that it is under the
label.
Step 21 Repeat the last two steps to add a second
button under the first.
Your form should appear similar to the one shown in
figure 4-9.
Figure 4-9
Step 22Select the button with the caption
Command1. Make sure you can see the
handles that indicate that the button is
selected.
PAGE 24 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 23 Using the properties window; change the
caption property to On.
Step 24 Use the same process to set the caption
for the bottom button to Off.
Step 25Using the properties window; change the
Name property for the form to Hello.
1.5. Running your application
Now that the form is complete you can see it in action
by running it.
When you have written code for the buttons, running
the application will allow you to activate the code. For
now your buttonswill not do anything.
Step 26 Click on the start (play) button on the tool
bar.
Figure 5-1
PAGE 25 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Your form will appear like a window from any other
Microsoft application.
Step 27 Click on one of the command buttons to
see it depress.
Step 28Click on the Stop button on the tool bar to
return to the Visual Basic design
environment.
1.6. Saving your application
The last step in this chapteris to save your application
so that you can use it for the exercises later in the book.
Step 29 Click on the save button on the tool bar.
Figure 6-1
Visual Basic first asks you to save the form and then the
project file. Remember that each representsa separate
file.
Figure 6-2
PAGE 26 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 30Change the directory to the a: drive or to
the folder where you want to save your
work.
Specify the filename for the form as hello.frm. The file
extension “frm” indicates that the file is a form file.
Step 31 Click on the Save button.
Figure 6-3
Step 32Change the name of the project to
hello.vbp and click on Save. The file
extension “vbp” indicates a project file.
Always take care to ensure that you save all the files
that make up a project.
Exercises
Exercise 1
1.Open a new project by choosing New Project
from the File menu.
PAGE 27 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
2.Design the form shown below. Note that you
should set the caption property for the form to
Project One.
3.Change the Name property for the form to
frmMain.
4.Change the MousePointer property for the
form to „2 - Cross‟. This is one of the values in
the drop down list for this property.
5.Change the MousePointer property for the
command button to „1 - Arrow‟
6.Run the project and move the mouse across
the form to check that the cursor changes
when it is over the command button.
7.Save the project as Project1. (Use the default
name of frmMain.frm for the form filename.
8.Close Visual Basic.
Exercise 2
1.Launch Visual Basic
2.Open the project you saved in Exercise 1.
3.Using the form you created above, add a
second button as shown below.
PAGE 28 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
4.Change the Mouse Pointer property for the
new button to „1 - Arrow‟.
5.Save the project using the same file names as
before.
PAGE 29 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Chapter 2. INTRODUCTION TO EVENTSAND
PROGRAMMING
2.1 Programming in Visual Basic
Now that you have designed a form and set some
properties, it is time to make your applicationdo
something.
Visual Basic applicationswork by executing code
written in the Visual Basic programming language.
Code is associated with events.
2.1.1 What are events?
Each Visual Basic object has a set of events. Events are
actions which Visual Basic can detect and respond to.
For example, a user clicking on a command button on a
form will generate a click event for that button.
When an event is generated Visual Basic will run any
code you have entered for that event.
2.1.2 Opening an existing project
You will now add code to the „Hello‟ project, which
you created in the last chapter.
PAGE 30 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 1 Choose Open Project from the File menu.
Step 2 Change to the appropriate drive and
folder and open hello.vbp.
Figure 2-1
The project window will display the file “Hello.frm”
from your project.
Figure 2-2
Step 3 If the form is not visible, click the View
Object button.
2.2 Writing code
In this exercise you will write code to make your
„Hello‟ label visible and invisible to the user when the
On and Off buttonsare clicked.
PAGE 31 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Each object has an associated set of events which it can
respond to. To enter the code, you must access the code
window.
Step 4 Double click on the „On‟ button or click the
right mouse button and select View Code
from the object menu.
Figure 2-3
The code window will appear as shown below.
Figure 2-4
Clickon the Object menu and select Command1
from the list.
The code window has three main components.
PAGE 32 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
1.The object menu allows you to change
between controls. For example, you could use
this menu to look at the code for the Off
button.
2.The event menu lists all of the available events
for the selected control. This list will change
depending on the type of control selected.
3.The code for the event is shown in the main
part of the window. Your code will appear
between the Private Sub and End Sub
statements.
Step 5 Position the cursor in the blank line
between the Private Sub and End Sub
statements.
Figure 2.5
Visual Basic 5.0 uses the programming language Visual
Basic for Applications, the same language found in
many Microsoft Office applicationsincluding Microsoft
Excel, Microsoft Access and Microsoft Project.
The statement you are going to type assigns a value to
the Visible propertyfor the label on your form. This
propertyis set to the value True by default at design
time. Your Visual Basic statement will change the value
of the propertywhen the command buttonis clicked.
PAGE 33 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
The syntax to refer to a propertyin code is:
ObjectName.PropertyName
The syntax to assign a value is
ObjectName.PropertyName= Value
Step 6 Press the tab key to indent the line of code
and type Label1.Visible = True, and
press the Enter key.
Figure 2.6
Notice that the True has turned blue. This indicates that
True is a special word or symbol that Visual Basic
understands. Such words are known as reserved words.
Step 7 Close the code window.
You will now follow the same process to enter the code
for the Off buttonto make the „Hello‟label invisible
when this buttonis clicked.
Step 8 Double click on the Off button to access
the code window.
Step 9 Position the cursor between the Private
Sub and End Sub statements.
PAGE 34 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 10 Press Tab and type Label1.Visible =
False. Press the Enter key.
Step 11 Close the code window.
Whatthe does the code do?
The code under your On button will appear as shown
below.
PrivateSub Command1_Click()
Label1.Visible= True
End Sub
Each Visual Basic event has a subprogram associated
with it. This is why Visual Basic automaticallyincludes
the Sub and End Sub commands in your code.
The name of the control(in this case Command1)and
the event (in this case Click)will be joined together to
form the subprogram name. This name follows the Sub
statement.
The line of code that you typed for the On button sets
the Visible propertyof the control named Label1 to
True (to make it visible).
Similarly, the code underthe Off button sets the Visible
propertyfor the Label1 control to False (to make it
invisible). You can now change the value of the Visible
propertywhile you are running your application.
PAGE 35 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
2.3 Running your application
Now that the code has been written your buttonswill
operatewhen you run your application.
Step 12 Click on the start button on the tool bar to
run your application.
The “Hello” label will initially be displayed because the
visible propertyin the propertieswindow was set to
true.
Step 13 Click on the Off button.
The label will disappear.
Step 14 Click on the On button to make the text
visible again.
Step 15When you have finished experimenting
click on the end button on the tool bar to
return to Visual Basic.
2.4 Help for Visual Basic
The Visual Basic environment provides you with a very
comprehensive help system. You should also take
advantage of the Microsoft on the Web feature for more
PAGE 36 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
detailed instructionsand examples of how to carry out
tasks.
Figure 2-7
2.5 Input dialog boxes
In this exercise you will add a third buttonto the form
which will enablethe user to change the text displayed
in Label1.
An easy way to get user input, is by using input dialog
boxes.
Step 16 Add a third button to your form as shown
below.
Figure 2-8
Step 17 Change the button‟s caption to “Change
Text”.
PAGE 37 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Having added the buttonyou can now write the code to
make it work.
Step 18 Double click on the new button to display
the code window.
Step 19Enter the following code. Note that the
Sub and End Sub lines will already be
entered by Visual Basic.
PrivateSub Command3_Click()
Label1.Caption= InputBox("Enter the new text")
End Sub
Step 20 Close the code window.
Whatdoes the code do?
In the same way that you set the Visible propertyfor the
Label1 control earlier on in the exercise, this code sets
the Captionproperty.
The InputBox function displays a dialog box to the user
that allows them to enter in a value. This value is then
returned by the function and assigned to the Caption
propertyfor Label1.
Step 21 Run the application to test your changes.
Step 22 Click on the Change Text button.
PAGE 38 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
The input dialog box shown below will appear.
Figure 2-9
Step 23 Type in your name and click OK.
Your name will now appearas the label on the form.
Step 24 Click on the end button on the tool bar to
return to Visual Basic.
2.6 The BackColor property
The finishing touch for your application will be to
change the colourof the form to green.
Step 25Click anywhere in the form itself to select
it. Be careful not to click on a control.
Step 26In the properties window select the drop
down menu for the BackColor property
and select Palette. Remember, if you
cannot see the BackColor property, use
the scroll bars to move up or down the list.
The color palettewill appear.
PAGE 39 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Figure 2-10
Step 27 Click on a green square to set it as the
new color.
The color of the form will change.
Step 28 Change the Back Color property for
Label1 to a color of your choice.
Step 29 Run your application to test your changes.
Step 30 Save your project.
Exercises
Exercise 1
1.1 Open a new project by choosing New Project from
the File menu.
PAGE 40 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
1.2 Design the form shown below with three labels and
one command button.
Figure 2-11
1.3 Write the following code for the click event of the
Time button.
PrivateSub Command1_Click ()
Label2.Caption= Time
End Sub
1.4 Run your application.
When you click on the time buttonthe current time
should appear as shown below.
Figure 2-12
1.5 Save your application underthe names time.frm
and time.vbp.
PAGE 41 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Exercise 2
2.1 Using the form you created above, add a second
buttonand change the Label3 caption as shown below.
Figure 2-13
2.2 Write the following code for the click event for the
Date button.
PrivateSub Command2_Click()
Label1.Caption= “The date is:”
Label2.Caption= Date
End Sub
2.3 Add a line of code to the Command1_Click
subprogram so that the caption for Label1 is set
back to “The time is:” when the Time buttonis
pressed.
2.4 Change the BackColor of the form to a colourof
your choice.
2.5 Run your applicationto test that it is working.
2.6 Save your application files with the names
DateTime.frm and DateTime.vbp.
PAGE 42 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Chapter 3. Text, Check Boxes and Buttons
3.1 Text boxes
In the last chapter you used an input dialog box to get
input from the user. In this chapteryou will use a text
box control.
You use a text box rather than an input dialog box when
you want the user to enter data directly onto the form.
The text box controldefines an area on a form where
the user can type text. Visual Basic automatically
responds to the delete and backspace keys, and other
text editing functions such as copy and paste, without
the need for any programming.
In this chapteryou will create the form shown below
which will allow the user to enter some text and format
it.
Figure 1-1
Firstly open a new project.
Step 1 Choose New Project from the File menu,
and select Standard EXE.
PAGE 43 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
3.1.1 Creating a text box.
Adding a text box to a form is exactly the same as
adding any other Visual Basic control.
Step 2 Double click on the text box control in the
tool box window.
Figure 1-2
PAGE 44 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 3 Click and drag in the text box to move it to
where you want it and to make it larger.
Your text box should appearas shown below.
Figure 1-3
3.1.2 The Name property
When you draw a controlon a form Visual Basic
automaticallyassigns the controla name. In this case
“Text1”.
The default name is determined by the type of control
and the number of those controlson your form.
The name is used to refer to a control when
programming. It is good practice to change the name to
something meaningful to your application.A useful
convention is to give the control name a prefix which
indicates the type of control.
You will change this text box control‟sName to
txtInput.
PAGE 45 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 4 Check that the Text1 control is selected. It
will appear on your form surrounded by
black handles.
Step 5 If the properties window is not visible, click
the right mouse button and select
Properties from the object menu.
Step 6 Click on the Name property in the
properties window. If the Name property is
not visible, use the scroll bars to display it.
Figure 1-4
Step 7 Type txtInput as the new name for the
control.
You have used the prefix “txt” to show that this control
is a text box.
When you draw a controlon a form Visual Basic
automaticallyassigns the controla name. In this case
“Text1”.
The default name is determined by the type of control
and the number of those controlson your form.
PAGE 46 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
The name is used to refer to a control when
programming. It is good practice to change the name to
something meaningful to your application.A useful
convention is to give the control name a prefix which
indicates the type of control.
You will change this text box control‟sName to
txtInput.
Step 4 Check that the Text1 control is selected. It
will appear on your form surrounded by
black handles.
Step 5 If the properties window is not visible, click
the right mouse button and select
Properties from the object menu.
Step 6 Click on the Name property in the
properties window. If the Name property is
not visible, use the scroll bars to display it.
PAGE 47 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Figure 1-4
Step 7 Type txtInput as the new name for the
control.
You have used the prefix “txt” to show that this control
is a text box.
3.1.3 The Text property
You will notice that the text box on your form still
contains the text “Text1”. This text is the current value
of the Text property.
To set the default text to something else, you would
change the Text propertyfor the text box. In this case,
you want it to be blank.
Step 8 Check that the txtInput control on your
form is selected and click on the Text
property in the properties window.
PAGE 48 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Figure 1-5
Step 9 Highlight Text1 and press the delete key
on your keyboard to delete the text.
The text should have now disappeared from the control
on your form.
3.2 Adding a label
In the previous chapteryou used a label to display the
text that the user entered. More commonly labels are
used as headings to prompt the user.
You will now create a label to explain the text box.
Step 10 Double click on the label tool in the tool
box.
Step 11 Click and drag to create a label in the
form, positioned above the text box.
PAGE 49 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 12 Change the Caption property of the label
to Enter your text here:.
Step 13 Use the Font property and change the
font size to 14.
Figure 2-1
Having added the text box and label to your form you
can run it to see what it looks like.
Step 14 Click on the play button on the tool bar.
Step 15 Type something in the text box.
Step 16 Click on the stop button on the tool bar to
return to Visual Basic
3.3 Check boxes
The next step is to add check boxes to the form. These
will allow the user to change the format of the text in
the text box to bold or italics.
Check boxes are used to represent on/off values. They
can either be checked or unchecked. When they are
checked a tick will appear in the box.
PAGE 50 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 17 Double click on the check box control in
the tool box.
Figure 3-1
Step 18 Move the check box to an appropriate
location.
Step 19 Repeat the last two steps to add a second
check box below the first.
PAGE 51 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Your form will appear like this.
Figure 3-2
3.3.1 Changing the properties of the check boxes
As with the text box you should now change the names
of the check boxes to something more meaningful.
Step 20 Click on the Check1 check box in your
form.
Step 21 Change the Name property to chkBold.
You will also need to change the caption of the check
box. Remember that this is what appears on the form.
Step 22 Change the Caption property to Bold.
Step 23Repeat the steps above to change the
name of the second check box to chkItalic
and the caption to Italic.
PAGE 52 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Figure 3-3
3.3.2 Setting the Value property
The Value propertyof a check box determines whether
the check box is checked, unchecked or greyed.
Step 24 Click on the Bold check box on your form
to select it.
Step 25 Click on the Value property in the
properties window.
Step 26 Use the pull down menu to see the
possible values for this property.
Figure 3-4
PAGE 53 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
For now, leave the value propertyas 0 – Unchecked.
You are going to write code so that if this property has
the value one (i.e. the user has checked the box), the
font will be bold, otherwise it will not be bold.
3.3.3 Writing the code
At this stage the check boxes on the form will not
perform any action. To make the appearanceof the text
in the text box change when the check boxes are
checked and unchecked you need to write code.
Step 27 Double click on the Bold check box on
your form.
The code window will appear.
Step 28Enter the code shown below. Remember
that the Private Sub and End Sub
statements are automatically included by
Visual Basic
Private Sub chkBold_Click ()
If chkBold.Value = 1 Then
txtInput.FontBold = True
PAGE 54 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Else
txtInput.FontBold = False
End If
End Sub
This is shown below.
Figure 3-5
This is an example of the Visual Basic If statement. The
If statement in code allows a choice of actions. The
general structureis:
If expression Then
statements1
Else
statements2
End If
PAGE 55 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
The first line of the If statement in your example tests
the current value of the check box. If the value of the
propertyValue is equal to 1, then the check box is
checked.
If the bold checkbox is checked, set the FontBold
propertyof the txtInput control to True.
Otherwise, if the check box value is not 1 (unchecked
or inactive), set the FontBoldpropertyof the txtInput
control to False.
Run the application to see the result.
Step 29 Click on the play button on the tool bar.
Step 30 Type some text in the text box.
Step 31 Click on the Bold check box to check it.
The text in the text box should be displayed as bold.
Step 32 Click on the Bold check box to uncheck it.
The text in the text box should be displayed as normal.
Step 33 Click on the stop button on the tool bar to
return to Visual Basic.
PAGE 56 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
3.3.4 The Italic check box code
The code for the Italic check box is similar to the Bold
check box but changes the FontItalic property.
Step 34 Double click on the Italic check box.
The code window will appear.
Step 35Enter the code shown below. Remember
that the Private Sub and End Sub
statements are automatically included by
Visual Basic.
Private Sub chkItalic_Click ()
If chkItalic.Value= 1 Then
txtInput.FontItalic = True
Else
txtInput.FontItalic = False
End If
End Sub
Run the application to see the result.
PAGE 57 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 36 Click on the play button on the tool bar.
Step 37 Type some text in the text box.
Step 38 Click on the Italic check box to check it.
The text in the text box should be displayed as italic.
Step 39 Click on the stop button on the tool bar to
return to Visual Basic.
3.4 Option buttons
Check boxes are used to represent true/false, on/off or
yes/no values. Option buttonsare used to give a set of
choices. Option buttonsare sometimes called radio
buttons.
When one option button is selected the othersin the set
are automaticallydeselected.
You will now use a set of option buttonson your form
to represent three font type choices for the text box.
Step 40 Double click on the option button control
in the tool box.
PAGE 58 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Figure 4-1
Step 41 Move the option button to an appropriate
location.
Step 42 Repeat the steps above to add two more
option buttons to your form.
Figure 4-2
Step 43 Change the Name and Caption properties
of the option buttons as given below.
Name Caption
optMSSansSerif MS Sans Serif
PAGE 59 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
optTimesNewRoman Times New
Roman
optArial Arial
You may need to resize the option controlsso that the
caption fits on one line.
Figure 4-3
3.4.1 Setting the Value property
You will set the initial value of the MS Sans Serif
option buttonto True because this is the default font
type for the text box.
To do this you set the Value property. The possible
values are True or False. True indicates that the option
buttonis selected.
Step 44 Click on the MS Sans Serif option button
on your form to select it.
Step 45 Click on the Value property in the
properties window.
PAGE 60 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 46 Use the pull down menu in the editing
panel to change the value to Trueas
shown below.
Figure 4-4
3.4.2 The code for the optionbuttons
The code for the option buttonssets the FontName
propertyfor the text box accordingly.
Step 47 Double click on the MS Sans Serif option
button in your form.
The code window will appear.
Step 48Enter the code shown below. Remember
that the Private Sub and End Sub
statements are automatically included by
Visual Basic.
Private Sub optMSSansSerif_Click ()
PAGE 61 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
txtInput.FontName = "MS Sans Serif"
End Sub
Step 49Repeat this process to enter the code
shown below for the Times New Roman
button.
Private Sub optTimesNewRoman_Click ()
txtInput.FontName = "Times New Roman"
End Sub
Step 50 Repeat the process again to enter the
code shown below for the Arial button.
Private Sub optArial_Click ()
txtInput.FontName = "Arial"
End Sub
Run the application to see the result.
Step 51 Type some text in the text box.
Step 52 Click on the Times New Roman option
button to select it.
PAGE 62 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
The text in the text box will be displayed in Times New
Roman.
Step 53 Try the Arial and MS Sans Serif buttons.
Step 54 Click on the Stop button on the tool bar to
return to Visual Basic.
3.4.3 Changing the colourof the text box
The final task is to change the background colourof the
text box when the font changes. Visual Basic provides
names for colourvalues, for example, vbRed for red,
vbGreen for green and vbBlue for blue.
You need to add a line of code, which assigns a value to
the BackColor propertyof the text box, to the option
buttoncode.
Step 55 Double click on the MS Sans Serif option
button and insert the line shown:
Private Sub optMSSansSerif_Click ()
txtInput.FontName = "MS Sans Serif"
txtInput.BackColor = vbGreen
End Sub
PAGE 63 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 56Add code to the Times New Roman button
to change the text box colour to Red and
code to the Arial button to change the text
box colour to Blue.
Step 57 Try out your application.
3.5 Finishing the form
To tidy up the form you will add a title to it and change
the size.
Step 58 Click anywhere on the form to select it. Be
careful not to click on a control.
Step 59 Change the Caption property of the form
to read Text Formats.
Step 60 Resize the form to a more appropriate
size.
The completed form will appearas shown below.
Figure 5-1
PAGE 64 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 61 Run the application to test the changes.
Step 62 Save the project. Remember to save both
the project file and the form.
3.6 Programming Notes
3. 6.1 If statements
Use the If statement when you want to carry out
different actions dependingon current values in your
application.
The syntax of the If statement can be any of the
following:
a) If condition Thenstatements1
b) If condition Thenstatements1 Else statements2
c) If condition Then
statements1
End If
d) If condition Then
statements1
Else
statements2
PAGE 65 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
End If
e) If condition Then
statements1
Elseif
statements2
Else
statements3
End If
3. 6.2 Built-In Color Constants
Visual Basic recognizes the following constant names
for colors. You can use these names in your code in
place of the actual values.
Constant Value Description
vbBlack 0x0 Black
vbRed 0xFF Red
vbGreen 0xFF00 Green
vbYellow 0xFFFF Yellow
vbBlue 0xFF0000 Blue
vbMagenta 0xFF00FF Magenta
vbCyan 0xFFFF00 Cyan
vbWhite 0xFFFFFF White
PAGE 66 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Exercises
Exercise 1
1.1 Your boss has commissioned you to design the
interface for a name tag recording system.
1.2 Open a new project by choosing New Project from
the File menu and design the form shown below.
Figure 7-1
1.3 Change the name of the text box to txtName.
1.4 Write the code to make the bold and italic check
boxes work.
Exercise 2
2.1 Change the Caption of the form to Name Tag
Designer and the name of the form to frmTag.
2.2 Add Times New Roman and Arial option buttons
to change the font of the text box.
Figure 7-2
PAGE 67 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
2.3 Write the code to make the option buttonswork.
Exercise 3
3.1 Add a command buttonto the form so when it is
clicked, the background colourof the text box changes
to green if it was blue or changes to blue otherwise.
Figure 7-3
(Hint: If txtName.BackColor= vbBlue Then…)
3.2 Add a command buttonto the form so that when it
is clicked, an input dialog box asks the user for a font
size for the tag.
Figure 7-4
(Hint: txtName.FontSize= InputBox(“Entera new font
size e.g. 14”))
PAGE 68 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Chapter 4. List boxes, combo boxes and forms
4.1 Projects with more than one form
In this chapteryou will create an application in which
one form opens a second form. The opening form,
which is called the main form, will include a command
buttonwhich activates the second form. The second
form allows the user to maintain a list of clients. When
your user is finished with the second form they will
return to the main form to terminate the application.
Step 1 Design a main form to look like the
following.
Figure 1-1
Step 2 Change the name of the form to
frmWidgets.
Step 3 Change the names of the command
buttons to cmdClientList (caption Client
List) and cmdQuit (caption Quit).
Step 4 Add the following code to the Quit button
to terminate the application.
PAGE 69 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Private Sub cmdQuit_Click()
End
End Sub
The next stage will add a second form to the
application.At this stage, just an outlinedesign will be
sufficient.
Step 5 Click on the add form button on the
toolbar.
Figure 1-2
Click Form from the display list of available forms and
then click Open.
Figure 1-3
PAGE 70 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
The form will be displayed in a separate window. Select
the new window so you can see the form.
Step 6 Design the form to look like the following.
Figure 1-4
Step 7 Change the name of the form to
frmClientList and the name of the Close
button to cmdClose.
Step 8 Add this code to the command button.
Private Sub cmdClose_Click()
Unload Me
End Sub
Visual Basic provides the keyword Me as a shortcut to
refer to the currentlyactive form.
Step 9 Select View Object for frmWidgets from
the Project window.
PAGE 71 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Figure 1-5
When your user clicks on the Client List button,the
second form should become active. The process of
making a form active is a two stage process. It must
first be loaded into memory using the Visual basic
subprogram Load. Then it must be made visible using
the method Show.
Step 10 Add the following code for the Client List
command button.
Private Sub cmdClientList_Click()
Load frmClientList
frmClientList.Show
End Sub
Step 11Run your application to check that it works
correctly. Use the Quit button to stop the
application running.
Step 12Save the project. Note that there are now
three files to be saved. Use the Visual
Basic default of the form names as the file
PAGE 72 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
names for the forms. Save the project file
as Widgets.vbp
The Visual Basic default will mean that the filenames
for your form files are frmClientList.frm and
frmWidgets.frm.
4.2 List boxes
You will now complete the design of the second form
4.2.1 What is a list box?
A list box is a list, which appears on the screen, from
which the user can choose items. In this chapteryou
will create the text box and list box shown below and
program the add, delete, clear buttons.
Figure 2-1
The add button will add the contentsof the text box to
the list. The delete buttonwill remove the item in the
list which the user has selected. The clear buttonwill
deleteall the items from the list.
PAGE 73 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
4.2.2 Creating a list box
Creating a list box is just like creating any other Visual
Basic control.
Step 13 Select View Form for frmClientList from
the Project window.
Step 14 Double click on the list box control in the
tool box.
Figure 2-2
Step 15 Move and size the list box.
Figure 2-3
Adding the other controls on the form
PAGE 74 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 16Add a label, text box and three more
command buttons to the form, as shown
below.
Figure 2-4
Step 17Name each of the new controls by
selecting the control on your form and
changing the name property in the
properties window.
Control Name
Text1 txtInput
List1 lstClientList
Command1 cmdAdd
Command2 cmdDelete
Command3 cmdClear
The controlsalso require the captionsto be changed.
Step 18 Change the Caption properties as shown
below.
PAGE 75 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Control Name
Label1 Enter the name to add:
cmdAdd Add
cmdDelete Delete
cmdClear Clear
Step 19 Clear the Text property of the txtInput
control.
Check the appearanceof the form at this stage.
Step 20Click on the play button on the tool bar.
Click on the Client List button. The second
form should then appear as shown below.
Figure 2-5
Step 21 Click on the Close and Quit buttons once
you have checked your form.
4.2.3 The add button
PAGE 76 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
The Add buttonwill add what the user has entered in
the text box into the list.
Step 22 Double click on the Add button on your
form to display the code window.
Step 23Enter the code shown below. Remember
that the Private Sub and End Sub
statements will have been entered for you
by Visual Basic.
Private Sub cmdAdd_Click ()
lstClientList.AddItem txtInput.Text
txtInput.Text = ""
txtInput.SetFocus
End Sub
The first line of the code adds an item to the list box
(remember the name of the list box is lstClientList). The
item which is added is the current value of the Text
propertyof the txtInput control. This will be what the
user has entered into the text box.
The next line resets the Text propertyof the text box to
a blankstring, ready for the next entry by the user.
The last line repositionsthe cursor inside the text box so
the user can begin typing the next entry.
PAGE 77 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 24 Run the application to test the Add button
code.
Step 25 Type your name as the first client to add
to the list.
Step 26 Click on the Add button.
Your name should appear in the list box and the text
box should appearempty. The cursor will be positioned
in the text box ready for the next entry.
Figure 2-6
Step 27Use the Close and Quit buttons on your
forms to stop the application when you
have finished.
4.2.4 The Delete button
The Delete button will delete the item that the user has
selected in the list box from the list.
PAGE 78 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 28 Double click on the Delete button on your
form to display the code window.
Step 29Enter the code shown below. Remember
that the Private Sub and End Sub
statements will have been entered for you
by Visual Basic.
Private Sub cmdDelete_Click ()
If lstClientList.ListIndex >= 0 Then
lstClientList.RemoveItem
lstClientList.ListIndex
Else
Beep
End If
End Sub
The code uses an If statement to test whether the user
has selected an item in the list.
The ListIndex propertyfor the lstClientList control
holdsa number indicating which item in the list has
been selected by the user.
PAGE 79 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
If ListIndex is zero then the first item in the list is
selected. If ListIndex is equal to one then the next item
in the list is selected and so on.
If ListIndex is less than zero it means that nothingis
selected.
If something is selected the next line of the code will
remove it from the list. Visual Basic will remove the
item at list position lstClientList.ListIndex.
If nothing is selected a Beep is sounded.
You should run the applicationto test the Delete button
code.
Step 30 Click on the play button on the tool bar
and click the Client List button.
Firstly, test to see what happens if you click the Delete
buttonwhen the list is empty.
Step 31 Click the Delete button.
A beep should sound to indicate that nothing in the list
is selected for deletion.The action should not result in
an error message.
Next, test that the Delete buttoncorrectlydeletes list
items.
Step 32Add several items to the list by entering
them in the text box and clicking on the
Add button.
PAGE 80 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Figure 2-7
Step 33 Click on one of the items to select it.
The item you select will become highlighted.
Step 34 Click on the Delete button.
The item will be removed from the list.
Step 35 Try selecting and deleting the other list
items.
Step 36 Stop the application running when you
have finished testing.
4.2.5 The Clear button
The Clear button will deleteall of the items in the list.
Step 37 Double click on the Clear button on your
form to display the code window.
PAGE 81 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 38 Enter the code shown below. Remember
that the Private Sub and End Sub
statements will have been entered for you
by Visual Basic.
Private Sub cmdClear_Click ()
lstClientList.Clear
End Sub
The code uses the Clear method to deleteall the items
from the list.
Run the application to test the Clear button code.
Step 39 Click on the play button on the tool bar.
Click on Client List.
Step 40 Add several items to the list using the Add
button.
Step 41 Click on the Clear button.
All of the items in the list will be deleted.
Step 42 Stop the application running.
Step 43 Save the project.
4.2.6 Selecting Items from a list
PAGE 82 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
The following example demonstrateshow to use a list
box to allow your user to make a selection from the list.
The items in this list box example are added at design
time.
Step 44 Start a new project.
Step 45 Add a list box to the form. Change its
name to lstFruits.
Step 46 Select the List property for the list box and
click on the drop down arrow.
Step 47 Type apples then press Ctrl Enter
Step 48 Type the other fruit names shown
pressing Ctrl Enter after each one.
Figure 2-8
Step 49If you cannot see the full list of fruit in the
list box on your form, make the list box
larger.
PAGE 83 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 50Add a label with the caption shown and a
text box to your form as in
figure 2-9.
Step 51 Change the name of the text box to
txtSelection.
Figure 2-9
The List propertyreturns the contentsof the list for the
specified index.
Step 52 Add the following code to the click event
for the list box.
Private Sub lstFruits_Click()
txtSelection.Text =
lstFruits.List(lstFruits.ListIndex)
End Sub
PAGE 84 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 53Run the application and test that clicking
on an item in the list box will cause it to be
displayed in the text box.
Step 54 Stop the application.
4.3 Combo Boxes
The combo box control is a combination of a list box
and a text box. Items are stored in a list and viewed
using a drop down menu.
The contentsof the Text property of the combo box are
displayed at the top of the combo box.
Step 55 Start a new project.
Step 56 Add a combo box to the form.
Figure 3-1
PAGE 85 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 57 Using the List property, add the items
Potatoes, Carrots, Peas and Beans to
the list.
Step 58Run the application. Click on the drop
down arrow for the combo box and select
one of the items.
The Text propertyof the combo box is set to the
selected item and it will be displayed.
Step 59 Stop the application.
Combo boxes also allow your user to type in a value to
set the text propertywhile the program is running. This
can be useful if the user needs a value that is not in the
list.
4.4 Programming Notes
4.4.1 Subprograms
A subprogram is a block of code which is activated by
an event or called from anotherblock of code.
The syntax to call a subprogram is:
SubprogramName [parameters]
or
Call SubprogramName[(Parameters)]
PAGE 86 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Examples of subprograms provided by Visual Basic are
Load, Unload and End.
4.4.2 Methods
A method is a subprogram which acts on an object.
The syntax to use a method is:
object.method[parameters]
Examples of methods are:
 Show for a form e.g. frmMain.Show
 AddItem for a list or combo box e.g.
lstFruits.AddItem “Pineapple”
 SetFocus for any object e.g.
cmdQuit.SetFocus
4.4.3 Displaying Form
Each form must first be loaded into memory and then
made visible.
The main form of a project is automaticallyloaded into
memory and made visible when the project starts
running.
For otherforms, statements to Load them and Show
them must be executed. For example:
Load Form2
PAGE 87 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Form2.Show
If you have forms which you want to open and close
frequently, you can avoid the overhead of loading them
into memory each time by loadingthem once then using
the Show and Hide methods to make them available
when required.
Each form has a load event and you can put code here
to initialise values. The load event for the main form is
a good placeto load otherforms.
Private Sub Form1_Load()
Load Form2
Load Form3
End Sub
When you want Form2 to become visible, use the
statement:
Form2.Show
When you want Form2 to be invisible, use the
statement:
Form2.Hide
4.4.4 Summary
PAGE 88 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Load is a Sub (subprogram) which loads the specified
form into memory. Note that it doesn‟t necessarily
make the form visible. (Load is also an event for a
Form).
Show is a method which loads the specified form into
memory if it isn‟t already loaded and makes it visible.
Unload is a Sub which unloadsthe specified form from
memory.
Hide is a method which makes a form invisible but
doesn‟t unload it from memory.
Me is a convenient way of referring to the current form.
End is a subprogram which terminates the currently
running project and unloadsall its forms.
Exercises
Exercise 1
1.1 Open a new project by choosing New Project from
the File menu.
1.2 Design the form shown below. Change the Name
propertyto frmDemo. Add code to the QUIT
buttonto stop the applicationrunning.
PAGE 89 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Figure 5-1
1.3 Click on the Add Form buttonand then on the
existing tab to add the form frmClientList, which
you saved earlier in the chapter, to this project.
1.4 Add code to frmDemo to access frmClientList
when the List Box buttonis clicked.
Exercise 2
2.1 Choose Add Form from the Project menu or click
the Add Form button on the toolbarto add a new
form to your project. Change the name propertyto
frmComboDemo.
2.2 Design the form to look like the following. Set the
Style propertydifferently for each combo box.
2.3 Add the items: eggs, butterand ham to the lists for
each combo box.
PAGE 90 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Figure 5-2
2.4 Add code to the Combo Box buttonin frmDemo to
activate frmComboDemo.
2.5 Save the project as Ch4Demo.
PAGE 91 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Chapter 5. Multiple Document Interface (MDI)
Applications, Toolbars, Using the Clipboard, Popup
Menus
5.1 Multiple Document Interface Applications
WordPad and Notepad are examples of applications
using the single document interface (SDI) style.
Applicationssuch as Word and Excel that allow you to
display multipledocuments at the same time are known
as MDI (MultipleDocument Interface) applications.
In an MDI application,the main form is called the MDI
form, and the document forms are called MDI child
forms. You usually design only one child form for an
MDI application. This form is then copied for each
document that is opened.
In this chapter, you will create an MDI application
5.2 Building the Application
5.2.1 Creating the MDI form
Step 1 Start a new project and remove the
existing Form1 (select Remove Form1
from the Project menu).
Step 2 Select Add MDI Form, New, from the
Project menu. Click on MDI Form. Click
Open.
PAGE 92 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Figure 2-1
Step 3 Change the Name property for this form
to MDIEditor.
5.2.2 Adding the child form
Step 4 Select Add Form, Existing, from the
Project menu and add the form you
developed for the FileViewer application
(frmFileViewer.frm).
Step 5 Select the Fileviewer form. Change the
MDIChild property to True.
Step 6 Change the Name property to frmDoc.
Save the form as frmDoc.frm. Save the
MDI form as mdiEditor.frm and the
project as MDI.vbp
You will write code to create a new instance of this
form each time a new document is opened.
PAGE 93 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
5.2.3 Resizing the rich textbox
The child forms in an MDI applicationcan be resized
independently. The rich textboxcontrol should grow or
shrink with the form. The resize event for the form can
be used to achieve this.
Step 7 Add the following code to the Resize
event for the frmDoc form
Private Sub Form_Resize()
rtbDisplay.Move0,0,Me.Width – 100,
Me.Height - 300
End Sub
5.2.4 Setting up the menus
You have a choice of how you set up menus for an MDI
project:
 Attach all the menu options to the MDI form
and disable the options not available until
there is a child form open
 Attach menu options to the child form and use
a different menu on the MDI form – the menu
on the MDI form will automatically be replaced
by the menu on the child form as soon as a
child form is opened
PAGE 94 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
The child form that you have just added to the project,
already has a menu attached so the second option is
preferable for this project.
Step 8 Select code for the frmDoc form and
amend the mnuNew and mnuOpenFile
options as shown
A new instance of a document form is created by
declaring a variable as type New frmDoc.
Private Sub mnuNew_Click()
Dim newDoc As New frmDoc
newDoc.Show
newDoc.Caption = "Untitled"
newDoc.rtbDisplay.Text = ""
End Sub
Private Sub mnuOpenFile_Click()
Dim strFileName As String
Dim newDoc As New frmDoc
newDoc.Show
newDoc.cdlViewer.Filter=
"RTF|*.rtf|Text|*.txt|All|*.*"
PAGE 95 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
newDoc.cdlViewer.ShowOpen
On Error GoTo OpenProblems
strFileName = newDoc.cdlViewer.filename
newDoc.rtbDisplay.LoadFile strFileName
newDoc.Caption = strFileName
Exit Sub
OpenProblems:
MsgBox "Can’t open the file, select
another.", vbCritical
Exit Sub
End Sub
The main MDI form can be given a simple menu to
enablethe first document to be opened.
Step 9 Select the MDIEditor form and use the
Menu Editor to set up a menu which looks
like the following
Figure 2-2
Caption Name
PAGE 96 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
&File mnuFile
&New mnuNew
&Open mnuOpen
- mnuSep1
E&xit mnuExit
Step 10Add a common dialog control to form
MDIEditor. Change its Name property to
cdlViewer.
Step 11Copy the code from the frmDoc form New
and Open options to the New and Open
options for the MDIEditor form.
Step 12 Add code for the mnuExit option to the
MDIEditor form
Private Sub mnuExit_Click()
End
End Sub
Step 13Save the project and files. Run the
application to test the main menu options.
(You will need to set Start Up Object to
MDIEditor).
PAGE 97 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Figure 2-3
5.3 Closing MDI application forms
In MDI applications, there is usually an option for the
user to close individual child forms. The close operation
might be cancelledif the information in the child form
has not been saved since it was last changed.
You will also need to ensure that the child forms are
properlyclosed if the user presses the close buttonon
the main MDI form.
5.3.1 Deciding if changes to data in a child form
have been saved
The simplest way to do this is to use a global boolean
variable which is set to true when text in the rich
textbox is changed, and set to false when the data is
saved.
PAGE 98 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 14Save the form mdiEditor.frm as
mdiEditor1.frm. Save the form
frmDoc.frm as frmDoc1.frm. Save the
project as MDI1.vbp. Use the menu editor
to add a Close option (&Close and
mnuClose) to the File menu on the
frmDoc1 form.
Step 15Add the following declaration to the
General Declarations section of the
frmDoc1 form (below the Option Explicit
statement).
Dim Changed As Boolean
Step 16 Add this code to the Change event for the
rtbDisplay (the rich textbox).
Private Sub rtbDisplay_Change()
Changed= True
End Sub
Step 17 Amend the code for the mnuSaveFile
option for frmDoc1 form as shown
Private Sub mnuSave_Click()
PAGE 99 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Dim strFileName As String
cdlViewer.Filter = "RTF|*.rtf|Text|*.txt"
cdlViewer.ShowSave
On Error GoTo SaveProblems
strFileName = cdlViewer.filename
If cdlViewer.FilterIndex = 1 Then
cdlViewer.DefaultExt = “RTF”
rtbDisplay.SaveFile strFileName, rtfRTF
Else
cdlViewer.DefaultExt = “txt”
rtbDisplay.SaveFile strFileName, rtfText
End If
Me.Caption = strFileName
Changed= False
Exit Sub
SaveProblems:
MsgBox "Can’t save the file, try again.",
vbCritical
Exit Sub
End Sub
PAGE 100 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 18 Add code to the mnuClose option of the
frmDOC1 form.
Private Sub mnuClose_Click()
Unload Me
End Sub
The Unload event will cause the child form to be
unloaded but before it does this, it triggers the
QueryUnload event for the form. This event allows you
to cancel the unload if you wish.
Step 19 Add the following code to the
QueryUnload event for the frmDoc1 form
(select Form, QueryUnload in the code
window)..
Private Sub Form_QueryUnload (Cancel As
Integer, _
UnloadModeAs Integer)
Dim Response As Long
If Changed Then
PAGE 101 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Response = MsgBox("Are you sure you
want to close_
without saving?", vbYesNoCancel,
Me.Caption)
Select Case Response
Case vbNo: Cancel = True
Case vbCancel: Cancel = True
End Select
End If
End Sub
This QueryUnload code will also be executed if the user
clicks the Close button on a child form.
5.3.2 Closing the application
The Exit option on the File menu should also close
down the application properly. Using an Unload
statement for the main MDI form will trigger the
QueryUnload events for any open child forms and allow
changes to be saved before the application terminates.
Step 20 Amend the code for the mnuExit option on
the frmDoc1 as shown
PAGE 102 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Private Sub mnuExit_Click()
Unload MDIEditor
End
End Sub
An Unload event for the main form is triggered if the
Close button on the main form is clicked so you don‟t
need to do anything further to deal with this event.
Save the project and files and test the program.
Figure 2-4
5.4 Using the Clipboard
Adding Cut, Copy and Paste optionsto the Edit menu
for this application will show you how to manipulate
the clipboard from a Visual Basic application.
Step 21Save mdiEditor1.frm as mdiEditor2.frm.
Save frmDoc1.frm as frmDoc2.frm. Save
the project as MDI2.vbp. Use the Menu
Editor to add Cut, Copy, Paste and Delete
PAGE 103 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
sub-options to a new Edit menu on the
frmDoc2 form.
Caption Name
&Edit mnuEdit
C&ut mnuCut
C&opy mnuCopy
&Paste mnuPaste
&Delete mnuDelete
Step 22 Add the following code for these menu
options.
Public Sub mnuCopy_Click()
Clipboard.Clear
Clipboard.SetText rtbDisplay.SelText,
vbCFRTF
End Sub
Public Sub mnuCut_Click()
Clipboard.Clear
PAGE 104 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Clipboard.SetText rtbDisplay.SelText,
vbCFRTF
rtbDisplay.SelText = ""
End Sub
Public Sub mnuPaste_Click()
rtbDisplay.SelText =
Clipboard.GetText(vbCFRTF)
End Sub
Private Sub mnuDelete_Click()
rtbDisplay.SelText = ""
End Sub
Step 23 Save the project and files. Test the edit
menu options.
5.5 Adding a toolbar
The Microsoft Windows Common Controls 5.0
component provides the controlssuch as ToolBar,
StatusBar, ProgressBar, TreeView, ListView and
PAGE 105 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
TabStrip which are part of a typical Windows 95
application interface.
The collectionalso includesthe ImageList control
which is used as a central repositoryto supply other
controlswith images. An ImageList controlcontains a
collectionof ListImage objects, each of which can be
referred to by its index or key.
This section shows you how to use the Toolbarand
ImageList controlto add an edit toolbarcontainingCut,
Copy and Paste buttonsto your application.
Save mdiEditor2.frmas mdiEditor3.frm. Save
frmDoc2.frmas frmDoc3.frmand save the project as
MDI3.vbp.
Step 24Select Microsoft Windows Common
Controls 6.0 from Components on the
Project menu.
5.5.1 Using an ImageList control to store button
pictures for the toolbar
The imagelist controlwill be used as a repository for
the images on the toolbarbuttons.
Step 25Add an ImageList control to form
MDIEditor3. (The control is not visible
when the application is running).
PAGE 106 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 26 Change the Name property for the
ImageList control to imlToolbar.
Step 27Right click on the ImageList control and
select Properties to bring up the property
pages.
Figure 5-1
Step 28On the General tab, select Custom size.
(This will size the images to the size of the
first image added).
Step 29 Select the Images tab and select Insert
Picture.
The folder Program FilesMicrosoft Visual
StudioCommonGraphicsBitmapsTlbr_Win95
contains the images you will need to create typical
Windows toolbars.
Step 30 Add an image for the Cut button and set
the Key property to Cut.
PAGE 107 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 31Repeat the last step for Copy and Paste.
Then click OK to close the Property Pages
dialog box.
Figure 5-2
Note that the cut picturehas an index value of 1, the
copy image an index value of 2, and the paste image an
index value of 3. The images in the imagelist control
can be referenced either by using the index or the key
values.
5. 5.2 Setting up the toolbar
Step 32Add a Toolbar control to the MDIEditor3
form. Change the Name property to
tlbEditor.
Step 33Right click on the toolbar control and
select Properties to bring up the property
pages.
Step 34Set the ImageList property to imlToolbar.
PAGE 108 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Figure 5-3
Step 35 Select the Buttons tab and select Insert
Button.
Step 36Set the Key, ToolTip Text and Image
values as shown.
Figure 5-4
Button Index ToolTipText
Key Image
Cut 1 Cut text Cut 1
PAGE 109 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Copy 2 Copy text Copy
2
Paste 3 Paste text Paste
3
Step 37 Repeat the last step for the Copy and Paste
buttons.
Your form should now look like
Figure 5-5
5. 5.3 Adding the code
The code for the Cut, Copy and Paste optionshas
already been added to the menu optionson the frmDoc
form. This code can be accessed from the MDIEditor
form if it is made Public rather than Private. The
ActiveForm propertyis used to refer to the currently
active child form.
Step 38View the code for form frmDoc3 and
change the Private declaration to Public
for the Cut, Copy and Paste menu options.
PAGE 110 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Public Sub mnuCut_Click()
Clipboard.Clear
Clipboard.SetText rtbDisplay.SelText,
vbCFRTF
' Delete selected text.
rtbDisplay.SelText = ""
End Sub
Step 39 View the code for form MDIEditor3 and
add the following event code
Private Sub tlbEditor_ButtonClick(ByVal Button As
ComctlLib.Button)
On Error GoTo NoActiveForm
Select Case Button.Key
Case "Cut":
Me.ActiveForm.mnuCut_Click
Case "Copy":
Me.ActiveForm.mnuCopy_Click
Case "Paste":
Me.ActiveForm.mnuPaste_Click
PAGE 111 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
End Select
Exit Sub
NoActiveForm:
Exit Sub
End Sub
The error code is added to deal with the buttonsbeing
clicked when there is no child form open.
5. 5.4 Adding an option to view the toolbar
Step 40Using the Menu Editor, add an Options
menu to the MDIEditor3 form menubar.
Add a submenu to this called toolbar.
Caption Name
&Options mnuOption
&Toolbar mnuToolbar
PAGE 112 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Figure 5-6
Step 41 Select the checked option for the toolbar
menu item. Close the menu editor.
Step 42 Add code to the Toolbar menu option.
Private Sub mnuToolbar_Click()
mnuToolbar.Checked = Not
mnuToolbar.Checked
tlbEditor.Visible = Not tlbEditor.Visible
End Sub
5.6 Popup Menus
Windows applications usuallyprovide a popup menu
(or context menu) when you click the right mouse
button. A popup menu is designed using the Menu
PAGE 113 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Editor. Setting the Visible propertyto false ensures that
the popup menu is not displayed on the menu bar.
Invoking the PopupMenu method for the form when the
right mouse button is clicked displays the menu as a
popup menu.
This example displays a pop-upmenu at the cursor
location when the user clicks the right mouse button
over the rich textbox displaying a document.
Step 43 Add a menu to the frmDoc3 form that
contains the options shown
Figure 6-1
CaptionName
&Popup mnuPopup
&Cut mnuPopupCut
C&opy mnuPopupCopy
P&aste mnuPopupPaste
C&hange Font mnuPopupChangeFont
PAGE 114 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 44 Make sure that the Visible box for the menu
header is not checked.
Step 45 Add the following code to the popup menu
options.
Private Sub mnuPopupCopy_Click()
mnuCopy_Click
End Sub
Private Sub mnuPopupCut_Click()
mnuCut_Click
End Sub
Private Sub mnuPopupFont_Click()
mnuChangeFont_Click
End Sub
Private Sub mnuPopupPaste_Click()
mnuPaste_Click
End Sub
PAGE 115 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Step 46Add the following code to the MouseUp
event for rtbDisplay (the rich textbox) on
form frmDoc3.
Private Sub rtbDisplay_MouseUp(ButtonAs
Integer, Shift As Integer, x As Single, y As Single)
If Button= 2 Then
Me.PopupMenu mnuPopup
End If
End Sub
Save the project and files and test the application. You
should now get a context menu if you click the right
mouse button anywhere in a document.
Exercises
Exercise 1
Add an undo option to the Edit menu which undoesthe
last delete. (Hint: declare a public string variable and
copy the selection to this variable when Delete is
executed).
Exercise 2
Add a Windows menu to your application that includes
the options to Tile or Cascade the currentlyopen child
PAGE 116 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
windows. (Hint: use the Arrange method for
MDIEditor).
PAGE 117 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
References
VB Reference Books
A Visual Basic 6 Programmer's Toolkit
by Smith, Eric and Marquis, Hank; Apress;
3/2000; ISBN 1893115062. List $39.95
softcover.
Visual Basic 6 Programmer's Reference
by Dan Rahmel; Osborne, ISBN: 0-07-
882576-8, $16.99 US.
I. Overview of VB, VBA, and VB Script
II. Programming System Information and
How-To-Examples
III. Language Reference
IV. Object-Model Diagrams
VB6 Reference Library
Visual Basic 6 Programmer's Guide
PAGE 118 Programming with Visual Basic 6.0 by J. T
 University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
Visual Basic 6 Unleashed,
Professional Reference Edition
Author: Rob Thayer, et al. Retail Price:
$49.99 US; Publisher: SAMS; ISBN:
0672315084; Publication Date: June 22,
1999; Pages: 1296

Weitere ähnliche Inhalte

Ähnlich wie Programming with visual_basic_new

Project final report
Project final reportProject final report
Project final reportALIN BABU
 
Work Measurement Application - Ghent Internship Report - Adel Belasker
Work Measurement Application - Ghent Internship Report - Adel BelaskerWork Measurement Application - Ghent Internship Report - Adel Belasker
Work Measurement Application - Ghent Internship Report - Adel BelaskerAdel Belasker
 
(Manual) auto cad 2000 visual lisp tutorial (autocad)
(Manual) auto cad 2000 visual lisp tutorial (autocad)(Manual) auto cad 2000 visual lisp tutorial (autocad)
(Manual) auto cad 2000 visual lisp tutorial (autocad)Ketut Swandana
 
A Cloud Decision making Framework
A Cloud Decision making FrameworkA Cloud Decision making Framework
A Cloud Decision making FrameworkAndy Marshall
 
Digital Content Retrieval Final Report
Digital Content Retrieval Final ReportDigital Content Retrieval Final Report
Digital Content Retrieval Final ReportKourosh Sajjadi
 
Conference proceedings 2011 AEGIS International Workshop and Conference
Conference proceedings 2011 AEGIS International Workshop and ConferenceConference proceedings 2011 AEGIS International Workshop and Conference
Conference proceedings 2011 AEGIS International Workshop and ConferenceAEGIS-ACCESSIBLE Projects
 
Chat Application [Full Documentation]
Chat Application [Full Documentation]Chat Application [Full Documentation]
Chat Application [Full Documentation]Rajon
 
Google app engine
Google app engineGoogle app engine
Google app engineSuraj Mehta
 
online examination management system
online examination management systemonline examination management system
online examination management systemPraveen Patel
 
Ensuring Distributed Accountability in the Cloud
Ensuring Distributed Accountability in the CloudEnsuring Distributed Accountability in the Cloud
Ensuring Distributed Accountability in the CloudSuraj Mehta
 
Project Report (1) (1).docx
Project Report (1) (1).docxProject Report (1) (1).docx
Project Report (1) (1).docxDarshanaThorat2
 
iPad as a pedagogical device
iPad as a pedagogical deviceiPad as a pedagogical device
iPad as a pedagogical deviceFrode Kyrkjebø
 
Bike sharing android application
Bike sharing android applicationBike sharing android application
Bike sharing android applicationSuraj Sawant
 
Microlearning Programs Calendar 2020
Microlearning Programs Calendar 2020Microlearning Programs Calendar 2020
Microlearning Programs Calendar 2020Manisha Khetarpal
 
Using ADO.NET Entity Framework in Domain Driven Design: A Pattern Approach
Using ADO.NET Entity Framework in Domain Driven Design: A Pattern ApproachUsing ADO.NET Entity Framework in Domain Driven Design: A Pattern Approach
Using ADO.NET Entity Framework in Domain Driven Design: A Pattern ApproachHoan Phuc
 

Ähnlich wie Programming with visual_basic_new (20)

Project final report
Project final reportProject final report
Project final report
 
Work Measurement Application - Ghent Internship Report - Adel Belasker
Work Measurement Application - Ghent Internship Report - Adel BelaskerWork Measurement Application - Ghent Internship Report - Adel Belasker
Work Measurement Application - Ghent Internship Report - Adel Belasker
 
(Manual) auto cad 2000 visual lisp tutorial (autocad)
(Manual) auto cad 2000 visual lisp tutorial (autocad)(Manual) auto cad 2000 visual lisp tutorial (autocad)
(Manual) auto cad 2000 visual lisp tutorial (autocad)
 
document
documentdocument
document
 
A Cloud Decision making Framework
A Cloud Decision making FrameworkA Cloud Decision making Framework
A Cloud Decision making Framework
 
Digital Content Retrieval Final Report
Digital Content Retrieval Final ReportDigital Content Retrieval Final Report
Digital Content Retrieval Final Report
 
Conference proceedings 2011 AEGIS International Workshop and Conference
Conference proceedings 2011 AEGIS International Workshop and ConferenceConference proceedings 2011 AEGIS International Workshop and Conference
Conference proceedings 2011 AEGIS International Workshop and Conference
 
Chat Application [Full Documentation]
Chat Application [Full Documentation]Chat Application [Full Documentation]
Chat Application [Full Documentation]
 
Google app engine
Google app engineGoogle app engine
Google app engine
 
online examination management system
online examination management systemonline examination management system
online examination management system
 
Final Report
Final ReportFinal Report
Final Report
 
Ensuring Distributed Accountability in the Cloud
Ensuring Distributed Accountability in the CloudEnsuring Distributed Accountability in the Cloud
Ensuring Distributed Accountability in the Cloud
 
Project Report (1) (1).docx
Project Report (1) (1).docxProject Report (1) (1).docx
Project Report (1) (1).docx
 
Report-V1.5_with_comments
Report-V1.5_with_commentsReport-V1.5_with_comments
Report-V1.5_with_comments
 
iPad as a pedagogical device
iPad as a pedagogical deviceiPad as a pedagogical device
iPad as a pedagogical device
 
Bike sharing android application
Bike sharing android applicationBike sharing android application
Bike sharing android application
 
Cloud view platform-highlights-web3
Cloud view platform-highlights-web3Cloud view platform-highlights-web3
Cloud view platform-highlights-web3
 
Openobject bi
Openobject biOpenobject bi
Openobject bi
 
Microlearning Programs Calendar 2020
Microlearning Programs Calendar 2020Microlearning Programs Calendar 2020
Microlearning Programs Calendar 2020
 
Using ADO.NET Entity Framework in Domain Driven Design: A Pattern Approach
Using ADO.NET Entity Framework in Domain Driven Design: A Pattern ApproachUsing ADO.NET Entity Framework in Domain Driven Design: A Pattern Approach
Using ADO.NET Entity Framework in Domain Driven Design: A Pattern Approach
 

Mehr von FLYMAN TECHNOLOGY LIMITED

Flyman Technology Limited Introduction Presentation
Flyman Technology Limited Introduction PresentationFlyman Technology Limited Introduction Presentation
Flyman Technology Limited Introduction PresentationFLYMAN TECHNOLOGY LIMITED
 
Flyman Technology Limited (Mzumbe University Tanzania)
Flyman Technology Limited (Mzumbe University Tanzania)Flyman Technology Limited (Mzumbe University Tanzania)
Flyman Technology Limited (Mzumbe University Tanzania)FLYMAN TECHNOLOGY LIMITED
 
introduction to the document object model- Dom chapter5
introduction to the document object model- Dom chapter5introduction to the document object model- Dom chapter5
introduction to the document object model- Dom chapter5FLYMAN TECHNOLOGY LIMITED
 

Mehr von FLYMAN TECHNOLOGY LIMITED (20)

Flyman Technology Limited Introduction Presentation
Flyman Technology Limited Introduction PresentationFlyman Technology Limited Introduction Presentation
Flyman Technology Limited Introduction Presentation
 
Flyman Technology Limited (Mzumbe University Tanzania)
Flyman Technology Limited (Mzumbe University Tanzania)Flyman Technology Limited (Mzumbe University Tanzania)
Flyman Technology Limited (Mzumbe University Tanzania)
 
Flyman Technology Limited Tanzania
Flyman Technology Limited TanzaniaFlyman Technology Limited Tanzania
Flyman Technology Limited Tanzania
 
Saadani national park 2015
Saadani national park 2015Saadani national park 2015
Saadani national park 2015
 
Uuzaji wa mazao ya kuku wa asili
Uuzaji wa mazao ya kuku wa asiliUuzaji wa mazao ya kuku wa asili
Uuzaji wa mazao ya kuku wa asili
 
Entrepreneurship manual BIT
Entrepreneurship manual BITEntrepreneurship manual BIT
Entrepreneurship manual BIT
 
Security chapter6
Security chapter6Security chapter6
Security chapter6
 
introduction to the document object model- Dom chapter5
introduction to the document object model- Dom chapter5introduction to the document object model- Dom chapter5
introduction to the document object model- Dom chapter5
 
Dhtml chapter2
Dhtml chapter2Dhtml chapter2
Dhtml chapter2
 
internet intranet and extranet
internet intranet and extranetinternet intranet and extranet
internet intranet and extranet
 
introduction to web application development
introduction to web application developmentintroduction to web application development
introduction to web application development
 
Marketing management
Marketing managementMarketing management
Marketing management
 
Marketing communications
Marketing communicationsMarketing communications
Marketing communications
 
Marketing analysis
Marketing analysisMarketing analysis
Marketing analysis
 
Market segmentation
Market segmentationMarket segmentation
Market segmentation
 
Consumer markets
Consumer marketsConsumer markets
Consumer markets
 
ENTREPRENEURSHIP MANUAL
ENTREPRENEURSHIP MANUALENTREPRENEURSHIP MANUAL
ENTREPRENEURSHIP MANUAL
 
THINGS NOT TO DO IN A JOB INTERVIEW!
THINGS NOT TO DO IN A JOB INTERVIEW!THINGS NOT TO DO IN A JOB INTERVIEW!
THINGS NOT TO DO IN A JOB INTERVIEW!
 
TAXATION CONCEPTS
TAXATION CONCEPTSTAXATION CONCEPTS
TAXATION CONCEPTS
 
Html tutorial
Html tutorialHtml tutorial
Html tutorial
 

Kürzlich hochgeladen

Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 

Kürzlich hochgeladen (20)

Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

Programming with visual_basic_new

  • 1. UNIVERSITY OF DAR ES SALAAM COMPUTING CENTRE Certificate in Computing and Information Technology PROGRAMMING WITH VISUAL BASIC 6.0 MANUAL
  • 2. PAGE 2 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 © 2005 University Computing Centre ltd University Road P.O Box 35062 Dar es Salaam Tanzania Tel: +255 (022) 2410645 Fax: +255 (022) 2410690 Email: training@udsm.ac.tz Internet: http://www.ucc.co.tz All trademarks acknowledged. E&OE. © University Computing Centre ltd. No part of this document may be copied without written permission from University Computing Centre unless produced under the terms of a courseware site license agreement with University Computing Centre. While all reasonable precautions have been taken in the preparation of this document, including both technical and non-technical proofing. University Computing Centre ltd
  • 3. PAGE 3 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 and all staff assume no responsibility for any errors or omissions. No warranties are made, expressed or implied with regard to these notes. University Computing Centre ltd shall not be responsible for any direct, incidental or consequential damages arising from the use of any material contained in this document. If you find any errors in these training modules, please inform University Computing Centre ltd. Whilst every effort is made to eradicate typing or technical mistakes, we apologise for any errors you may detect. University Computing Centre ltd manuals are updated on a regular basis, so your feedback is both valued by us and will help us to maintain the highest possible standards.
  • 4. PAGE 4 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 COURSE DESCRIPTION............................................. 8 COURSE OBJECTIVES............................................... 8 1. DESIGN PROGRAMS IN A VISUAL LANGUAGE. ........ 9 2. CODE PROGRAMS WITH VISUAL FEATURES IN A WINDOWS ENVIRONMENT. ........................................ 9 3. CREATE PROGRAMS THAT ARE EVENT-DRIVEN. .... 9 4. CREATE VISUAL BASIC CONTROL AND DATA ARRAYS. ................................................................... 9 DELIVERY METHODOLOGY........................................ 9 Chapter 1: Introduction to the Visual Basic Environment, Forms, Controls and Properties ................................................................................. 11 1.1. STARTING VISUAL BASIC................................. 11 1.2. THE VISUAL BASIC ENVIRONMENT................... 11 1.2.1 The tool bar............................................... 12 1.2.2 The tool box.............................................. 12 1.2.3 The form designer window ...................... 13 1.2.4 The properties window............................. 14 1.2.5 The project explorer window.................... 14 1.2.6 The form layout window........................... 15 1.3. CREATING AN APPLICATION ............................. 16 1.4. DESIGNING THE FORM ..................................... 16 1.4.1 Drawing controls....................................... 17 1.4.2 Moving controls ........................................ 18
  • 5. PAGE 5 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 1.4.3 Resizing controls...................................... 18 1.4.4 Resizing the form...................................... 19 1.4.5 Setting properties ..................................... 20 1.4.6 Adding the button controls....................... 22 1.5. RUNNING YOUR APPLICATION .......................... 24 1.6. SAVING YOUR APPLICATION............................. 25 EXERCISES............................................................. 26 Chapter 2. INTRODUCTION TO EVENTS AND PROGRAMMING................................................... 29 2.1 PROGRAMMING IN VISUAL BASIC...................... 29 2.1.1 What are events? ..................................... 29 2.1.2 Opening an existing project..................... 29 You will now add code to the ‘Hello’ project, which you created in the last chapter............... 29 2.2WRITING CODE ................................................. 30 2.3 RUNNING YOUR APPLICATION ........................... 35 2.4 HELP FOR VISUAL BASIC .................................. 35 2.5 INPUT DIALOG BOXES ....................................... 36 2.6 THE BACKCOLOR PROPERTY ........................... 38 EXERCISES............................................................. 39 Chapter 3. Text, Check Boxes and Buttons.... 42 3.1 TEXT BOXES ..................................................... 42 3.1.1 Creating a text box................................... 43 3.1.2 The Name property .................................. 44 3.2 ADDING A LABEL ............................................... 48 3.3 CHECK BOXES .................................................. 49 3.4 OPTION BUTTONS............................................. 57 3.5 FINISHING THE FORM........................................ 63 3.6 PROGRAMMING NOTES .................................... 64
  • 6. PAGE 6 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 EXERCISES............................................................. 66 Chapter 4. List boxes, combo boxes and forms ................................................................................. 68 4.1 PROJECTS WITH MORE THAN ONE FORM........... 68 4.2 LIST BOXES ...................................................... 72 4.2.1......................................... What is a list box? 72 4.2.2 Creating a list box..................................... 73 4.3 COMBO BOXES................................................. 84 4.4 PROGRAMMING NOTES .................................... 85 4.4.2......................................................... Methods 86 EXERCISES............................................................. 88 Chapter 5. Multiple Document Interface (MDI) Applications, Toolbars, Using the Clipboard, Popup Menus........................................................ 91 5.1 MULTIPLE DOCUMENT INTERFACE APPLICATIONS ............................................................................... 91 5.2 BUILDING THE APPLICATION ............................. 91 5.2.1 Creating the MDI form.............................. 91 5.2.2 Adding the child form ............................... 92 5.2.3 Resizing the rich textbox.......................... 93 5.2.4 Setting up the menus............................... 93 5.3 CLOSING MDI APPLICATION FORMS ................. 97 5.3.1 Deciding if changes to data in a child form have been saved................................................ 97 5.3.2 Closing the application........................... 101 5.4 USING THE CLIPBOARD .................................. 102 5.5 ADDING A TOOLBAR........................................ 104
  • 7. PAGE 7 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 5.5.1 Using an ImageList control to store button pictures for the toolbar..................................... 105 5. 5.2 Setting up the toolbar............................ 107 5. 5.3 Adding the code..................................... 109 5. 5.4 Adding an option to view the toolbar.... 111 5.6 POPUP MENUS............................................... 112 EXERCISES........................................................... 115 REFERENCES ....................................................... 117 VB Reference Books ....................................... 117
  • 8. PAGE 8 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Course Description This course guides the student through the process of creating programs in Visual Basic. Provides a task- driven experience to allow students to perform complex programming tasks more easily than would-bepossible without a visual language. Course Objectives The objective of this course is to give studentsa more thorough understandingof Visual Basic so that they may apply for a position as a Visual Basic Programmer. At the end of the course students should be able to
  • 9. PAGE 9 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 1.Design programsin a visual language. 2.Code programs with visual features in a Windows environment. 3.Create programs that are event-driven. 4.Create Visual Basic control and data arrays. Delivery Methodology The course will be delivered in form of lecturers…. in the classroom and in the Computer lab accordingly. Exercise with real life nature will be provided during and at the end of the class. The manual is also designed such that one can follow the course at own time and pace….
  • 10. PAGE 10 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645
  • 11. PAGE 11 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Chapter 1: Introduction to the Visual Basic Environment, Forms, Controls and Properties 1.1. Starting Visual Basic Starting Visual Basic is the same as starting any other Windows application. Step 1 Launch Visual Basic from the Start menu by choosing Programs; Microsoft Visual Studio 6.0; Microsoft Visual Basic 6.0 Depending on the environment settings, you may see the New Project dialog box. Figure 1-1 Step 2 If the New Project box appears, select Standard.EXE, and then click Open. 1.2. The Visual Basic environment The Visual Basic environment is made up of several windows. The initial appearanceof the windows on
  • 12. PAGE 12 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 your screen will depend on the way your environment has been set up. Figure 2-1 1.2.1 The tool bar The Visual Basic tool bar functions like the tool bar in any other Microsoft application. It provides shortcuts for many of the common operatingcommands. It also shows you the dimensions and location of the form currentlybeing designed. Figure 2-2 1.2.2 The tool box The tool box gives you access to the controlsthat you use on a form.
  • 13. PAGE 13 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Figure 2-3 A control is an object such as a button, label or grid. Controlsare used on forms to display outputor get input. Each control appearsas a buttonin the tool box. If the control you are looking for is not in the toolbox, select Componentsfrom the Project menu. If the tool box is not displayed on your screen, or if at any time during the exercises you close it, choose Toolboxfrom the View menu. 1.2.3 The form designer window This window is where you design the forms that make up your user interface.
  • 14. PAGE 14 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Figure 2-4 If the form designer window is not displayed on your screen, or if at any time during the exercises you close it, choose Object from the View menu. 1.2.4 The properties window A form, and each controlon it, has a set of properties which controlits characteristics such as size, position and color. The propertieswindow lists all the properties a control has and their value. Setting the propertyvalue using the propertieswindow when you design your application or changed by assigning a new value in code while your application is runningcan change the default value of a property. If the propertieswindow is not displayed on your screen, or if at any time during the exercises you close it, choose Properties Windowfrom the View menu. 1.2.5 The project explorer window
  • 15. PAGE 15 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 A project is a collectionof the forms and code that make up an application. A file in the project explorer window representseach form in your application. Figure 2-5 A form file containsboth the description of the screen layout for the form and the program code associated with it. If the project explorerwindow is not displayed on your screen, or if at any time during the exercises you close it, choose Project Explorerfrom the View menu. 1.2.6 The form layout window Move the form in the screen in this window to set the position of your form when your applicationis running. Figure 2-6 You may wish to close the form layout window to allow more space for the properties window. To open the window again, select Form Layout Window from the View menu.
  • 16. PAGE 16 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 1.3. Creating an application There are three steps to creating an application. 1. Design the forms by drawing controls. 2. Set initial propertyvalues for the forms and controls. 3. Write code to perform the required tasks. In the exercise that follows you will create a single form as shown below. Figure 3-1 The exercise shows you how to add controlsto a form and how to set propertiesat design time. This first application will not actuallydo anything until you add code as shown in chapter2. 1.4. Designing the form The form has three controls on it. There is a label which will display the text “Hello”and two command buttons. A label control is used to display text. A command buttoncontrolenables the user of your application to click on a buttonwhen they require an action to be carried out. You will learn later how to write the code that will be run when the button is clicked.
  • 17. PAGE 17 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 1.4.1 Drawing controls Drawing controlsis a two step process. First you need to add the controlyou want by doubleclicking on it in the tool box. Secondly, you need to position and size the control by clicking and dragging on the form. Firstly you will create the label which contains the text “Hello”. Step 3 Move the mouse over the controls in the toolbox until the tool tips indicate that you have found the label control. Figure 4-1 Step 4 Double click on the label control button and a label will appear in the middle of the form.
  • 18. PAGE 18 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Figure 4-2 1.4.2 Moving controls Controlscan be moved around a form by dragging them with the mouse. Step 5 Move the cursor to the label and depress the left mouse button. Keeping the mouse button depressed, drag the control to its new location, shown in Figure 4-3. Figure 4-3 Step 6 Release the mouse button. . 1.4.3 Resizing controls To achieve the form design shown in figure 3-1, you need to make the label controllarger.
  • 19. PAGE 19 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Before a controlcan be resized it must be selected. When a controlis selected black dots or handleswill surround it. Clicking and dragging one of these handleswill adjust the size of the control. Step 7 Click on the label control to select it. Figure 4-4 Step 8 Drag the bottom right handle to enlarge the control, then release the mouse button. Figure 4-5 1.4.4 Resizing the form The form can be resized in the same way as a control.
  • 20. PAGE 20 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 9 Position the mouse pointer over the right hand edge of the form. The mouse pointer will change to a doubleheaded arrow. Step 10Hold down the left mouse button and drag the edge of the form to the left to make the form smaller. 1.4.5 Setting properties Once the control is positioned correctlyon the form you then need to customise the propertiesof the control to suit your requirements. This application requires you to change the text that is displayed in the label control from the default “Label1” to “Hello”. You are also going to change the alignment to Centre and the font to Arial with font size 16. To change the propertiesfor a specific control,you must have the controlselected. Step 11 Click on the label control on your form to select it. Handles will appear on the label control to show it is selected.
  • 21. PAGE 21 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 12If the properties window is not visible, choose Properties Window from the View menu or right click on the label control and select Properties. Step 13 Check that the Properties window shows the properties for the label control. Figure 4-6 The Caption propertydetermines what text is displayed in the label control. Step 14If the Caption property is not highlighted, click on it to select it. If Caption is not visible in the list, use the scroll bar on the right to display it. Step 15 Type Hello in place of Label1 Step 16 Select the Alignment property and click on the drop down arrow to reveal the choices for this property.
  • 22. PAGE 22 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 17 Choose „2 - Centre‟. Step 18 Select the Font property and click the dialog button (…) to bring up the font window. Set the font values to Arial, Regular, 16. Figure 4-7 1.4.6 Adding the button controls You can use the same process to add two buttons to the form. Step 19 Double click on the command button control in the tool box.
  • 23. PAGE 23 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Figure 4-8 A command buttoncontrolwill appear in the middle of the form. Step 20 Move the button so that it is under the label. Step 21 Repeat the last two steps to add a second button under the first. Your form should appear similar to the one shown in figure 4-9. Figure 4-9 Step 22Select the button with the caption Command1. Make sure you can see the handles that indicate that the button is selected.
  • 24. PAGE 24 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 23 Using the properties window; change the caption property to On. Step 24 Use the same process to set the caption for the bottom button to Off. Step 25Using the properties window; change the Name property for the form to Hello. 1.5. Running your application Now that the form is complete you can see it in action by running it. When you have written code for the buttons, running the application will allow you to activate the code. For now your buttonswill not do anything. Step 26 Click on the start (play) button on the tool bar. Figure 5-1
  • 25. PAGE 25 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Your form will appear like a window from any other Microsoft application. Step 27 Click on one of the command buttons to see it depress. Step 28Click on the Stop button on the tool bar to return to the Visual Basic design environment. 1.6. Saving your application The last step in this chapteris to save your application so that you can use it for the exercises later in the book. Step 29 Click on the save button on the tool bar. Figure 6-1 Visual Basic first asks you to save the form and then the project file. Remember that each representsa separate file. Figure 6-2
  • 26. PAGE 26 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 30Change the directory to the a: drive or to the folder where you want to save your work. Specify the filename for the form as hello.frm. The file extension “frm” indicates that the file is a form file. Step 31 Click on the Save button. Figure 6-3 Step 32Change the name of the project to hello.vbp and click on Save. The file extension “vbp” indicates a project file. Always take care to ensure that you save all the files that make up a project. Exercises Exercise 1 1.Open a new project by choosing New Project from the File menu.
  • 27. PAGE 27 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 2.Design the form shown below. Note that you should set the caption property for the form to Project One. 3.Change the Name property for the form to frmMain. 4.Change the MousePointer property for the form to „2 - Cross‟. This is one of the values in the drop down list for this property. 5.Change the MousePointer property for the command button to „1 - Arrow‟ 6.Run the project and move the mouse across the form to check that the cursor changes when it is over the command button. 7.Save the project as Project1. (Use the default name of frmMain.frm for the form filename. 8.Close Visual Basic. Exercise 2 1.Launch Visual Basic 2.Open the project you saved in Exercise 1. 3.Using the form you created above, add a second button as shown below.
  • 28. PAGE 28 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 4.Change the Mouse Pointer property for the new button to „1 - Arrow‟. 5.Save the project using the same file names as before.
  • 29. PAGE 29 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Chapter 2. INTRODUCTION TO EVENTSAND PROGRAMMING 2.1 Programming in Visual Basic Now that you have designed a form and set some properties, it is time to make your applicationdo something. Visual Basic applicationswork by executing code written in the Visual Basic programming language. Code is associated with events. 2.1.1 What are events? Each Visual Basic object has a set of events. Events are actions which Visual Basic can detect and respond to. For example, a user clicking on a command button on a form will generate a click event for that button. When an event is generated Visual Basic will run any code you have entered for that event. 2.1.2 Opening an existing project You will now add code to the „Hello‟ project, which you created in the last chapter.
  • 30. PAGE 30 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 1 Choose Open Project from the File menu. Step 2 Change to the appropriate drive and folder and open hello.vbp. Figure 2-1 The project window will display the file “Hello.frm” from your project. Figure 2-2 Step 3 If the form is not visible, click the View Object button. 2.2 Writing code In this exercise you will write code to make your „Hello‟ label visible and invisible to the user when the On and Off buttonsare clicked.
  • 31. PAGE 31 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Each object has an associated set of events which it can respond to. To enter the code, you must access the code window. Step 4 Double click on the „On‟ button or click the right mouse button and select View Code from the object menu. Figure 2-3 The code window will appear as shown below. Figure 2-4 Clickon the Object menu and select Command1 from the list. The code window has three main components.
  • 32. PAGE 32 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 1.The object menu allows you to change between controls. For example, you could use this menu to look at the code for the Off button. 2.The event menu lists all of the available events for the selected control. This list will change depending on the type of control selected. 3.The code for the event is shown in the main part of the window. Your code will appear between the Private Sub and End Sub statements. Step 5 Position the cursor in the blank line between the Private Sub and End Sub statements. Figure 2.5 Visual Basic 5.0 uses the programming language Visual Basic for Applications, the same language found in many Microsoft Office applicationsincluding Microsoft Excel, Microsoft Access and Microsoft Project. The statement you are going to type assigns a value to the Visible propertyfor the label on your form. This propertyis set to the value True by default at design time. Your Visual Basic statement will change the value of the propertywhen the command buttonis clicked.
  • 33. PAGE 33 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 The syntax to refer to a propertyin code is: ObjectName.PropertyName The syntax to assign a value is ObjectName.PropertyName= Value Step 6 Press the tab key to indent the line of code and type Label1.Visible = True, and press the Enter key. Figure 2.6 Notice that the True has turned blue. This indicates that True is a special word or symbol that Visual Basic understands. Such words are known as reserved words. Step 7 Close the code window. You will now follow the same process to enter the code for the Off buttonto make the „Hello‟label invisible when this buttonis clicked. Step 8 Double click on the Off button to access the code window. Step 9 Position the cursor between the Private Sub and End Sub statements.
  • 34. PAGE 34 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 10 Press Tab and type Label1.Visible = False. Press the Enter key. Step 11 Close the code window. Whatthe does the code do? The code under your On button will appear as shown below. PrivateSub Command1_Click() Label1.Visible= True End Sub Each Visual Basic event has a subprogram associated with it. This is why Visual Basic automaticallyincludes the Sub and End Sub commands in your code. The name of the control(in this case Command1)and the event (in this case Click)will be joined together to form the subprogram name. This name follows the Sub statement. The line of code that you typed for the On button sets the Visible propertyof the control named Label1 to True (to make it visible). Similarly, the code underthe Off button sets the Visible propertyfor the Label1 control to False (to make it invisible). You can now change the value of the Visible propertywhile you are running your application.
  • 35. PAGE 35 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 2.3 Running your application Now that the code has been written your buttonswill operatewhen you run your application. Step 12 Click on the start button on the tool bar to run your application. The “Hello” label will initially be displayed because the visible propertyin the propertieswindow was set to true. Step 13 Click on the Off button. The label will disappear. Step 14 Click on the On button to make the text visible again. Step 15When you have finished experimenting click on the end button on the tool bar to return to Visual Basic. 2.4 Help for Visual Basic The Visual Basic environment provides you with a very comprehensive help system. You should also take advantage of the Microsoft on the Web feature for more
  • 36. PAGE 36 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 detailed instructionsand examples of how to carry out tasks. Figure 2-7 2.5 Input dialog boxes In this exercise you will add a third buttonto the form which will enablethe user to change the text displayed in Label1. An easy way to get user input, is by using input dialog boxes. Step 16 Add a third button to your form as shown below. Figure 2-8 Step 17 Change the button‟s caption to “Change Text”.
  • 37. PAGE 37 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Having added the buttonyou can now write the code to make it work. Step 18 Double click on the new button to display the code window. Step 19Enter the following code. Note that the Sub and End Sub lines will already be entered by Visual Basic. PrivateSub Command3_Click() Label1.Caption= InputBox("Enter the new text") End Sub Step 20 Close the code window. Whatdoes the code do? In the same way that you set the Visible propertyfor the Label1 control earlier on in the exercise, this code sets the Captionproperty. The InputBox function displays a dialog box to the user that allows them to enter in a value. This value is then returned by the function and assigned to the Caption propertyfor Label1. Step 21 Run the application to test your changes. Step 22 Click on the Change Text button.
  • 38. PAGE 38 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 The input dialog box shown below will appear. Figure 2-9 Step 23 Type in your name and click OK. Your name will now appearas the label on the form. Step 24 Click on the end button on the tool bar to return to Visual Basic. 2.6 The BackColor property The finishing touch for your application will be to change the colourof the form to green. Step 25Click anywhere in the form itself to select it. Be careful not to click on a control. Step 26In the properties window select the drop down menu for the BackColor property and select Palette. Remember, if you cannot see the BackColor property, use the scroll bars to move up or down the list. The color palettewill appear.
  • 39. PAGE 39 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Figure 2-10 Step 27 Click on a green square to set it as the new color. The color of the form will change. Step 28 Change the Back Color property for Label1 to a color of your choice. Step 29 Run your application to test your changes. Step 30 Save your project. Exercises Exercise 1 1.1 Open a new project by choosing New Project from the File menu.
  • 40. PAGE 40 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 1.2 Design the form shown below with three labels and one command button. Figure 2-11 1.3 Write the following code for the click event of the Time button. PrivateSub Command1_Click () Label2.Caption= Time End Sub 1.4 Run your application. When you click on the time buttonthe current time should appear as shown below. Figure 2-12 1.5 Save your application underthe names time.frm and time.vbp.
  • 41. PAGE 41 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Exercise 2 2.1 Using the form you created above, add a second buttonand change the Label3 caption as shown below. Figure 2-13 2.2 Write the following code for the click event for the Date button. PrivateSub Command2_Click() Label1.Caption= “The date is:” Label2.Caption= Date End Sub 2.3 Add a line of code to the Command1_Click subprogram so that the caption for Label1 is set back to “The time is:” when the Time buttonis pressed. 2.4 Change the BackColor of the form to a colourof your choice. 2.5 Run your applicationto test that it is working. 2.6 Save your application files with the names DateTime.frm and DateTime.vbp.
  • 42. PAGE 42 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Chapter 3. Text, Check Boxes and Buttons 3.1 Text boxes In the last chapter you used an input dialog box to get input from the user. In this chapteryou will use a text box control. You use a text box rather than an input dialog box when you want the user to enter data directly onto the form. The text box controldefines an area on a form where the user can type text. Visual Basic automatically responds to the delete and backspace keys, and other text editing functions such as copy and paste, without the need for any programming. In this chapteryou will create the form shown below which will allow the user to enter some text and format it. Figure 1-1 Firstly open a new project. Step 1 Choose New Project from the File menu, and select Standard EXE.
  • 43. PAGE 43 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 3.1.1 Creating a text box. Adding a text box to a form is exactly the same as adding any other Visual Basic control. Step 2 Double click on the text box control in the tool box window. Figure 1-2
  • 44. PAGE 44 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 3 Click and drag in the text box to move it to where you want it and to make it larger. Your text box should appearas shown below. Figure 1-3 3.1.2 The Name property When you draw a controlon a form Visual Basic automaticallyassigns the controla name. In this case “Text1”. The default name is determined by the type of control and the number of those controlson your form. The name is used to refer to a control when programming. It is good practice to change the name to something meaningful to your application.A useful convention is to give the control name a prefix which indicates the type of control. You will change this text box control‟sName to txtInput.
  • 45. PAGE 45 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 4 Check that the Text1 control is selected. It will appear on your form surrounded by black handles. Step 5 If the properties window is not visible, click the right mouse button and select Properties from the object menu. Step 6 Click on the Name property in the properties window. If the Name property is not visible, use the scroll bars to display it. Figure 1-4 Step 7 Type txtInput as the new name for the control. You have used the prefix “txt” to show that this control is a text box. When you draw a controlon a form Visual Basic automaticallyassigns the controla name. In this case “Text1”. The default name is determined by the type of control and the number of those controlson your form.
  • 46. PAGE 46 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 The name is used to refer to a control when programming. It is good practice to change the name to something meaningful to your application.A useful convention is to give the control name a prefix which indicates the type of control. You will change this text box control‟sName to txtInput. Step 4 Check that the Text1 control is selected. It will appear on your form surrounded by black handles. Step 5 If the properties window is not visible, click the right mouse button and select Properties from the object menu. Step 6 Click on the Name property in the properties window. If the Name property is not visible, use the scroll bars to display it.
  • 47. PAGE 47 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Figure 1-4 Step 7 Type txtInput as the new name for the control. You have used the prefix “txt” to show that this control is a text box. 3.1.3 The Text property You will notice that the text box on your form still contains the text “Text1”. This text is the current value of the Text property. To set the default text to something else, you would change the Text propertyfor the text box. In this case, you want it to be blank. Step 8 Check that the txtInput control on your form is selected and click on the Text property in the properties window.
  • 48. PAGE 48 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Figure 1-5 Step 9 Highlight Text1 and press the delete key on your keyboard to delete the text. The text should have now disappeared from the control on your form. 3.2 Adding a label In the previous chapteryou used a label to display the text that the user entered. More commonly labels are used as headings to prompt the user. You will now create a label to explain the text box. Step 10 Double click on the label tool in the tool box. Step 11 Click and drag to create a label in the form, positioned above the text box.
  • 49. PAGE 49 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 12 Change the Caption property of the label to Enter your text here:. Step 13 Use the Font property and change the font size to 14. Figure 2-1 Having added the text box and label to your form you can run it to see what it looks like. Step 14 Click on the play button on the tool bar. Step 15 Type something in the text box. Step 16 Click on the stop button on the tool bar to return to Visual Basic 3.3 Check boxes The next step is to add check boxes to the form. These will allow the user to change the format of the text in the text box to bold or italics. Check boxes are used to represent on/off values. They can either be checked or unchecked. When they are checked a tick will appear in the box.
  • 50. PAGE 50 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 17 Double click on the check box control in the tool box. Figure 3-1 Step 18 Move the check box to an appropriate location. Step 19 Repeat the last two steps to add a second check box below the first.
  • 51. PAGE 51 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Your form will appear like this. Figure 3-2 3.3.1 Changing the properties of the check boxes As with the text box you should now change the names of the check boxes to something more meaningful. Step 20 Click on the Check1 check box in your form. Step 21 Change the Name property to chkBold. You will also need to change the caption of the check box. Remember that this is what appears on the form. Step 22 Change the Caption property to Bold. Step 23Repeat the steps above to change the name of the second check box to chkItalic and the caption to Italic.
  • 52. PAGE 52 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Figure 3-3 3.3.2 Setting the Value property The Value propertyof a check box determines whether the check box is checked, unchecked or greyed. Step 24 Click on the Bold check box on your form to select it. Step 25 Click on the Value property in the properties window. Step 26 Use the pull down menu to see the possible values for this property. Figure 3-4
  • 53. PAGE 53 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 For now, leave the value propertyas 0 – Unchecked. You are going to write code so that if this property has the value one (i.e. the user has checked the box), the font will be bold, otherwise it will not be bold. 3.3.3 Writing the code At this stage the check boxes on the form will not perform any action. To make the appearanceof the text in the text box change when the check boxes are checked and unchecked you need to write code. Step 27 Double click on the Bold check box on your form. The code window will appear. Step 28Enter the code shown below. Remember that the Private Sub and End Sub statements are automatically included by Visual Basic Private Sub chkBold_Click () If chkBold.Value = 1 Then txtInput.FontBold = True
  • 54. PAGE 54 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Else txtInput.FontBold = False End If End Sub This is shown below. Figure 3-5 This is an example of the Visual Basic If statement. The If statement in code allows a choice of actions. The general structureis: If expression Then statements1 Else statements2 End If
  • 55. PAGE 55 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 The first line of the If statement in your example tests the current value of the check box. If the value of the propertyValue is equal to 1, then the check box is checked. If the bold checkbox is checked, set the FontBold propertyof the txtInput control to True. Otherwise, if the check box value is not 1 (unchecked or inactive), set the FontBoldpropertyof the txtInput control to False. Run the application to see the result. Step 29 Click on the play button on the tool bar. Step 30 Type some text in the text box. Step 31 Click on the Bold check box to check it. The text in the text box should be displayed as bold. Step 32 Click on the Bold check box to uncheck it. The text in the text box should be displayed as normal. Step 33 Click on the stop button on the tool bar to return to Visual Basic.
  • 56. PAGE 56 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 3.3.4 The Italic check box code The code for the Italic check box is similar to the Bold check box but changes the FontItalic property. Step 34 Double click on the Italic check box. The code window will appear. Step 35Enter the code shown below. Remember that the Private Sub and End Sub statements are automatically included by Visual Basic. Private Sub chkItalic_Click () If chkItalic.Value= 1 Then txtInput.FontItalic = True Else txtInput.FontItalic = False End If End Sub Run the application to see the result.
  • 57. PAGE 57 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 36 Click on the play button on the tool bar. Step 37 Type some text in the text box. Step 38 Click on the Italic check box to check it. The text in the text box should be displayed as italic. Step 39 Click on the stop button on the tool bar to return to Visual Basic. 3.4 Option buttons Check boxes are used to represent true/false, on/off or yes/no values. Option buttonsare used to give a set of choices. Option buttonsare sometimes called radio buttons. When one option button is selected the othersin the set are automaticallydeselected. You will now use a set of option buttonson your form to represent three font type choices for the text box. Step 40 Double click on the option button control in the tool box.
  • 58. PAGE 58 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Figure 4-1 Step 41 Move the option button to an appropriate location. Step 42 Repeat the steps above to add two more option buttons to your form. Figure 4-2 Step 43 Change the Name and Caption properties of the option buttons as given below. Name Caption optMSSansSerif MS Sans Serif
  • 59. PAGE 59 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 optTimesNewRoman Times New Roman optArial Arial You may need to resize the option controlsso that the caption fits on one line. Figure 4-3 3.4.1 Setting the Value property You will set the initial value of the MS Sans Serif option buttonto True because this is the default font type for the text box. To do this you set the Value property. The possible values are True or False. True indicates that the option buttonis selected. Step 44 Click on the MS Sans Serif option button on your form to select it. Step 45 Click on the Value property in the properties window.
  • 60. PAGE 60 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 46 Use the pull down menu in the editing panel to change the value to Trueas shown below. Figure 4-4 3.4.2 The code for the optionbuttons The code for the option buttonssets the FontName propertyfor the text box accordingly. Step 47 Double click on the MS Sans Serif option button in your form. The code window will appear. Step 48Enter the code shown below. Remember that the Private Sub and End Sub statements are automatically included by Visual Basic. Private Sub optMSSansSerif_Click ()
  • 61. PAGE 61 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 txtInput.FontName = "MS Sans Serif" End Sub Step 49Repeat this process to enter the code shown below for the Times New Roman button. Private Sub optTimesNewRoman_Click () txtInput.FontName = "Times New Roman" End Sub Step 50 Repeat the process again to enter the code shown below for the Arial button. Private Sub optArial_Click () txtInput.FontName = "Arial" End Sub Run the application to see the result. Step 51 Type some text in the text box. Step 52 Click on the Times New Roman option button to select it.
  • 62. PAGE 62 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 The text in the text box will be displayed in Times New Roman. Step 53 Try the Arial and MS Sans Serif buttons. Step 54 Click on the Stop button on the tool bar to return to Visual Basic. 3.4.3 Changing the colourof the text box The final task is to change the background colourof the text box when the font changes. Visual Basic provides names for colourvalues, for example, vbRed for red, vbGreen for green and vbBlue for blue. You need to add a line of code, which assigns a value to the BackColor propertyof the text box, to the option buttoncode. Step 55 Double click on the MS Sans Serif option button and insert the line shown: Private Sub optMSSansSerif_Click () txtInput.FontName = "MS Sans Serif" txtInput.BackColor = vbGreen End Sub
  • 63. PAGE 63 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 56Add code to the Times New Roman button to change the text box colour to Red and code to the Arial button to change the text box colour to Blue. Step 57 Try out your application. 3.5 Finishing the form To tidy up the form you will add a title to it and change the size. Step 58 Click anywhere on the form to select it. Be careful not to click on a control. Step 59 Change the Caption property of the form to read Text Formats. Step 60 Resize the form to a more appropriate size. The completed form will appearas shown below. Figure 5-1
  • 64. PAGE 64 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 61 Run the application to test the changes. Step 62 Save the project. Remember to save both the project file and the form. 3.6 Programming Notes 3. 6.1 If statements Use the If statement when you want to carry out different actions dependingon current values in your application. The syntax of the If statement can be any of the following: a) If condition Thenstatements1 b) If condition Thenstatements1 Else statements2 c) If condition Then statements1 End If d) If condition Then statements1 Else statements2
  • 65. PAGE 65 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 End If e) If condition Then statements1 Elseif statements2 Else statements3 End If 3. 6.2 Built-In Color Constants Visual Basic recognizes the following constant names for colors. You can use these names in your code in place of the actual values. Constant Value Description vbBlack 0x0 Black vbRed 0xFF Red vbGreen 0xFF00 Green vbYellow 0xFFFF Yellow vbBlue 0xFF0000 Blue vbMagenta 0xFF00FF Magenta vbCyan 0xFFFF00 Cyan vbWhite 0xFFFFFF White
  • 66. PAGE 66 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Exercises Exercise 1 1.1 Your boss has commissioned you to design the interface for a name tag recording system. 1.2 Open a new project by choosing New Project from the File menu and design the form shown below. Figure 7-1 1.3 Change the name of the text box to txtName. 1.4 Write the code to make the bold and italic check boxes work. Exercise 2 2.1 Change the Caption of the form to Name Tag Designer and the name of the form to frmTag. 2.2 Add Times New Roman and Arial option buttons to change the font of the text box. Figure 7-2
  • 67. PAGE 67 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 2.3 Write the code to make the option buttonswork. Exercise 3 3.1 Add a command buttonto the form so when it is clicked, the background colourof the text box changes to green if it was blue or changes to blue otherwise. Figure 7-3 (Hint: If txtName.BackColor= vbBlue Then…) 3.2 Add a command buttonto the form so that when it is clicked, an input dialog box asks the user for a font size for the tag. Figure 7-4 (Hint: txtName.FontSize= InputBox(“Entera new font size e.g. 14”))
  • 68. PAGE 68 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Chapter 4. List boxes, combo boxes and forms 4.1 Projects with more than one form In this chapteryou will create an application in which one form opens a second form. The opening form, which is called the main form, will include a command buttonwhich activates the second form. The second form allows the user to maintain a list of clients. When your user is finished with the second form they will return to the main form to terminate the application. Step 1 Design a main form to look like the following. Figure 1-1 Step 2 Change the name of the form to frmWidgets. Step 3 Change the names of the command buttons to cmdClientList (caption Client List) and cmdQuit (caption Quit). Step 4 Add the following code to the Quit button to terminate the application.
  • 69. PAGE 69 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Private Sub cmdQuit_Click() End End Sub The next stage will add a second form to the application.At this stage, just an outlinedesign will be sufficient. Step 5 Click on the add form button on the toolbar. Figure 1-2 Click Form from the display list of available forms and then click Open. Figure 1-3
  • 70. PAGE 70 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 The form will be displayed in a separate window. Select the new window so you can see the form. Step 6 Design the form to look like the following. Figure 1-4 Step 7 Change the name of the form to frmClientList and the name of the Close button to cmdClose. Step 8 Add this code to the command button. Private Sub cmdClose_Click() Unload Me End Sub Visual Basic provides the keyword Me as a shortcut to refer to the currentlyactive form. Step 9 Select View Object for frmWidgets from the Project window.
  • 71. PAGE 71 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Figure 1-5 When your user clicks on the Client List button,the second form should become active. The process of making a form active is a two stage process. It must first be loaded into memory using the Visual basic subprogram Load. Then it must be made visible using the method Show. Step 10 Add the following code for the Client List command button. Private Sub cmdClientList_Click() Load frmClientList frmClientList.Show End Sub Step 11Run your application to check that it works correctly. Use the Quit button to stop the application running. Step 12Save the project. Note that there are now three files to be saved. Use the Visual Basic default of the form names as the file
  • 72. PAGE 72 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 names for the forms. Save the project file as Widgets.vbp The Visual Basic default will mean that the filenames for your form files are frmClientList.frm and frmWidgets.frm. 4.2 List boxes You will now complete the design of the second form 4.2.1 What is a list box? A list box is a list, which appears on the screen, from which the user can choose items. In this chapteryou will create the text box and list box shown below and program the add, delete, clear buttons. Figure 2-1 The add button will add the contentsof the text box to the list. The delete buttonwill remove the item in the list which the user has selected. The clear buttonwill deleteall the items from the list.
  • 73. PAGE 73 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 4.2.2 Creating a list box Creating a list box is just like creating any other Visual Basic control. Step 13 Select View Form for frmClientList from the Project window. Step 14 Double click on the list box control in the tool box. Figure 2-2 Step 15 Move and size the list box. Figure 2-3 Adding the other controls on the form
  • 74. PAGE 74 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 16Add a label, text box and three more command buttons to the form, as shown below. Figure 2-4 Step 17Name each of the new controls by selecting the control on your form and changing the name property in the properties window. Control Name Text1 txtInput List1 lstClientList Command1 cmdAdd Command2 cmdDelete Command3 cmdClear The controlsalso require the captionsto be changed. Step 18 Change the Caption properties as shown below.
  • 75. PAGE 75 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Control Name Label1 Enter the name to add: cmdAdd Add cmdDelete Delete cmdClear Clear Step 19 Clear the Text property of the txtInput control. Check the appearanceof the form at this stage. Step 20Click on the play button on the tool bar. Click on the Client List button. The second form should then appear as shown below. Figure 2-5 Step 21 Click on the Close and Quit buttons once you have checked your form. 4.2.3 The add button
  • 76. PAGE 76 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 The Add buttonwill add what the user has entered in the text box into the list. Step 22 Double click on the Add button on your form to display the code window. Step 23Enter the code shown below. Remember that the Private Sub and End Sub statements will have been entered for you by Visual Basic. Private Sub cmdAdd_Click () lstClientList.AddItem txtInput.Text txtInput.Text = "" txtInput.SetFocus End Sub The first line of the code adds an item to the list box (remember the name of the list box is lstClientList). The item which is added is the current value of the Text propertyof the txtInput control. This will be what the user has entered into the text box. The next line resets the Text propertyof the text box to a blankstring, ready for the next entry by the user. The last line repositionsthe cursor inside the text box so the user can begin typing the next entry.
  • 77. PAGE 77 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 24 Run the application to test the Add button code. Step 25 Type your name as the first client to add to the list. Step 26 Click on the Add button. Your name should appear in the list box and the text box should appearempty. The cursor will be positioned in the text box ready for the next entry. Figure 2-6 Step 27Use the Close and Quit buttons on your forms to stop the application when you have finished. 4.2.4 The Delete button The Delete button will delete the item that the user has selected in the list box from the list.
  • 78. PAGE 78 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 28 Double click on the Delete button on your form to display the code window. Step 29Enter the code shown below. Remember that the Private Sub and End Sub statements will have been entered for you by Visual Basic. Private Sub cmdDelete_Click () If lstClientList.ListIndex >= 0 Then lstClientList.RemoveItem lstClientList.ListIndex Else Beep End If End Sub The code uses an If statement to test whether the user has selected an item in the list. The ListIndex propertyfor the lstClientList control holdsa number indicating which item in the list has been selected by the user.
  • 79. PAGE 79 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 If ListIndex is zero then the first item in the list is selected. If ListIndex is equal to one then the next item in the list is selected and so on. If ListIndex is less than zero it means that nothingis selected. If something is selected the next line of the code will remove it from the list. Visual Basic will remove the item at list position lstClientList.ListIndex. If nothing is selected a Beep is sounded. You should run the applicationto test the Delete button code. Step 30 Click on the play button on the tool bar and click the Client List button. Firstly, test to see what happens if you click the Delete buttonwhen the list is empty. Step 31 Click the Delete button. A beep should sound to indicate that nothing in the list is selected for deletion.The action should not result in an error message. Next, test that the Delete buttoncorrectlydeletes list items. Step 32Add several items to the list by entering them in the text box and clicking on the Add button.
  • 80. PAGE 80 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Figure 2-7 Step 33 Click on one of the items to select it. The item you select will become highlighted. Step 34 Click on the Delete button. The item will be removed from the list. Step 35 Try selecting and deleting the other list items. Step 36 Stop the application running when you have finished testing. 4.2.5 The Clear button The Clear button will deleteall of the items in the list. Step 37 Double click on the Clear button on your form to display the code window.
  • 81. PAGE 81 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 38 Enter the code shown below. Remember that the Private Sub and End Sub statements will have been entered for you by Visual Basic. Private Sub cmdClear_Click () lstClientList.Clear End Sub The code uses the Clear method to deleteall the items from the list. Run the application to test the Clear button code. Step 39 Click on the play button on the tool bar. Click on Client List. Step 40 Add several items to the list using the Add button. Step 41 Click on the Clear button. All of the items in the list will be deleted. Step 42 Stop the application running. Step 43 Save the project. 4.2.6 Selecting Items from a list
  • 82. PAGE 82 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 The following example demonstrateshow to use a list box to allow your user to make a selection from the list. The items in this list box example are added at design time. Step 44 Start a new project. Step 45 Add a list box to the form. Change its name to lstFruits. Step 46 Select the List property for the list box and click on the drop down arrow. Step 47 Type apples then press Ctrl Enter Step 48 Type the other fruit names shown pressing Ctrl Enter after each one. Figure 2-8 Step 49If you cannot see the full list of fruit in the list box on your form, make the list box larger.
  • 83. PAGE 83 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 50Add a label with the caption shown and a text box to your form as in figure 2-9. Step 51 Change the name of the text box to txtSelection. Figure 2-9 The List propertyreturns the contentsof the list for the specified index. Step 52 Add the following code to the click event for the list box. Private Sub lstFruits_Click() txtSelection.Text = lstFruits.List(lstFruits.ListIndex) End Sub
  • 84. PAGE 84 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 53Run the application and test that clicking on an item in the list box will cause it to be displayed in the text box. Step 54 Stop the application. 4.3 Combo Boxes The combo box control is a combination of a list box and a text box. Items are stored in a list and viewed using a drop down menu. The contentsof the Text property of the combo box are displayed at the top of the combo box. Step 55 Start a new project. Step 56 Add a combo box to the form. Figure 3-1
  • 85. PAGE 85 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 57 Using the List property, add the items Potatoes, Carrots, Peas and Beans to the list. Step 58Run the application. Click on the drop down arrow for the combo box and select one of the items. The Text propertyof the combo box is set to the selected item and it will be displayed. Step 59 Stop the application. Combo boxes also allow your user to type in a value to set the text propertywhile the program is running. This can be useful if the user needs a value that is not in the list. 4.4 Programming Notes 4.4.1 Subprograms A subprogram is a block of code which is activated by an event or called from anotherblock of code. The syntax to call a subprogram is: SubprogramName [parameters] or Call SubprogramName[(Parameters)]
  • 86. PAGE 86 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Examples of subprograms provided by Visual Basic are Load, Unload and End. 4.4.2 Methods A method is a subprogram which acts on an object. The syntax to use a method is: object.method[parameters] Examples of methods are:  Show for a form e.g. frmMain.Show  AddItem for a list or combo box e.g. lstFruits.AddItem “Pineapple”  SetFocus for any object e.g. cmdQuit.SetFocus 4.4.3 Displaying Form Each form must first be loaded into memory and then made visible. The main form of a project is automaticallyloaded into memory and made visible when the project starts running. For otherforms, statements to Load them and Show them must be executed. For example: Load Form2
  • 87. PAGE 87 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Form2.Show If you have forms which you want to open and close frequently, you can avoid the overhead of loading them into memory each time by loadingthem once then using the Show and Hide methods to make them available when required. Each form has a load event and you can put code here to initialise values. The load event for the main form is a good placeto load otherforms. Private Sub Form1_Load() Load Form2 Load Form3 End Sub When you want Form2 to become visible, use the statement: Form2.Show When you want Form2 to be invisible, use the statement: Form2.Hide 4.4.4 Summary
  • 88. PAGE 88 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Load is a Sub (subprogram) which loads the specified form into memory. Note that it doesn‟t necessarily make the form visible. (Load is also an event for a Form). Show is a method which loads the specified form into memory if it isn‟t already loaded and makes it visible. Unload is a Sub which unloadsthe specified form from memory. Hide is a method which makes a form invisible but doesn‟t unload it from memory. Me is a convenient way of referring to the current form. End is a subprogram which terminates the currently running project and unloadsall its forms. Exercises Exercise 1 1.1 Open a new project by choosing New Project from the File menu. 1.2 Design the form shown below. Change the Name propertyto frmDemo. Add code to the QUIT buttonto stop the applicationrunning.
  • 89. PAGE 89 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Figure 5-1 1.3 Click on the Add Form buttonand then on the existing tab to add the form frmClientList, which you saved earlier in the chapter, to this project. 1.4 Add code to frmDemo to access frmClientList when the List Box buttonis clicked. Exercise 2 2.1 Choose Add Form from the Project menu or click the Add Form button on the toolbarto add a new form to your project. Change the name propertyto frmComboDemo. 2.2 Design the form to look like the following. Set the Style propertydifferently for each combo box. 2.3 Add the items: eggs, butterand ham to the lists for each combo box.
  • 90. PAGE 90 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Figure 5-2 2.4 Add code to the Combo Box buttonin frmDemo to activate frmComboDemo. 2.5 Save the project as Ch4Demo.
  • 91. PAGE 91 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Chapter 5. Multiple Document Interface (MDI) Applications, Toolbars, Using the Clipboard, Popup Menus 5.1 Multiple Document Interface Applications WordPad and Notepad are examples of applications using the single document interface (SDI) style. Applicationssuch as Word and Excel that allow you to display multipledocuments at the same time are known as MDI (MultipleDocument Interface) applications. In an MDI application,the main form is called the MDI form, and the document forms are called MDI child forms. You usually design only one child form for an MDI application. This form is then copied for each document that is opened. In this chapter, you will create an MDI application 5.2 Building the Application 5.2.1 Creating the MDI form Step 1 Start a new project and remove the existing Form1 (select Remove Form1 from the Project menu). Step 2 Select Add MDI Form, New, from the Project menu. Click on MDI Form. Click Open.
  • 92. PAGE 92 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Figure 2-1 Step 3 Change the Name property for this form to MDIEditor. 5.2.2 Adding the child form Step 4 Select Add Form, Existing, from the Project menu and add the form you developed for the FileViewer application (frmFileViewer.frm). Step 5 Select the Fileviewer form. Change the MDIChild property to True. Step 6 Change the Name property to frmDoc. Save the form as frmDoc.frm. Save the MDI form as mdiEditor.frm and the project as MDI.vbp You will write code to create a new instance of this form each time a new document is opened.
  • 93. PAGE 93 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 5.2.3 Resizing the rich textbox The child forms in an MDI applicationcan be resized independently. The rich textboxcontrol should grow or shrink with the form. The resize event for the form can be used to achieve this. Step 7 Add the following code to the Resize event for the frmDoc form Private Sub Form_Resize() rtbDisplay.Move0,0,Me.Width – 100, Me.Height - 300 End Sub 5.2.4 Setting up the menus You have a choice of how you set up menus for an MDI project:  Attach all the menu options to the MDI form and disable the options not available until there is a child form open  Attach menu options to the child form and use a different menu on the MDI form – the menu on the MDI form will automatically be replaced by the menu on the child form as soon as a child form is opened
  • 94. PAGE 94 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 The child form that you have just added to the project, already has a menu attached so the second option is preferable for this project. Step 8 Select code for the frmDoc form and amend the mnuNew and mnuOpenFile options as shown A new instance of a document form is created by declaring a variable as type New frmDoc. Private Sub mnuNew_Click() Dim newDoc As New frmDoc newDoc.Show newDoc.Caption = "Untitled" newDoc.rtbDisplay.Text = "" End Sub Private Sub mnuOpenFile_Click() Dim strFileName As String Dim newDoc As New frmDoc newDoc.Show newDoc.cdlViewer.Filter= "RTF|*.rtf|Text|*.txt|All|*.*"
  • 95. PAGE 95 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 newDoc.cdlViewer.ShowOpen On Error GoTo OpenProblems strFileName = newDoc.cdlViewer.filename newDoc.rtbDisplay.LoadFile strFileName newDoc.Caption = strFileName Exit Sub OpenProblems: MsgBox "Can’t open the file, select another.", vbCritical Exit Sub End Sub The main MDI form can be given a simple menu to enablethe first document to be opened. Step 9 Select the MDIEditor form and use the Menu Editor to set up a menu which looks like the following Figure 2-2 Caption Name
  • 96. PAGE 96 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 &File mnuFile &New mnuNew &Open mnuOpen - mnuSep1 E&xit mnuExit Step 10Add a common dialog control to form MDIEditor. Change its Name property to cdlViewer. Step 11Copy the code from the frmDoc form New and Open options to the New and Open options for the MDIEditor form. Step 12 Add code for the mnuExit option to the MDIEditor form Private Sub mnuExit_Click() End End Sub Step 13Save the project and files. Run the application to test the main menu options. (You will need to set Start Up Object to MDIEditor).
  • 97. PAGE 97 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Figure 2-3 5.3 Closing MDI application forms In MDI applications, there is usually an option for the user to close individual child forms. The close operation might be cancelledif the information in the child form has not been saved since it was last changed. You will also need to ensure that the child forms are properlyclosed if the user presses the close buttonon the main MDI form. 5.3.1 Deciding if changes to data in a child form have been saved The simplest way to do this is to use a global boolean variable which is set to true when text in the rich textbox is changed, and set to false when the data is saved.
  • 98. PAGE 98 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 14Save the form mdiEditor.frm as mdiEditor1.frm. Save the form frmDoc.frm as frmDoc1.frm. Save the project as MDI1.vbp. Use the menu editor to add a Close option (&Close and mnuClose) to the File menu on the frmDoc1 form. Step 15Add the following declaration to the General Declarations section of the frmDoc1 form (below the Option Explicit statement). Dim Changed As Boolean Step 16 Add this code to the Change event for the rtbDisplay (the rich textbox). Private Sub rtbDisplay_Change() Changed= True End Sub Step 17 Amend the code for the mnuSaveFile option for frmDoc1 form as shown Private Sub mnuSave_Click()
  • 99. PAGE 99 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Dim strFileName As String cdlViewer.Filter = "RTF|*.rtf|Text|*.txt" cdlViewer.ShowSave On Error GoTo SaveProblems strFileName = cdlViewer.filename If cdlViewer.FilterIndex = 1 Then cdlViewer.DefaultExt = “RTF” rtbDisplay.SaveFile strFileName, rtfRTF Else cdlViewer.DefaultExt = “txt” rtbDisplay.SaveFile strFileName, rtfText End If Me.Caption = strFileName Changed= False Exit Sub SaveProblems: MsgBox "Can’t save the file, try again.", vbCritical Exit Sub End Sub
  • 100. PAGE 100 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 18 Add code to the mnuClose option of the frmDOC1 form. Private Sub mnuClose_Click() Unload Me End Sub The Unload event will cause the child form to be unloaded but before it does this, it triggers the QueryUnload event for the form. This event allows you to cancel the unload if you wish. Step 19 Add the following code to the QueryUnload event for the frmDoc1 form (select Form, QueryUnload in the code window).. Private Sub Form_QueryUnload (Cancel As Integer, _ UnloadModeAs Integer) Dim Response As Long If Changed Then
  • 101. PAGE 101 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Response = MsgBox("Are you sure you want to close_ without saving?", vbYesNoCancel, Me.Caption) Select Case Response Case vbNo: Cancel = True Case vbCancel: Cancel = True End Select End If End Sub This QueryUnload code will also be executed if the user clicks the Close button on a child form. 5.3.2 Closing the application The Exit option on the File menu should also close down the application properly. Using an Unload statement for the main MDI form will trigger the QueryUnload events for any open child forms and allow changes to be saved before the application terminates. Step 20 Amend the code for the mnuExit option on the frmDoc1 as shown
  • 102. PAGE 102 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Private Sub mnuExit_Click() Unload MDIEditor End End Sub An Unload event for the main form is triggered if the Close button on the main form is clicked so you don‟t need to do anything further to deal with this event. Save the project and files and test the program. Figure 2-4 5.4 Using the Clipboard Adding Cut, Copy and Paste optionsto the Edit menu for this application will show you how to manipulate the clipboard from a Visual Basic application. Step 21Save mdiEditor1.frm as mdiEditor2.frm. Save frmDoc1.frm as frmDoc2.frm. Save the project as MDI2.vbp. Use the Menu Editor to add Cut, Copy, Paste and Delete
  • 103. PAGE 103 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 sub-options to a new Edit menu on the frmDoc2 form. Caption Name &Edit mnuEdit C&ut mnuCut C&opy mnuCopy &Paste mnuPaste &Delete mnuDelete Step 22 Add the following code for these menu options. Public Sub mnuCopy_Click() Clipboard.Clear Clipboard.SetText rtbDisplay.SelText, vbCFRTF End Sub Public Sub mnuCut_Click() Clipboard.Clear
  • 104. PAGE 104 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Clipboard.SetText rtbDisplay.SelText, vbCFRTF rtbDisplay.SelText = "" End Sub Public Sub mnuPaste_Click() rtbDisplay.SelText = Clipboard.GetText(vbCFRTF) End Sub Private Sub mnuDelete_Click() rtbDisplay.SelText = "" End Sub Step 23 Save the project and files. Test the edit menu options. 5.5 Adding a toolbar The Microsoft Windows Common Controls 5.0 component provides the controlssuch as ToolBar, StatusBar, ProgressBar, TreeView, ListView and
  • 105. PAGE 105 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 TabStrip which are part of a typical Windows 95 application interface. The collectionalso includesthe ImageList control which is used as a central repositoryto supply other controlswith images. An ImageList controlcontains a collectionof ListImage objects, each of which can be referred to by its index or key. This section shows you how to use the Toolbarand ImageList controlto add an edit toolbarcontainingCut, Copy and Paste buttonsto your application. Save mdiEditor2.frmas mdiEditor3.frm. Save frmDoc2.frmas frmDoc3.frmand save the project as MDI3.vbp. Step 24Select Microsoft Windows Common Controls 6.0 from Components on the Project menu. 5.5.1 Using an ImageList control to store button pictures for the toolbar The imagelist controlwill be used as a repository for the images on the toolbarbuttons. Step 25Add an ImageList control to form MDIEditor3. (The control is not visible when the application is running).
  • 106. PAGE 106 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 26 Change the Name property for the ImageList control to imlToolbar. Step 27Right click on the ImageList control and select Properties to bring up the property pages. Figure 5-1 Step 28On the General tab, select Custom size. (This will size the images to the size of the first image added). Step 29 Select the Images tab and select Insert Picture. The folder Program FilesMicrosoft Visual StudioCommonGraphicsBitmapsTlbr_Win95 contains the images you will need to create typical Windows toolbars. Step 30 Add an image for the Cut button and set the Key property to Cut.
  • 107. PAGE 107 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 31Repeat the last step for Copy and Paste. Then click OK to close the Property Pages dialog box. Figure 5-2 Note that the cut picturehas an index value of 1, the copy image an index value of 2, and the paste image an index value of 3. The images in the imagelist control can be referenced either by using the index or the key values. 5. 5.2 Setting up the toolbar Step 32Add a Toolbar control to the MDIEditor3 form. Change the Name property to tlbEditor. Step 33Right click on the toolbar control and select Properties to bring up the property pages. Step 34Set the ImageList property to imlToolbar.
  • 108. PAGE 108 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Figure 5-3 Step 35 Select the Buttons tab and select Insert Button. Step 36Set the Key, ToolTip Text and Image values as shown. Figure 5-4 Button Index ToolTipText Key Image Cut 1 Cut text Cut 1
  • 109. PAGE 109 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Copy 2 Copy text Copy 2 Paste 3 Paste text Paste 3 Step 37 Repeat the last step for the Copy and Paste buttons. Your form should now look like Figure 5-5 5. 5.3 Adding the code The code for the Cut, Copy and Paste optionshas already been added to the menu optionson the frmDoc form. This code can be accessed from the MDIEditor form if it is made Public rather than Private. The ActiveForm propertyis used to refer to the currently active child form. Step 38View the code for form frmDoc3 and change the Private declaration to Public for the Cut, Copy and Paste menu options.
  • 110. PAGE 110 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Public Sub mnuCut_Click() Clipboard.Clear Clipboard.SetText rtbDisplay.SelText, vbCFRTF ' Delete selected text. rtbDisplay.SelText = "" End Sub Step 39 View the code for form MDIEditor3 and add the following event code Private Sub tlbEditor_ButtonClick(ByVal Button As ComctlLib.Button) On Error GoTo NoActiveForm Select Case Button.Key Case "Cut": Me.ActiveForm.mnuCut_Click Case "Copy": Me.ActiveForm.mnuCopy_Click Case "Paste": Me.ActiveForm.mnuPaste_Click
  • 111. PAGE 111 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 End Select Exit Sub NoActiveForm: Exit Sub End Sub The error code is added to deal with the buttonsbeing clicked when there is no child form open. 5. 5.4 Adding an option to view the toolbar Step 40Using the Menu Editor, add an Options menu to the MDIEditor3 form menubar. Add a submenu to this called toolbar. Caption Name &Options mnuOption &Toolbar mnuToolbar
  • 112. PAGE 112 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Figure 5-6 Step 41 Select the checked option for the toolbar menu item. Close the menu editor. Step 42 Add code to the Toolbar menu option. Private Sub mnuToolbar_Click() mnuToolbar.Checked = Not mnuToolbar.Checked tlbEditor.Visible = Not tlbEditor.Visible End Sub 5.6 Popup Menus Windows applications usuallyprovide a popup menu (or context menu) when you click the right mouse button. A popup menu is designed using the Menu
  • 113. PAGE 113 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Editor. Setting the Visible propertyto false ensures that the popup menu is not displayed on the menu bar. Invoking the PopupMenu method for the form when the right mouse button is clicked displays the menu as a popup menu. This example displays a pop-upmenu at the cursor location when the user clicks the right mouse button over the rich textbox displaying a document. Step 43 Add a menu to the frmDoc3 form that contains the options shown Figure 6-1 CaptionName &Popup mnuPopup &Cut mnuPopupCut C&opy mnuPopupCopy P&aste mnuPopupPaste C&hange Font mnuPopupChangeFont
  • 114. PAGE 114 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 44 Make sure that the Visible box for the menu header is not checked. Step 45 Add the following code to the popup menu options. Private Sub mnuPopupCopy_Click() mnuCopy_Click End Sub Private Sub mnuPopupCut_Click() mnuCut_Click End Sub Private Sub mnuPopupFont_Click() mnuChangeFont_Click End Sub Private Sub mnuPopupPaste_Click() mnuPaste_Click End Sub
  • 115. PAGE 115 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Step 46Add the following code to the MouseUp event for rtbDisplay (the rich textbox) on form frmDoc3. Private Sub rtbDisplay_MouseUp(ButtonAs Integer, Shift As Integer, x As Single, y As Single) If Button= 2 Then Me.PopupMenu mnuPopup End If End Sub Save the project and files and test the application. You should now get a context menu if you click the right mouse button anywhere in a document. Exercises Exercise 1 Add an undo option to the Edit menu which undoesthe last delete. (Hint: declare a public string variable and copy the selection to this variable when Delete is executed). Exercise 2 Add a Windows menu to your application that includes the options to Tile or Cascade the currentlyopen child
  • 116. PAGE 116 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 windows. (Hint: use the Arrange method for MDIEditor).
  • 117. PAGE 117 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 References VB Reference Books A Visual Basic 6 Programmer's Toolkit by Smith, Eric and Marquis, Hank; Apress; 3/2000; ISBN 1893115062. List $39.95 softcover. Visual Basic 6 Programmer's Reference by Dan Rahmel; Osborne, ISBN: 0-07- 882576-8, $16.99 US. I. Overview of VB, VBA, and VB Script II. Programming System Information and How-To-Examples III. Language Reference IV. Object-Model Diagrams VB6 Reference Library Visual Basic 6 Programmer's Guide
  • 118. PAGE 118 Programming with Visual Basic 6.0 by J. T  University Computing Centre Ltd. One stop Centre for your ICT Solutions. www.ucc.co.tz. 022 2410645 Visual Basic 6 Unleashed, Professional Reference Edition Author: Rob Thayer, et al. Retail Price: $49.99 US; Publisher: SAMS; ISBN: 0672315084; Publication Date: June 22, 1999; Pages: 1296