Visual Basic for Education A step by step approach to Visual Basic specially tailored for the classroom. Adrian Janson

Note that this preview contains the contents pages and the second chapter of the book. The first chapter of the book introduces students to algorithmic design and programming techniques and is not typical of the book as a whole. For this reason, it was not chosen as the preview chapter.

Published by Adrian Janson

Published in 2003 by Adrian Janson ABN 58285868158 1/28 McLaren Street Mount Waverley, Vic. 3149 Australia E-mail: [email protected] Telephone: 0407818037 Facsimile: (03) 9807 2527 © Adrian Janson 2003 ISBN 0-9750764-0-X Companion CD-ROM ISBN 0-9750764-1-8 Copying for educational purposes The Australian Copyright Act 1968 (the Act) allows a maximum of one chapter or 10% of this book, whichever is the greater, to be copied by any educational institution for its educational purposes PROVIDED THAT THE EDUCATIONAL INSTITUTION (OR THE BODY THAT ADMINISTERS IT) HAS GIVEN A REMUNERATION NOTICE TO COPYRIGHT AGENCY LIMITED (CAL) UNDER THE ACT. For details of the CAL licence for educational institutions contact: Copyright Agency Limited Level 19, 157 Liverpool Street Sydney, NSW 2000 Telephone: (02) 9394 7600 Facsimile: (02) 9394 7601 E-mail: [email protected] Copying for other purposes Except as permitted under the Act (for example a fair dealing for the purposes of study, research, criticism or review) no part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means without prior written permission. All inquiries should be made to the publisher at the email address or phone number shown above. Janson, Adrian. Visual Basic for education : a step by step approach to Visual Basic specially tailored for the classroom. For secondary students. ISBN 0 9750764 0 X. ISBN 0 9750764 1 8 (CD-ROM). 1. Microsoft Visual BASIC. I. Title. 005.133 Screen shot(s) reprinted by permission from Microsoft Corporation. Cover Photography and Design: © Dominique Moollan Printed by: Media Services Department Holmesglen Institute of TAFE Chadstone Campus PO Box 42 Holmesglen 3148 (03) 9564 1555 ii

Contents 1

Introduction…………………………………………...… 1

2

Basics………………………………………………....… 8 Program 1: First Visual Basic Program…………………………….. 12 Program 2: Simple Hangman Game……………………………...... 16 Program 3: Liquid Crystal Display…………………………………... 22

3

Using Timers, Numbers and Forms…………………..28 Program 4: Animated Cartoon Character………………….………..29 Program 5: The Maths Machine…………………………………….. 37 Program 6: Maths Machine Title Screen…………………………… 42

4

Images, Drive Controls and Scroll Bars…………...… 48 Program 7: Image Browser………………………………….………..50 Program 8: Image Browser with Zoom…………………….……….. 56

5

Shapes, Loops and Decisions………………………... 60 Program 9: Graphics Demonstration……………………………….. 64 Program 10: Sketch Program…………………………………….…..74

6

Different Forms of Input and Output…………………. 81 Program 11: Cats and Dogs…………………………………………. 85 Program 12: Currency Converter…………………………………… 93

7

Selecting from a List…………………………………… 99 Program 13: World Time Clock……………………………………… 102 Program 14: Hi-Score Table…………………………………………. 112

8

Animation and Making Games…………………..…… 120 Program 15: Balloon Race!............................................................ 123 Program 16: Catch the Bunny!....................................................... 130

9

Collision Detection……………………………………... 138 Program 17: Classic Pong…………………………………………… 142 Program 18: Meteor Dodge………………………………………….. 151

10

Control Arrays and Multimedia……………………….. 161 Program 19: Classic Snake…………………………………….……. 163 Program 20: Multimedia Player……………………………………… 176

11

Databases………………………………………………. 187 Tutorial 1: Movies Database…………………………………………. 193

12

SQL……………………………………………………… 198 Tutorial 2: Movies Database continued………………………….…. 200

iii

Appendices 1

ASCII Character Table..………….…………………… 203

2

Keycode Characters……………………………………205

3

Useful Mouse Pointer Constants …………………….207

4

Course Outline with CSFII Learning Outcomes (Victoria)………………………………….…208

5

Sample Assessment ………………………………….211

6

Sample Test Questions..……………………………….212

iv

Visual Basic For Education – Preview Document © Adrian Janson 2003

2.

Basics

Starting a Visual Basic program When the Visual Basic program is launched, it presents the user with the start-up screen shown below.

Figure 1: Visual Basic start-up screen

From this screen, it is possible to load an existing program or start a new one. As we are learning Visual Basic, we will limit ourselves to the ‘standard objects’. Later, when we begin to expand our skills, we will start to use some of the more advanced objects that are available. Select the ‘standard exe’ icon and click ‘open’.

Page

1

Visual Basic For Education – Preview Document © Adrian Janson 2003

Organising your work into directories As you work through this book, you will create a large number of programs. Each of these will consist of a number of different parts. In order to avoid confusion when starting a new program, create a separate directory in which you can save all of your work without the risk of over-writing other projects. Naming the directory after the name or number of the program will also help you to remember the purpose of the program when returning to it at a later time. An example of these directories is shown below.

Figure 2: Disk directory structure

The Visual Basic screen The following is a typical initial screen in Visual Basic.

Figure 3: Visual Basic screen

Page

2

Visual Basic For Education – Preview Document © Adrian Janson 2003

Down the left hand side of the screen is the ‘toolbox’. The ‘toolbox’ contains ‘objects’ that you can place in your application. The area marked ‘form1’ is the place in which you will start to write your first program. Notice that above this is an area marked ‘project1’. A ‘project’ is a collection of ‘forms’. It is possible to switch between ‘forms’ within a ‘project’. Most of the programs you will write in the beginning will only have one ‘form’, but as you learn to write more complex programs, you will create ‘projects’ with many ‘forms’. Along the top are control buttons and menus. To the right are the ‘properties’ and ‘project’ windows. The ‘properties’ window displays all the properties for the currently selected ‘object’. Each property can be changed by clicking the appropriate section and typing in a new value. The ‘project’ window displays all the ‘forms’ that have been created. Note that in this example, there is only one ‘project’ and one ‘form’.

The ‘toolbox’ Below you will find a description of the standard objects that appear in the Visual Basic ‘toolbox’. Each of these will be explained in depth as you work through this booklet.

Figure 4: The Visual Basic Toolbox

Pointer, Picture Box, Text Box Label, Frame, Command Button Check Box, Option Button, Combo Box List Box, Horizontal Scroll Bar, Vertical Scroll Bar Timer, Drive List Box, Directory List Box File List Box, Shape, Line Image, Data Control, OLE

Pointer:

Provides a way to move and resize ‘forms’ and ‘controls’.

Picture Box:

Displays bitmaps, icons or Windows Metafiles.

Text Box:

Provides an area to input or display text.

Label:

Displays text as a heading which the user cannot modify in the same way as they can the contents of a ‘text box’. Provides a visual and functional container for controls.

Frame:

Page

3

Visual Basic For Education – Preview Document © Adrian Janson 2003

Command Button: Check Box: Option Button: Combo Box: List Box: Horizontal Scroll: Vertical Scroll:

Timer: Drive List Box:

Can be programmed to carry out a command or a series of commands when it is clicked on. Returns a true/false value. Any number of ‘check boxes’ can be ‘checked’ at one time. Displays a number of choices within a ‘frame’ (or ‘form’) from which the user can select one. Combines a ‘text box’ with a ‘list box’. Allows a user to type in a selection or select an item from a drop-down list. Displays a list of items that a user can choose from. Allows a user to select a value within a range of values by sliding a box along a horizontal track. This control works in the same way as the ‘horizontal scroll bar’, except that the box slides up and down instead of left and right. These controls are very similar to the scroll bars that appear on the edges of program windows, but can be programmed to do many different things. Executes a command or series of commands at specified time intervals.

Shape:

Displays the currently selected disk drive and allows the user to select from the available choices. Displays the currently selected path and allows the user to select a new one. Displays a list of files in the currently selected directory and allows the user to select any one of these. Adds a rectangle, square, ellipse, or circle to a ‘form’.

Line:

Adds a straight-line segment to a ‘form’.

Image:

Displays bitmaps, icons, and Windows Metafiles but can also act as a ‘command button’ when clicked. Used to link Visual Basic to databases created in any external database program. Links files from external programs and applications into Visual Basic.

Directory List Box: File List Box:

Data Control: OLE:

Page

4

Visual Basic For Education – Preview Document © Adrian Janson 2003

Program 1: First Visual Basic program

Description To help you familiarise yourself with Visual Basic, we will create a simple welcome screen program. When the user presses a button, a message will be displayed. A rough sketch of the opening screen is shown below. When creating a program of your own, it is always good practice to first draw some rough screen designs. Doing so helps you work out what objects you will require, and how you intend to place them on the screen.

Algorithm of the solution This problem involves the following steps: Display the initial screen. When the user clicks the ‘Start’ button, display a welcome message. When the user clicks the ‘Quit’ button, end the program.

Defining objects The ‘form’ will be made up of four ‘objects’ – two ‘labels’ and two ‘command buttons’. Object Label1 Label2 Command1 Command2

Purpose Display the text ‘Visual Basic Program 1’. Display the text ‘Welcome to Visual Basic!’ Make ‘Label2’ appear on the screen. End the program.

Follow the instructions on the next page to place these ‘objects’ on a ‘form’. Page

5

Visual Basic For Education – Preview Document © Adrian Janson 2003

1. Start a new Visual Basic program. 2. Select the ‘label’ tool from the ‘toolbox’ and use it to draw two rectangular boxes in the center of the ‘form’ as shown in Figure 5.

Figure 5: Program Layout

3. Select the ‘command button’ tool from the ‘toolbox’ and use it to draw two rectangular ‘command buttons’ underneath the ‘labels’ that you have already drawn. The ‘form’ should now contain four ‘objects’ and look like the one above.

Assigning properties In order to customise the ‘objects’, you can assign different ‘properties’ to each of the ‘objects’ and to the ‘form’ itself. To do this, follow these steps: 1. Click on ‘form1’. Click on the ‘properties’ window, and set the ‘backColor’ property to dark blue. 2. Click on the ‘object’ marked ‘label1’. 3. Set the properties for ‘label1’ to the following: Name: BackColor: Caption: Font: Alignment:

lblMessage1 Light Blue Visual Basic Program 1 Times New Roman, size 28 2 - Center

4. Set the properties for the other three ‘objects’, as follows: Label 2 Name: BackColor: Caption: ForeColor: Font: Alignment:

lblMessage2 Light Blue Welcome to Visual Basic! Red Times New Roman, size 28 2 - Center

Visible:

False

Page

6

Visual Basic For Education – Preview Document © Adrian Janson 2003 Command 1 Name: Caption:

Command 2 cmdStart Start

Name: Caption:

cmdQuit Quit

Note: As a convention throughout this booklet, ‘objects’ will be named according to the type of ‘object’ they are, using the first three letters of their names. For example, ‘labels’ will all begin with ‘lbl’, ‘text boxes’ will all begin with ‘txt’ and ‘command buttons’ will all begin with ‘cmd’. What follows after these letters does not matter, but by using this convention it will be easier for us to keep track of the ‘objects’ on our ‘forms’.

Coding the events 1. Double click on the ‘start’ button and enter the following code: Private Sub cmdStart_Click() lblMessage2.Visible = True End Sub

Note that the two lines in italics do not need to be typed in, as they will automatically appear when you double click on any ‘object’. The purpose of this instruction is to make the second ‘label’ visible on pressing the ‘start’ button. 2. The ‘object drop down box’ can be found at the top of the code window on the left hand side. Clicking on the arrow will display a list of all of the ‘objects’ which have been created. From the ‘object drop down box’, select the ‘cmdQuit’ ‘command button’ and enter the code as shown. Private Sub cmdQuit_Click() End End Sub

The purpose of this instruction is to close, or terminate the program.

Saving your project It is always good practice at this stage of program development to save all of your code to disk. Create a directory on your disk with the name ‘program 1’ in which you can save your files. 1. From the ‘file’ menu select ‘save project as’. You will first be asked if you wish to save the changes to ‘form1’. This allows you to call up and use the ‘form’ again in a future application. Page

7

Visual Basic For Education – Preview Document © Adrian Janson 2003

2. Select the ‘program 1’ directory and type in the name ‘frmFirst’. Click on ‘save’. Note how the name of the ‘form’ fits with the convention that we are using to name the ‘objects’. 3. You will now be given the option of saving the ‘project’. Type in the name ‘firstProg’ and click on ‘save’.

Running your project To run your program, you can either select ‘run’ from the ‘run’ menu or click on the ‘play’ button on the ‘toolbar’ along the top of the screen. Run your program now and ensure that it operates correctly. On clicking the ‘start’ button, the ‘welcome’ message should be displayed. Click ‘quit’ to close the program.

Extensions •

Alter the code for ‘cmdStart’ so that when the button is pressed the first ‘label’ becomes invisible.



Alter the code for ‘cmdStart’ so that the button itself becomes invisible when pressed.



Alter the ‘properties’ of both the ‘form’ and the ‘label’ so that the ‘form’ is in red and the text is in yellow.

Page

8

Visual Basic For Education – Preview Document © Adrian Janson 2003

Program 2: Simple Hangman Game

Description We are now going to create a small program which can be used as part of a game of ‘Hangman’. When the program starts running, the user will be presented with a screen containing a series of buttons and a frame in which the ‘hangman’ drawing will appear. Each button will have number written on it which corresponds to the number of wrong guesses a player has made. There will also be a ‘Reset’ button which will clear the drawing and reset all of the buttons and a ‘Quit’ button. A screen sketch is shown below.

As the game progresses, the user clicks on the appropriate button and the next part of the ‘hangman’ will be drawn. Each button will only become available when the button before it has been clicked. Clicking on the ‘Quit’ button will end the program.

Algorithm of the solution The solution to this problem requires a number of steps, namely: Display the initial screen. When the user clicks any of the numbered buttons, display the next shape in the ‘hangman’ drawing. Enable the next button in the sequence. Has the last shape been displayed? Yes No Display “Lose” message.

Page

9

Visual Basic For Education – Preview Document © Adrian Janson 2003

When the user clicks the ‘Reset’ button, reset the program to its original state. When the user clicks the ‘Quit’ button, end the program.

Defining objects The program requires twelve ‘command buttons’ and two ‘labels’. The ‘hangman’ figure will be built up using the ‘shape’ and ‘line’ tools. Object Label1 Label2 Shapes 1-7 Lines 1-3 Commands 1-10 Command11 Command12

Purpose Display the text ‘Hangman!’ Display the text ‘You have lost!’ Rectangles in various positions forming the ‘hangman’ figure. The head will be represented by a circle. These will be used to form the legs and part of the stand. Make combinations of the shapes appear so that the ‘hangman’ gradually appears. Reset the game to the starting point. End the program.

Now follow these steps to create the program. 1. Start a new Visual Basic program. 2. Select the ‘label’ tool from the ‘toolbox’. Use it to draw a rectangular box in the center and at the top of ‘form1’ 3. Draw another ‘label’ of the same size just below the first. 4. Use the ‘command button’ tool to draw a total of twelve ‘command buttons’ as shown in Figure 6. Note that you can also copy and paste the ‘command buttons’ in place after drawing the first one on the ‘form’. However, if you do this, Visual Basic will ask you if you ‘wish to create a control array?’ When this happens, you should select the ‘no’ option, otherwise the names of the ‘command buttons’ will be changed in a way you may not expect and the program will not work correctly. ‘Control arrays’ are covered later in this book. 5. Use the ‘shape’ tool to create seven rectangular boxes as shown in Figure 6. These rectangles will make up most of the features of the ‘hangman’ figure. Note that at this stage, none of the rectangles have been shaded, and the head of the figure is a rectangle and not a circle. 6. Use the ‘line’ tool to draw lines for the legs of the ‘hangman’ figure and to form the triangle at the base.

Page 10

Visual Basic For Education – Preview Document © Adrian Janson 2003

Figure 6: Program Layout

Assigning properties Click on each ‘object’ in turn and set the following ‘properties’: Command 1

Command 2

Command 3

Name: cmdOne Caption: 1 Command 4

Name: cmdTwo Caption: 2 Enabled: False Command 5

Name: cmdThree Caption: 3 Enabled: False Command 6

Name: cmdFour Caption: 4 Enabled: False Command 7

Name: cmdFive Caption: 5 Enabled: False Command 8

Name: cmdSix Caption: 6 Enabled: False Command 9

Name: cmdSeven Caption: 7 Enabled: False

Name: cmdEight Caption: 8 Enabled: False

Name: cmdNine Caption: 9 Enabled: False

Page 11

Visual Basic For Education – Preview Document © Adrian Janson 2003 Command 10

Command 11

Command 12

Name: cmdTen Caption: 10 Enabled: False Label 1

Name: cmdReset Caption: Reset

Name: cmdQuit Caption: Quit

Name: Alignment: Caption: Font: ForeColor:

lblTitle 2 - Center HangMan! Arial, Bold, size 18 Blue

Name: Alignment: Caption: Font: ForeColor: Visible: Shape 7

lblLose 2 - Center You have lost! Arial, Bold, size 18 Red False

see below Red 0 - Solid False

Name: FillColor: FillStyle: Visible: Shape:

see below Red 0 - Solid False 3 - Circle

Shapes (1-6) Names: FillColor: FillStyle: Visible:

Label 2

Lines (1-3) Names: see below BorderColor: Red BorderWidth: 5 Visible: False

The names of the shapes and lines: Usually we would name all of the ‘shape’ and ‘line’ objects using the prefix associated with that ‘object’ type. For example, for a ‘shape’ object we would use the prefix ‘shp’ and for a ‘line’ ‘object’ we would use the prefix ‘lin’. However, in a program such as this, it is suggested that you name the ‘shapes’ and ‘lines’ according to their numbered position on the figure. For example, the circle ‘shape’ which is being used for the head could be given the name ’Hangman6’ and the one on the right hand side forming the stand could be called ‘Hangman2’.

Hint:: When you have a number of ‘objects’ that have to be assigned the same ‘properties’, you can do this at the same time by highlighting all of the ‘objects’ and then setting the particular ‘properties’. Selecting a group of ‘objects’ can be done by clicking on each ‘object’ with the ‘ctrl’ key held down, or using the pointer from the ‘toolbox’ to draw a large box around all of the ‘objects’.

Page 12

Visual Basic For Education – Preview Document © Adrian Janson 2003

Coding the events Now we have set the ‘properties’ for each of the ‘objects’. However, we still need to provide code for the ‘command buttons’ to get the program to work. 1. Double click on the button ‘1’ and enter the following lines of code. Private Sub cmdOne_Click() Hangman1.Visible = True cmdTwo.Enabled = True End Sub

The first line of code changes the ‘visible’ property of the first ‘shape’ to ‘true’ so that it appears on the screen. The next line of code enables the second ‘command button’ so that the user may click on it when they are ready. When an ‘object’ has its ‘enabled’ property set to ‘false’ it cannot be interacted with in any way. 2. From the ‘object drop down box’ select each of the ten ‘command buttons’ that will be used to display the ‘hangman’ figure in turn. Type in code similar to that shown above, making sure that you make the next part of the ‘hangman’ figure visible and you enable the next ‘command button’ in the sequence. 3. When you have reached the last ‘command button’ in the sequence, there are no more ‘command buttons’ to enable. Type in the code below which will display the ‘You have lost!’ message. Private Sub cmdTen_Click() Hangman10.Visible = True lblLose.Visible = True End Sub

4. From the ‘object drop down box’, select ‘cmdQuit’ and type the following. Private Sub cmdQuit_Click() End End Sub

5. From the ‘object drop down box’, select ‘cmdReset’. Type in code which will change the ‘visible properties’ of all of the ‘hangman’ ‘shapes’ to ‘false’. Also type in code which will set the ‘enabled property’ of all of the ‘command buttons’ used to display the ‘hangman’ (except ‘cmdOne’) back to ‘false’.

Page 13

Visual Basic For Education – Preview Document © Adrian Janson 2003

Saving your project 1. From the ‘file’ menu select ‘save project as’. 2. Select the ‘program 2’ directory and type in the name ‘frmHang’. Click on ‘save’. 3. You will now be given the option of saving the project. Type in the name ‘hangman’ and click on ‘save’.

Running your project Run your program now and ensure that it operates correctly. Each of the parts of the ‘hangman’ figure should appear in the correct order and the buttons should be enabled at the right time. Ensure that your ‘reset’ button operates in the correct way. Click ‘quit’ to close the program.

Extensions •

Create an alternative set of buttons for each of the parts of the ‘hangman’ character, so that each button makes shapes of different size or colour appear. This way, a user could choose which of the buttons to press and create a different looking ‘hangman’ each time the program is run.



Create a mini game program using a large number of ‘command buttons’ on the ‘form’. Some ‘command buttons’ should make the letters of a “You Win” banner appear, while the buttons that make the different parts of the hangman appear, should be placed in random locations. The user presses buttons randomly and tries to make the winning message appear before all the parts of the hangman have been revealed.

Page 14

Visual Basic For Education – Preview Document © Adrian Janson 2003

Program 3: Liquid Crystal Display

Description Using a similar technique to the last program, we are now going to create a small program to model a liquid crystal display such as those used in calculators and electronic scoreboards. When the program starts running, the user will be presented with a screen containing the heading ‘liquid crystal display’ and a total of eleven buttons. Ten of these buttons will have digits on them and the eleventh one will be a ‘quit’ button. A screen sketch is shown below.

Clicking on the ‘quit’ button will end the program, but clicking on any of the digit buttons will cause a number of rectangular shapes to become visible in the shape of the selected digit. When the program starts, all of these shapes should be invisible.

Algorithm of the solution The solution to this problem requires a number of steps, namely: Display the initial screen. When the user clicks any of the digit buttons, display the required combination of shapes to form that digit. When the user clicks the ‘Quit’ button, end the program.

Defining objects The program requires eleven ‘command buttons’ and one ‘label’. The display digits will be built up using the ‘shape’ tool.

Page 15

Visual Basic For Education – Preview Document © Adrian Janson 2003

Object Label1 Shapes 1-8 Commands 1-10 Command11

Purpose Display the text ‘LCD Digit Demo’. Rectangles in various positions forming the elements of a LCD digit. Make combinations of the shapes appear so that the required digit is displayed. End the program.

Now follow these steps to create the program. 1. Start a new Visual Basic program. 2. Select the ‘label’ tool from the ‘toolbox’. Use it to draw a rectangular box in the center and at the top of ‘form1’ 3. Use the ‘command button’ tool to draw a total of eleven ‘command buttons’ as shown in Figure 7 below.

Figure 7: Program Layout

4. Use the ‘shape’ tool to create seven rectangular boxes as shown in Figure 7. These rectangles will make up our LCD digit.

Page 16

Visual Basic For Education – Preview Document © Adrian Janson 2003

Assigning properties Click on each ‘object’ in turn and set the following ‘properties’: Command 1

Command 2

Command 3

Name: cmdOne Caption: 1 Command 4

Name: cmdTwo Caption: 2 Command 5

Name: cmdThree Caption: 3 Command 6

Name: cmdFour Caption: 4 Command 7

Name: cmdFive Caption: 5 Command 8

Name: cmdSix Caption: 6 Command 9

Name: cmdSeven Caption: 7 Command 10

Name: cmdEight Name: cmdNine Caption: 8 Caption: 9 Command 11

Name: cmdTen Caption: 0 Label 1

Name: cmdQuit Caption: Quit Shapes (1-8)

Name: Alignment: Caption: Font: ForeColor:

Names: FillColor: FillStyle: Visible:

lblTitle 2 - Center HangMan! Arial, Bold, size 18 Blue

see below Red 0 - Solid False

The names of the shapes The ‘shapes’ should all be given the prefix ‘shp’ as per our naming convention. It is suggested that you name the ‘shapes’ according to their position on the digit. For example, the top rectangle could be given the name ‘shpTop’ and the one on the left hand side top could be called ‘shpTopLeft’.

Coding the events Now we have set the ‘properties’ for each of the ‘objects’. However, we still need to provide code for the ‘command buttons’ to get the program to work. 1. Double click on the button ‘1’ and enter the following lines of code.

Page 17

Visual Basic For Education – Preview Document © Adrian Janson 2003 Private Sub cmdOne_Click() shpTopRight.Visible = True shpBottomRight.Visible = True shpBottom.Visible = False shpBottomLeft.Visible = False shpTopLeft.Visible = False shpTop.Visible = False shpMiddle.Visible = False End Sub

These lines make the two rectangular shapes on the right hand side visible so that the digit ‘1’ appears and ensures that all of the other ‘shapes’ are not visible. If the user has already displayed one of the other digits, some of the other ‘shapes’ may have already been made visible. For this reason, if the ‘shapes’ that are not being used are not made invisible, they may interfere with our display. 2. From the ‘object drop down box’ select each of the other ‘command buttons’ in turn and set the ‘shapes’ to either visible or invisible to form the required digit. 3. From the ‘object drop down box’, select ‘cmdQuit’ and type the following code. Private Sub cmdQuit_Click() End End Sub

Saving your project 1. From the ‘file’ menu select ‘save project as’. 2. Select the ‘program 3’ directory and type in the name ‘frmLCD’. Click on ‘save’. 3. You will now be given the option of saving the project. Type in the name ‘LcdDemo’ and click on ‘save’.

Running your project Run your program now and ensure that it operates correctly. On clicking each of the digit buttons, the correct LCD digit should be displayed. Click ‘quit’ to close the program.

Extensions •

Write a program using the same technique which allows the user to ‘Build a Funny Face!’. Use ‘shapes’ to create the features of a face and then create ‘command buttons’ which make separate features visible. Use the ‘shape’ property of each of the ‘shapes’ to make the face as interesting as possible. Add additional features to your face such as hair, eyebrows or a hat. Try to think of features that are both original and interesting. Page 18

Visual Basic For Education – Preview Document © Adrian Janson 2003



Write a program which consists of a number of disabled ‘command buttons’. The user clicks on buttons randomly which in turn enable other buttons. The goal is to enable a button which will display a winning message when clicked. Some buttons may begin invisible while others may begin disabled.

Page 19

Visual Basic For Education – Preview Document © Adrian Janson 2003

Context questions and activities 1. Using any ‘object’ you have used so far as an example, describe the difference between the ‘name’ and the ‘caption’ properties. 2. Load any of the three programs you have just written and examine the list of ‘properties’ available for the ‘form’. Experiment with the values of the ‘properties’ called ‘StartUpPosition’ and ‘WindowState’. Describe the effect changing either of these ‘properties’ has on a ‘form’. 3. Describe what would happen if two Visual Basic programs were saved in the same location and the user had not renamed any of the ‘forms’ that had been used. 4. Describe two reasons why it is good programming practice to include a screen sketch as part of the design process in creating a new Visual Basic program.

Page 20