Programming with Microsoft Visual Basic 6.0 – Enhanced Edition

Answers to Questions and Exercises

Table of Contents

Tutorial 1, Lesson APage 1

Tutorial 1, Lesson BPage 4

Tutorial 1, Lesson CPage 7

Tutorial 1, DebuggingPage 17

Tutorial 2, Lesson APage 20

Tutorial 2, Lesson BPage 24

Tutorial 2, Lesson CPage 26

Tutorial 2, DebuggingPage 74

Tutorial 3, Lesson APage 75

Tutorial 3, Lesson BPage 76

Tutorial 3, Lesson CPage 82

Tutorial 3, DebuggingPage 93

Tutorial 4, Lesson APage 95

Tutorial 4, Lesson BPage 106

Tutorial 4, Lesson CPage 109

Tutorial 4, DebuggingPage 113

Tutorial 5, Lesson APage 116

Tutorial 5, Lesson BPage 122

Tutorial 5, Lesson CPage 126

Tutorial 5, DebuggingPage 134

Tutorial 6, Lesson APage 135

Tutorial 6, Lesson BPage 156

Tutorial 6, Lesson CPage 161

Tutorial 6, DebuggingPage 174

Tutorial 7, Lesson APage 175

Tutorial 7, Lesson BPage 176

Tutorial 7, Lesson CPage 179

Tutorial 7, DebuggingPage 194

Tutorial 8, Lesson APage 196

Tutorial 8, Lesson BPage 203

Tutorial 8, Lesson CPage 213

Tutorial 8, DebuggingPage 227

Tutorial 9, Lesson APage 230

Tutorial 9, Lesson BPage 234

Tutorial 9, Lesson CPage 244

Tutorial 9, DebuggingPage 251

Tutorial 10, Lesson APage 252

Tutorial 10, Lesson BPage 274

Tutorial 10, Lesson CPage 283

Tutorial 10, DebuggingPage 292

Tutorial 11, Lesson APage 293

Tutorial 11, Lesson BPage 294

Tutorial 12, Lesson APage 297

Tutorial 12, Lesson BPage 299

Tutorial 1, Lesson A

Questions

  1. b form
  2. b toolbox
  3. d Properties
  4. e .frm
  5. e .vbp
  6. a BackColor
  7. a Caption
  8. c Caption
  9. d Name
  10. d Name
  11. H Form windowI Properties List

DMain windowA Properties window

CMenu barJ Settings box

EObject boxB Toolbar

GProject windowF Toolbox window

  1. A form’s Caption property controls the text that appears in the form’s title bar and on the application’s button on the Windows 95 taskbar when the application is running. The Name property is used by the programmer to refer to an object in code. The user sees the Caption property; the programmer uses the Name property.
  2. To open a new project, click File on the menu bar, then click New Project; or press Ctrl+N. When the New Project dialog box appears, click Standard EXE, and then click the OK button.
  3. To open an existing project, click the Open Project button on the Standard toolbar. You also can press Ctrl+O; or click File, then click Open Project. Select the appropriate drive and folder. Click the project name in the list of file names, and then click the Open button.
  4. To display the Project window, click the Project Explorer button on the Standard toolbar. You also can click View, then click Project Explorer; or press Ctrl+R. To display the Properties window, click the Properties Window button on the Standard toolbar. You also can press the F4 key; or click View, and then click Properties Window; or right-click an object and then click Properties.

Exercises

  1. Text shaded gray indicates specific application requirements to note. The student should submit a printout of the Form As Text.

VERSION 5.00
Begin VB.Form frmCharity
BackColor = &H00800000&
Caption = "Charities Unlimited"
ClientHeight = 4140
ClientLeft = 1035
ClientTop = 1515
ClientWidth = 8220
LinkTopic = "Form1"
PaletteMode = 1 'UseZOrder
ScaleHeight = 4140
ScaleWidth = 8220
Begin VB.Label Label1
BackColor = &H00FFFF80&
Caption = "Please help Charities Unlimited help others." / BeginProperty Font
Name = "MS Sans Serif"
Size = 18
Charset = 0
Weight = 700
Underline = 0 'False
Italic = -1 'True
Strikethrough = 0 'False
EndProperty
Height = 1935
Left = 120
TabIndex = 0
Top = 720
Width = 7935
End
End
  1. Text shaded gray indicates specific application requirements to note. The student should submit a printout of the Form As Text.

VERSION 5.00
Begin VB.Form frmPhotos
BackColor = &H000000FF&
Caption = "Photos Inc."
ClientHeight = 4140
ClientLeft = 1200
ClientTop = 1515
ClientWidth = 6690
LinkTopic = "Form1"
PaletteMode = 1 'UseZOrder
ScaleHeight = 4140
ScaleWidth = 6690 / Begin VB.Image Image1
Height = 3495
Left = 480
Picture = "La2.frx":0000
Stretch = -1 'True
Top = 360
Width = 5775
End
End

3.ACaptionBForeColor

BEnabledCIcon

BFillColorALeft

BFillStyleAName

CFontCPicture

4. (Discovery)

  1. Debug toolbar buttons: Start, Break, End, Toggle Breakpoint, Step Into, Step Over, Step Out, Locals Window, Immediate Window, WatchWindow, Quick Watch, Call Stack
  2. Edit toolbar button: List Properties/Methods, List Constants, Quick Info, Parameter Info, Complete Word, Indent, Outdent, Toggle Breakpoint, Comment Block, Uncomment Block, Toggle Bookmark, Next Bookmark, Previous Bookmark, Clear All Bookmarks
  3. Form Editor toolbar buttons: Bring To Front, Send To Back, Align (Lefts, Centers, Rights, Tops, Middles, Bottoms, to Grid), Center (Horizontally, Vertically), Make Same Size (Width, Height, Both), Lock Controls Toggle
  4. Customize tabs: The Toolbars tab allows you to create, rename, delete, and reset your toolbars. The Commands tab contains commands you can drag to your menus and toolbars. The Options tab allows you to change the size of your toolbar buttons, to show ScreenTips (ToolTips) and shortcut keys, and to add animation to your menus.
  1. (Discovery)

BorderStyle property: Returns/sets the border style for an object.

Icon property: Returns the icon displayed when a form is minimized at run time.

MaxButton property: Determines whether a form has a Maximize button.

WindowState property: Returns/sets the visual state of a form window at run time. The three values of the WindowState property are 0-Normal, 1-Minimized, and 2-Maximized.

1

Tutorial 1, Lesson B

Questions

  1. a F1
  2. d Label
  3. d twips
  4. c points
  5. e AutoSize
  6. a Form
  7. a) To select multiple controls, click the first control you want to select in the form and then press and hold down the Control (Ctrl) key as you click the other controls you want to select. b) To deselect one of the selected controls, press and hold down the Ctrl key as you click the control. c) To deselect all of the selected controls, click the form or any unselected control in the form.
  8. To size a control, first select the control, and then position the mouse pointer on one of the sizing handles and drag until the control is the desired size. To move a control, position the mouse pointer on the control, being careful not to position the pointer on any of the sizing handles, and then drag the control to its new location. To delete a control, select the control, and then press the Delete key. In order to delete a control, the Form window must be the active window.
  9. To save an existing form and an existing project under a different name, click File on the menu bar, then click Save <filename> As to save the form. Enter the new name for the form in the File name text box, select the appropriate drive and folder in the Save in list box, and then click the Save button. Click File on the menu bar, and then click Save Project As to save the project. Enter the new name for the project in the File name text box, select the appropriate drive and folder in the Save in list box, and then click the Save button.

Exercises

  1. lb1Done project—Text shaded gray indicates specific application requirements to note. The student should submit a printout of the Form As Text.

VERSION 5.00
Begin VB.Form frmIMA
BackColor = &H00FFFFFF&
Caption = "IMA"
ClientHeight = 3915
ClientLeft = 1155
ClientTop = 1545
ClientWidth = 6585
LinkTopic = "Form1"
ScaleHeight = 3915
ScaleWidth = 6585
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "International Mechanics Association"
BeginProperty Font
Name = "MS Sans Serif"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty / Height = 300
Left = 795
TabIndex = 0
Top = 1000
Width = 4365
End
End
  1. lb2Done project—Text shaded gray indicates specific application requirements to note. The student should submit a printout of the Form Image and the Form As Text.
VERSION 5.00
Begin VB.Form frmJefferson
BackColor = &H00FFFFC0&
Caption = "Jefferson Cleaning Inc."
ClientHeight = 3915
ClientLeft = 1440
ClientTop = 1515
ClientWidth = 6585
LinkTopic = "Form1"
ScaleHeight = 3915
ScaleWidth = 6585
Begin VB.Label Label2
AutoSize = -1 'True
BackColor = &H00FFFFFF&
Caption = "Jefferson Cleaning Inc."
BeginProperty Font
Name = "Arial"
Size = 24
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H000000FF&
Height = 540
Left = 960
TabIndex = 1
Top = 1920
Width = 4890
End
Begin VB.Label Label1
AutoSize = -1 'True
BackColor = &H00FFFFFF&
Caption = "Welcome To" / BeginProperty Font
Name = "Arial"
Size = 24
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H000000FF&
Height = 540
Left = 2055
TabIndex = 0
Top = 1200
Width = 2670
End
End
  1. lb3Done project—In this exercise, the student is allowed to choose the form’s name and color, as well as the color, font type, style, and size for the two label controls. The shaded text indicates where these changes would appear. The student should submit a printout of the Form Image and the Form As Text.
VERSION 5.00
Begin VB.Form frmYorktown
BackColor = &H0080FFFF&
Caption = "Yorktown Shopping Center"
ClientHeight = 3915
ClientLeft = 1155
ClientTop = 1545
ClientWidth = 6585
LinkTopic = "Form1"
ScaleHeight = 3915
ScaleWidth = 6585
Begin VB.Label Label2
AutoSize = -1 'True
BackColor = &H0080FFFF&
Caption = "Your one-stop shopping center"
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = -1 'True
Strikethrough = 0 'False
EndProperty
Height = 270
Left = 1560
TabIndex = 1
Top = 2040
Width = 3480
End / Begin VB.Label Label1
AutoSize = -1 'True
BackColor = &H0080FFFF&
Caption = "The Yorktown Mall of Kentucky"
BeginProperty Font
Name = "Arial"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 285
Left = 1530
TabIndex = 0
Top = 1440
Width = 3540
End
End
  1. (Discovery) The student should submit handwritten answers to steps f and g.

Step f: To display Help screens that contain the phrase msdn library, enclose the phrase—in this case, “msdn library”—in quotation marks.

Step g: To create a list of favorite topics, click the Contents, Index, or Search tab and open a topic you want to make a favorite topic. Click the Favorites tab. The topic that you opened will be displayed in the Current topic area. Click Add to add the topic to your favorites list. (Note: If you want to rename the displayed topic, type the new name in the Current topic area and then click Add.) To return to a favorite topic, highlight the topic and then click Display. You also can double-click the topic name. To remove a favorite topic, select the topic and then click Remove.

  1. (Discovery) In this exercise, the student learns about the Align and Make Same Size commands on Visual Basic’s Format menu. The student is not instructed to submit anything for this exercise.

1

Tutorial 1, Lesson C

Questions

  1. e Picture
  2. b End
  3. b Form As Text
  4. a Code
  5. b events
  6. c event procedures
  7. e syntax
  8. e keyword
  9. a BorderStyle
  10. e Selection
  11. a Current Module
  12. c Form As Text
  13. To place an existing control inside a frame, cut the existing control from the form, then select the frame, and then paste the control inside the frame.
  14. To draw a control on either a form or a frame control, click the appropriate tool in the toolbox. Position the crosshair on the form or inside the frame, and then drag until the control is the desired size.
  15. When the Procedure View button is selected, each procedure appears in its own Code window. When the Full Module View button is selected, the application’s code appears as a single listing.
  16. You open an object’s Code window by double-clicking the object.
  17. An event procedure is a block of code that tells an object how to respond to an event (such as clicking, double-clicking, and so on).
  18. The Code window’s Object box allows you to choose either the form or one of the controls on the form. The Code window’s Procedure box lists the event procedures to which the selected object is capable of responding.
  19. Syntax is the rules of a programming language.
  20. The File menu’s Make <projectname.exe> command saves a project’s compiled code in a separate file. You would use this command to create a file that can be run outside of Visual Basic, on any computer system that is running Microsoft Windows and has the msvbvm60.dll file in its Windows\System folder.

Exercises

  1. lc1Done project—Text shaded gray indicates specific application requirements to note. The student should submit a printout of the Form Image and the Form As Text, as well as an executable file. (The student cannot use the Working Model to make an executable file.)
VERSION 5.00
Begin VB.Form frmAnimal
BackColor = &H00FFFFFF&
Caption = "Peoria Animal Shelter"
ClientHeight = 5130
ClientLeft = 1155
ClientTop = 1545
ClientWidth = 6585
LinkTopic = "Form1"
ScaleHeight = 5130
ScaleWidth = 6585
Begin VB.Frame Frame1
Caption = "Click the logo"
BeginProperty Font
Name = "MS Sans Serif"
Size = 12
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 4935
Left = 120
TabIndex = 0
Top = 120
Width = 6375
Begin VB.CommandButton cmdExit
Caption = "Exit"
BeginProperty Font
Name = "MS Sans Serif"
Size = 12
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 495
Left = 5280
TabIndex = 2
Top = 4320
Width = 855
End / Begin VB.Image imgLogo
Height = 2820
Left = 840
Picture = "lc1Done.frx":0000
Stretch = -1 'True
Top = 1080
Width = 4755
End
Begin VB.Label lblTitle
AutoSize = -1 'True
Caption = "Welcome to the Peoria Animal Shelter"
BeginProperty Font
Name = "MS Sans Serif"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 300
Left = 840
TabIndex = 1
Top = 600
Visible = 0 'False
Width = 4560
End
End
End
Code:
Private Sub cmdExit_Click()
End
End Sub
Private Sub imgLogo_Click()
lblTitle.Visible = True
End Sub
  1. (Discovery) In this exercise, the student designs his or her own user interface. The interface should include an Exit button that will end the application. It also should include one or more label controls, image controls, and frame controls, as well as the Scroller control (assuming the student is not using the Working Model). The image control(s) can contain either the Interlocking picture used in the tutorial, or it can contain one of Visual Basic’s icons. Object that will either be coded or used in code should be named. The color and font types, styles, and sizes will vary. The student should submit an executable file and printouts of the form, properties, and code. (The student will not be able to use the Working Model to make an executable file.)
  1. (Discovery) lc3Done project—Text shaded gray indicates specific application requirements to note. (The control names may differ.) The student should submit a printout of the Form As Text and code.

VERSION 5.00
Begin VB.Form frmOnOff
Caption = "On and Off"
ClientHeight = 2940
ClientLeft = 2415
ClientTop = 1560
ClientWidth = 4440
LinkTopic = "Form1"
ScaleHeight = 2940
ScaleWidth = 4440
Begin VB.CommandButton cmdExit
Caption = "Exit"
Height = 495
Left = 2880
TabIndex = 2
Top = 2040
Width = 1215
End
Begin VB.CommandButton cmdOff
Caption = "Off"
Height = 495
Left = 1560
TabIndex = 1
Top = 2040
Width = 1215
End
Begin VB.CommandButton cmdOn
Caption = "On"
Height = 495
Left = 240
TabIndex = 0
Top = 2040
Width = 1215
End / Begin VB.Image imgOnOff
Height = 1410
Left = 720
Picture = "lc3Done.frx":0000
Stretch = -1 'True
Top = 240
Width = 2955
End
End
Code:
Private Sub cmdExit_Click()
End
End Sub
Private Sub cmdOff_Click()
imgOnOff.Visible = False
End Sub
Private Sub cmdOn_Click()
imgOnOff.Visible = True
End Sub
  1. (Discovery) In this exercise, the student uses Visual Basic’s Package and Deployment Wizard to create setup disks for the lc1Done project. To use the Package and Deployment Wizard, the student must have saved and compiled the lc1Done project. The student will not be able to create the compiled (executable) project with the Working Model. The student is instructed to submit the setup disks.
  2. (Discovery) In this exercise, the student learns how to set the attributes for the lcCopy.exe file created in Lesson C. The student will not be able to complete this exercise with the Working Model. The student is not instructed to submit anything for this exercise.
  3. (Discovery) In this exercise, the student learns how to step through an application’s code. The student is not instructed to submit anything for this exercise.
  4. (Discovery) In this exercise, the student learns how to use the line continuation character. The student is not instructed to submit anything for this exercise.
  5. (Discovery) lc8Splas and lc8About projects. In this exercise, the student uses Visual Basic’s Application Wizard to create a splash screen and an About box. The student should submit the files for this exercise.
  6. (Discovery) In this exercise, the student learns how to create a ToolTip (Ends the application) for the Exit button in the lcCopy project created in Lesson C. The student is not instructed to submit anything for this exercise.
  7. (Discovery) In this exercise, the student learns how to display a picture (graphic) in the lcCopy project’s Exit button. The student uses the command button’s Style and DownPicture properties. The student is not instructed to submit anything for this exercise.

1

Tutorial 1 – Debugging

Technique

The simplest technique a programmer uses for debugging an application is to print the application’s properties (Form As Text) and code. In the printout of the properties, the programmer looks for a property that is set incorrectly. In the code printout, the programmer looks for an instruction that is either in the wrong object’s Code window or in the wrong event procedure.

Exercises

  1. Text shaded gray indicates specific application requirements to note. For the lblTitle control, the student may change either the AutoSize property or the Width property. The student should submit the Form As Text with the corrections circled.

VERSION 5.00
Begin VB.Form frmGlamour
Appearance = 0 'Flat
BackColor = &H00C0C0C0&
Caption = "Glamour-Us"
ClientHeight = 4320
ClientLeft = 1275
ClientTop = 1455
ClientWidth = 7485
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H80000008&
LinkTopic = "Form1"
MaxButton = 0 'False
PaletteMode = 1 'UseZOrder
ScaleHeight = 4320
ScaleWidth = 7485 / Begin VB.Label LblTitle
Appearance = 0 'Flat
AutoSize = -1 'True
BackColor = &H80000005&
BackStyle = 0 'Transparent
Caption = "Glamour-Us Beauty Salon"
BeginProperty Font
Name = "MS Sans Serif"
Size = 24
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H80000008&
Height = 555
Left = 720
TabIndex = 0
Top = 1200
Width = 5925
End
End
  1. In this exercise, the student learns how to remove excess code from an application. The student should submit two printouts of the code.

Code from step h:
Private Sub Command1_Click()
End
End Sub
Private Sub cmdExit_Click()
End
End Sub /

Code from step k:

Private Sub cmdExit_Click()
End
End Sub
  1. Text shaded gray indicates specific application requirements to note. The student should submit a printout of the code with the correction circled.

Private Sub cmdDisplay_Click()
lblMessage.Caption = "OK"
End Sub / Private Sub cmdExit_Click()
End
End Sub
  1. Text shaded gray indicates specific application requirements to note. The student should submit a printout of the code and Form As Text from step a, and a printout of the code and Form As Text from step f with the corrections circled.

Step a
VERSION 5.00
Begin VB.Form frmDebug4
Appearance = 0 'Flat
BackColor = &H00C0C0C0&
BorderStyle = 1 'Fixed Single
Caption = "Debugging Exercise 4"
ClientHeight = 2160
ClientLeft = 1845
ClientTop = 2175
ClientWidth = 5355
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H80000008&
LinkTopic = "Form1"
MaxButton = 0 'False
PaletteMode = 1 'UseZOrder
ScaleHeight = 2160
ScaleWidth = 5355
Begin VB.CommandButton cmdExit
Appearance = 0 'Flat
BackColor = &H00C0C0C0&
Caption = "Command1"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 495
Left = 3840
TabIndex = 1
Top = 960
Width = 1215
End / Begin VB.Label lblNote
Appearance = 0 'Flat
AutoSize = -1 'True
BackColor = &H00C0C0C0&
BackStyle = 0 'Transparent
Caption = "The command button should end the application:"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H80000008&
Height = 195
Left = 240
TabIndex = 0
Top = 1080
Width = 3465
End
End
Code:
Private Sub cmdExit_DragDrop(Source As Control, X As Single, Y As Single)
End
End Sub
Step f
VERSION 5.00
Begin VB.Form frmDebug4
Appearance = 0 'Flat
BackColor = &H00C0C0C0&
BorderStyle = 1 'Fixed Single
Caption = "Debugging Exercise 4"
ClientHeight = 2160
ClientLeft = 1845
ClientTop = 2175
ClientWidth = 5355
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H80000008&
LinkTopic = "Form1"
MaxButton = 0 'False
PaletteMode = 1 'UseZOrder
ScaleHeight = 2160
ScaleWidth = 5355
Begin VB.CommandButton cmdExit
Appearance = 0 'Flat
BackColor = &H00C0C0C0&
Caption = "Exit"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 495
Left = 3840
TabIndex = 1
Top = 960
Width = 1215
End / Begin VB.Label lblNote
Appearance = 0 'Flat
AutoSize = -1 'True
BackColor = &H00C0C0C0&
BackStyle = 0 'Transparent
Caption = "The command button should end the application:"
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H80000008&
Height = 195
Left = 240
TabIndex = 0
Top = 1080
Width = 3465
End
End
Code:
Private Sub cmdExit_Click()
End
End Sub

Tutorial 2, Lesson A