4/20/2004 ppcc - introduction to vb6 copyright ©2004, tore bostrup 1 introduction to vb6 week 8

60
4/20/2004 4/20/2004 PPCC - Introduction to PPCC - Introduction to VB6 VB6 Copyright ©2004, Tore Copyright ©2004, Tore Bostrup Bostrup 1 Introduction to VB6 Introduction to VB6 Week 8 Week 8

Upload: samuel-robertson

Post on 25-Dec-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright ©2004, Tore BostrupCopyright ©2004, Tore Bostrup 11

Introduction to VB6Introduction to VB6

Week 8Week 8

Page 2: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 22

The End of the RoadThe End of the Road

Today, we will briefly reviewToday, we will briefly review String Manipulation ExerciseString Manipulation Exercise More ControlsMore Controls ContainersContainers Control Array ExerciseControl Array Exercise

Page 3: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 33

The End of the RoadThe End of the Road

We will addressWe will address Variable ScopeVariable Scope Using more than one formUsing more than one form A Graphics Viewer ExampleA Graphics Viewer Example LoopsLoops Custom Controls and Object ReferencesCustom Controls and Object References

FileSystemObjectFileSystemObject Rich Text BoxRich Text Box

Debugging CodeDebugging Code Error HandlingError Handling

Page 4: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 44

Review: String Manipulation Review: String Manipulation ExerciseExercise

You have received a mailing list with You have received a mailing list with names entered in different ways.names entered in different ways.

There are no middle names in the list, but There are no middle names in the list, but some names include titles, etc.some names include titles, etc. The titles may be:The titles may be:

"Mr.", "Mrs.", "Ms." at the beginning of the string"Mr.", "Mrs.", "Ms." at the beginning of the string ", Jr." or ", Sr." after the name", Jr." or ", Sr." after the name

Names may be entered asNames may be entered as [Title] First Last[Title][Title] First Last[Title] [Title] Last, First[Title][Title] Last, First[Title]

Page 5: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 55

Review: String Manipulation Review: String Manipulation ExerciseExercise

Our job is to write a subroutine that receives Our job is to write a subroutine that receives the name string as one parameter, and outputs the name string as one parameter, and outputs the name into four separate TextBoxesthe name into four separate TextBoxes txtPrefix, txtFirst, txtLast, txtSuffixtxtPrefix, txtFirst, txtLast, txtSuffix

We also need to create a "test harness" for the We also need to create a "test harness" for the subroutine subroutine In this case, a project with a form that allows us to In this case, a project with a form that allows us to

type in an arbitrary name entry, calls the subroutine, type in an arbitrary name entry, calls the subroutine, and contains the TextBoxes for the output.and contains the TextBoxes for the output.

Page 6: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 66

Review: String Manipulation Review: String Manipulation ExerciseExercise

Questions?Questions? Solutions?Solutions?

Label1(0)

txtFullName

cmdSplitName

txtPrefix txtFirstName txtLastName txtSuffix

Page 7: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 77

Review: More controls (Option Review: More controls (Option Button, CheckBox, Frame)Button, CheckBox, Frame)

Look at a typical Look at a typical Print dialog box:Print dialog box:

Checkboxes

Frames

Option Buttons

Page 8: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 88

Review: ContainersReview: Containers

Containers are controls (or windows) that Containers are controls (or windows) that can contain other controls (members)can contain other controls (members) Place a control on a container, and move the Place a control on a container, and move the

container – the member controls follow the container – the member controls follow the container.container.

Container Controls:Container Controls: PictureBoxPictureBox FrameFrame Tabbed DialogTabbed Dialog CoolBarCoolBar

Page 9: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 99

Review: Control Array Review: Control Array ExerciseExercise

Create a New ProjectCreate a New Project Add a Label, a TextBox and a Add a Label, a TextBox and a

CommandButtonCommandButton Label Name: "Label1", Caption: Label Name: "Label1", Caption:

"Character""Character" TextBox Name: "txtChar", Text: ""TextBox Name: "txtChar", Text: "" CommandButton Name: "cmdIsBitSet", CommandButton Name: "cmdIsBitSet",

Caption: "Is Bit n Set?"Caption: "Is Bit n Set?"

Page 10: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1010

Review: Control Array Review: Control Array ExerciseExercise

Add an Option ButtonAdd an Option Button Name: "optBitNo", Caption: "Bit 0"Name: "optBitNo", Caption: "Bit 0"

Copy the Option Button and PasteCopy the Option Button and Paste Answer YES to create a Control ArrayAnswer YES to create a Control Array Move the new Option Button to its locationMove the new Option Button to its location Modify Caption to "Bit 1"Modify Caption to "Bit 1"

Repeat Paste & Move new option buttonRepeat Paste & Move new option button Modify Caption to "Bit X" where X is 1 greater Modify Caption to "Bit X" where X is 1 greater

than in the previous option button's caption.than in the previous option button's caption. Repeat until you have 8 option buttonsRepeat until you have 8 option buttons

Page 11: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1111

Review: Control Array Review: Control Array ExerciseExercise

Add a second labelAdd a second label Name: lblResultName: lblResult Caption: ""Caption: ""

Page 12: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1212

Review: Control Array Review: Control Array ExerciseExercise

Double Click the button in design modeDouble Click the button in design mode You should be in the code window insideYou should be in the code window inside

Private Sub cmdIsBitSet_Click()Private Sub cmdIsBitSet_Click()

End SubEnd Sub Enter the code:Enter the code:

If miBitMask and ASC(txtChar.Text) <> 0 ThenIf miBitMask and ASC(txtChar.Text) <> 0 Then lblResult.Caption = "True"lblResult.Caption = "True"

ElseElse lblResult.Caption = "False"lblResult.Caption = "False"

End IfEnd If

Page 13: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1313

Review: Control Array Review: Control Array ExerciseExercise

But we don't have a variable named But we don't have a variable named miBitMask…miBitMask… Drop down the Drop down the

control selection incontrol selection incode window andcode window andselect (General)select (General)

Type in (under Option Explicit):Type in (under Option Explicit): Private miBitMask As IntegerPrivate miBitMask As Integer

Page 14: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1414

Review: Control Array Review: Control Array ExerciseExercise

Next, we need some code to set the Next, we need some code to set the bitmask:bitmask:

Double click one of the Option Double click one of the Option Buttons in the form designer, or drop Buttons in the form designer, or drop down the control selection combo in down the control selection combo in the code window and select optBitNothe code window and select optBitNo This brings up the Click event for the This brings up the Click event for the

optionsoptions

Page 15: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1515

Review: Control Array Review: Control Array ExerciseExercise

Add the following code:Add the following code:Private Sub optBitNo_Click(Index As Integer)Private Sub optBitNo_Click(Index As Integer)

miBitMask = 2 ^ IndexmiBitMask = 2 ^ Index

End SubEnd Sub

This results in the bitmask being set This results in the bitmask being set when we click one of the options.when we click one of the options.

Page 16: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1616

Review: Control Array Review: Control Array ExerciseExercise

Two things remain:Two things remain: Initializing the bitmask and optionInitializing the bitmask and option Input ValidationInput Validation

Page 17: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1717

Review: Control Array Review: Control Array ExerciseExercise

Initializing the bitmask and optionInitializing the bitmask and option We should set one of the options as We should set one of the options as

selectedselected By setting it in the Form_Load event By setting it in the Form_Load event

(code), we ensure that miBitMask also (code), we ensure that miBitMask also gets initializedgets initialized

Private Sub Form_Load()Private Sub Form_Load() optBitNo(0).Value = TrueoptBitNo(0).Value = True

End SubEnd Sub

Page 18: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1818

Review: Control Array Review: Control Array ExerciseExercise

Input ValidationInput Validation Make sure txtChar.Text is not emptyMake sure txtChar.Text is not empty A space is a valid character for this app, A space is a valid character for this app,

so we should not Trim() the textso we should not Trim() the text Private Sub cmdIsBitSet_Click()Private Sub cmdIsBitSet_Click()

If txtChar.Text = "" ThenIf txtChar.Text = "" Then MsgBox "You must first specify a character"MsgBox "You must first specify a character" Exit SubExit Sub

End IfEnd If … … the remainder of the code is already enteredthe remainder of the code is already entered

Page 19: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 1919

Review: Control Array Review: Control Array ExerciseExercise

Run the applicationRun the application Enter a characterEnter a character Select a bit numberSelect a bit number Click "Is Bit N Set?"Click "Is Bit N Set?" Try a few subsequent characters with Try a few subsequent characters with

Bit 0 selectedBit 0 selected We get TRUE for every one!We get TRUE for every one! We have a BUG in our APP!!!We have a BUG in our APP!!!

Page 20: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2020

Review: Control Array Review: Control Array ExerciseExercise

What is the problem:What is the problem: Is our miBitMask value incorrect?Is our miBitMask value incorrect? Is our IF-test wrong?Is our IF-test wrong?

Page 21: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2121

Review: Control Array Review: Control Array ExerciseExercise

It turns out that our IF-test is wrong because It turns out that our IF-test is wrong because of OPERATOR PRECEDENCE.of OPERATOR PRECEDENCE. If miBitMask And Asc(txtChar.Text) <> 0If miBitMask And Asc(txtChar.Text) <> 0

Remember: Arithmetic operators are evaluated first, Remember: Arithmetic operators are evaluated first, next the Comparison operators, and last the Logical next the Comparison operators, and last the Logical OperatorsOperators

So we get:So we get: 1: Asc(txtChar.Text) <> 0 => True1: Asc(txtChar.Text) <> 0 => True 2: miBitmask (=1) And True (-1) => 12: miBitmask (=1) And True (-1) => 1 3: 1 is different from False (0), and the expression is 3: 1 is different from False (0), and the expression is

therefore considered Truetherefore considered True Add parentheses to enforce order of operationsAdd parentheses to enforce order of operations

If If ((miBitMask And Asc(txtChar.Text)miBitMask And Asc(txtChar.Text))) <> 0 <> 0

Page 22: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2222

Review: Control Array Review: Control Array ExerciseExercise

Instead of using the statement Instead of using the statement miBitMask = 2 ^ IndexmiBitMask = 2 ^ Index

in the optBitNo_Click event, we could set in the optBitNo_Click event, we could set the Index properties of each of the options the Index properties of each of the options to the corresponding value:to the corresponding value:

1, 2, 4, 8, 16, 32, 64, 1281, 2, 4, 8, 16, 32, 64, 128and useand use

miBitMask = IndexmiBitMask = Index Try it!Try it!

Page 23: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2323

The End of the RoadThe End of the Road

We will addressWe will address

Page 24: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2424

Variable ScopeVariable Scope

"Variable Scope" means "where in my code "Variable Scope" means "where in my code is this variable name valid?"is this variable name valid?" Local variablesLocal variables

Only accessible in the procedure where they are Only accessible in the procedure where they are declared.declared.

Module level (Private)Module level (Private) Accessible anywhere in the form or module it is Accessible anywhere in the form or module it is

declared.declared. Public or Global variablesPublic or Global variables

Accessible throughout the application, but may need a Accessible throughout the application, but may need a reference to the form where it is declared.reference to the form where it is declared.

Page 25: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2525

Variable ScopeVariable Scope

Local variablesLocal variables Variables declared in a procedure are only Variables declared in a procedure are only

valid in that procedure. Parameters can also valid in that procedure. Parameters can also be considered local, but beware of ByRef be considered local, but beware of ByRef parameters (WHY?).parameters (WHY?).

Private Sub MySub(ByRef sName As String)Private Sub MySub(ByRef sName As String)

Dim sFirstName As StringDim sFirstName As String

……

End FunctionEnd Function

Page 26: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2626

Variable ScopeVariable Scope

Module level (Private)Module level (Private) Variables declared in the "General Variables declared in the "General

Declarations" section of a module or Declarations" section of a module or form, etc. using either of the statements:form, etc. using either of the statements:Private Private VariableNameVariableName As As SomeTypeSomeType

Dim Dim VariableNameVariableName As As SomeTypeSomeType

(Dim defaults to Private for variable (Dim defaults to Private for variable declarations)declarations)

Page 27: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2727

Variable ScopeVariable Scope

Public or Global variablesPublic or Global variables Variables declared in the "General Variables declared in the "General

Declarations" section of a module or Declarations" section of a module or form, etc. using the statement:form, etc. using the statement:Public Public VariableNameVariableName As As SomeTypeSomeType

In a module, this In a module, this obsoleteobsolete syntax is syntax is also allowed:also allowed:Global Global VariableNameVariableName As As SomeTypeSomeType

Page 28: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2828

More about ScopeMore about Scope

In addition to Variables, Scope In addition to Variables, Scope applies to procedure names, applies to procedure names, declared constants (declared constants (Const Const ConstantNameConstantName = = ValueValue), etc.), etc.

Variables declared with the same Variables declared with the same name in different scopes are name in different scopes are different variables!different variables!

Page 29: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 2929

Using More than One FormUsing More than One Form

Load Load FormXFormX This loads the form (FormX) into memory, but This loads the form (FormX) into memory, but

does not display it.does not display it. FormX.Show [ModalFlag]FormX.Show [ModalFlag]

This shows FormX. If it is not already loaded, it This shows FormX. If it is not already loaded, it will be loaded and then displayed.will be loaded and then displayed.

The optional [ModalFlag] parameter specifies if The optional [ModalFlag] parameter specifies if we want the new form to keep the other forms we want the new form to keep the other forms in the application from getting focus:in the application from getting focus:

FormX.Show vbModalFormX.Show vbModal

makes this happen.makes this happen.

Page 30: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 3030

Using More than One FormUsing More than One Form

To reference multiple instances of the same To reference multiple instances of the same form, or keep track of instances of different form, or keep track of instances of different forms, use a Form reference variable forms, use a Form reference variable instead of the form's name:instead of the form's name:

Dim frmX as Form, frmY as FormDim frmX as Form, frmY as FormSet frmX = New FormXSet frmX = New FormXfrmX.ShowfrmX.ShowfrmX.Caption = "Form X"frmX.Caption = "Form X"Set frmY = New FormXSet frmY = New FormXfrmY.Caption = "Form Y"frmY.Caption = "Form Y"frmY.Show vbModalfrmY.Show vbModalMsgBox frmY.CaptionMsgBox frmY.Caption

Page 31: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 3131

Custom Controls and Object Custom Controls and Object ReferencesReferences

FileSystemObjectFileSystemObject Rich Text BoxRich Text Box

Page 32: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 3232

Adding an Object ReferenceAdding an Object Reference

Click the Project menu, References…Click the Project menu, References… This shows a list of all the object This shows a list of all the object

references on your system. Not all are references on your system. Not all are (legally) available for you to use in (legally) available for you to use in development.development.

We will use the FileSystemObject, which is We will use the FileSystemObject, which is part of the "Microsoft Scripting Runtime" part of the "Microsoft Scripting Runtime" object library.object library.

Scroll down to "Microsoft Scripting Scroll down to "Microsoft Scripting Runtime" and select it, and click OK.Runtime" and select it, and click OK.

Page 33: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 3333

Using Referenced ObjectsUsing Referenced Objects

After adding the reference, we can After adding the reference, we can declare object reference variable declare object reference variable pointing to objects that are defined pointing to objects that are defined in that reference.in that reference.

In our case:In our case:Dim fso as Scripting.FileSystemObjectDim fso as Scripting.FileSystemObject

The FileSystemObject has several The FileSystemObject has several useful properties and methods.useful properties and methods.

Page 34: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 3434

The FileSystem ObjectThe FileSystem Object

We will use the FileSystemObject only to We will use the FileSystemObject only to list files in a folder:list files in a folder:

Dim fso as Scripting.FileSystemObjectDim fso as Scripting.FileSystemObjectDim fldrX as Scripting.FolderDim fldrX as Scripting.FolderDim fileX as Scripting.FileDim fileX as Scripting.FileSet fso = New Scripting.FileSystemObjectSet fso = New Scripting.FileSystemObjectSet fldrX = fso.GetFolder(txtPath.Text)Set fldrX = fso.GetFolder(txtPath.Text)For Each fileX in fldrX.FilesFor Each fileX in fldrX.Files

lstFiles.AddItem fileX.NamelstFiles.AddItem fileX.Name

NextNextSet fldrX = NothingSet fldrX = NothingSet fso = NothingSet fso = Nothing

Page 35: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 3535

A Graphics Viewer ExampleA Graphics Viewer Example

We can very easily make this a simple We can very easily make this a simple graphics viewer application.graphics viewer application. Add an Image controlAdd an Image control Add a Double-Click event handler for the list:Add a Double-Click event handler for the list:

Private Sub lstFiles_DblClick()Private Sub lstFiles_DblClick()

Set Image1.Picture = LoadPicture(txtPath.Text _Set Image1.Picture = LoadPicture(txtPath.Text _

& "\" & lstFiles.Text)& "\" & lstFiles.Text)

End SubEnd Sub

Page 36: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 3636

A Graphics Viewer ExampleA Graphics Viewer Example

If we were going to for instance draw If we were going to for instance draw on the picture, we would have used a on the picture, we would have used a PictureBox instead, but the Image PictureBox instead, but the Image control has an interesting property control has an interesting property called "Stretch". Add the following called "Stretch". Add the following code to the top of the form's code code to the top of the form's code window (after Option Explicit).window (after Option Explicit).

Page 37: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 3737

A Graphics Viewer ExampleA Graphics Viewer Example

Private lImageHeight As LongPrivate lImageHeight As LongPrivate lImageWidth As LongPrivate lImageWidth As Long

Private Sub Form_Load()Private Sub Form_Load() lImageHeight = Image1.HeightlImageHeight = Image1.Height lImageWidth = Image1.WidthlImageWidth = Image1.WidthEnd SubEnd Sub

Private Sub chkStretch_Click()Private Sub chkStretch_Click() Image1.Height = lImageHeightImage1.Height = lImageHeight Image1.Width = lImageWidthImage1.Width = lImageWidth Image1.Stretch = chkStretch.Value = vbCheckedImage1.Stretch = chkStretch.Value = vbCheckedEnd SubEnd Sub

Page 38: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 3838

LoopsLoops

We will use a We will use a Do While Do While ExpressionExpression

……

LoopLoop ……in our Text File Viewer application.in our Text File Viewer application.

Page 39: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 3939

A Text File Viewer ExampleA Text File Viewer Example

Copy the graphics viewer projectCopy the graphics viewer project Delete the Image control and all related Delete the Image control and all related

codecode Add a second form, name it Add a second form, name it

"frmDisplayFile" and add a TextBox on the "frmDisplayFile" and add a TextBox on the form (fill the form with it).form (fill the form with it).

Set TextBox name to "txtDisplay", Set TextBox name to "txtDisplay", MultiLine to True and ScrollBars to "2 – MultiLine to True and ScrollBars to "2 – Vertical".Vertical".

Page 40: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 4040

A Text File Viewer ExampleA Text File Viewer Example

We need some code to Read the file:We need some code to Read the file:Dim fso As Scripting.FileSystemObjectDim tsX As Scripting.TextStreamDim sText As StringDim frmDisplay As frmDisplayFile 'or As Form...

Set fso = New Scripting.FileSystemObject Set tsX = fso.OpenTextFile(txtPath.Text & "\" & lstFiles.Text, ForReading) Do While Not tsX.AtEndOfStream sText = sText & tsX.ReadLine & vbCrLf Loop tsX.Close Set tsX = Nothing Set fso = Nothing

Page 41: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 4141

A Text File Viewer ExampleA Text File Viewer Example

… … and some code to load the display and some code to load the display form and show the content of the form and show the content of the file:file: Set frmDisplay = New frmDisplayFile frmDisplay.txtDisplay.Text = sText frmDisplay.Show Set frmDisplay = Nothing

Page 42: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 4242

Using Custom ControlsUsing Custom Controls

Custom Controls are controls that are not Custom Controls are controls that are not normally in the ToolBox.normally in the ToolBox.

To add a Custom Control (that is installed To add a Custom Control (that is installed on your system) to the ToolBox, select the on your system) to the ToolBox, select the Project menu, Components…Project menu, Components…

Again, you may not have legal rights to Again, you may not have legal rights to use all of the controls listed.use all of the controls listed.

Typically, controls used by various Typically, controls used by various applications installed on your system may applications installed on your system may show up in this list.show up in this list.

Page 43: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 4343

Using Custom ControlsUsing Custom Controls

Each component may contain Each component may contain multiple controls.multiple controls.

Once you find the component that Once you find the component that you wish to use, select it and click you wish to use, select it and click OK. The controls in that component OK. The controls in that component will be added to your ToolBox, and will be added to your ToolBox, and you can place them on your form just you can place them on your form just like any other controls.like any other controls.

Page 44: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 4444

Custom ControlsCustom Controls

Useful components (controls) includeUseful components (controls) include Microsoft Calendar ControlMicrosoft Calendar Control Microsoft Common Dialog ControlMicrosoft Common Dialog Control Microsoft DataGrid ControlMicrosoft DataGrid Control Microsoft Rich Textbox ControlMicrosoft Rich Textbox Control Microsoft Tabbed Dialog ControlMicrosoft Tabbed Dialog Control Microsoft Windows Common Control (3 sets)Microsoft Windows Common Control (3 sets) … … and many others.and many others. Play with them to see what they do.Play with them to see what they do.

Page 45: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 4545

Custom Control ExampleCustom Control Example

Copy the Text File Viewer projectCopy the Text File Viewer project On the display form, delete the TextBox.On the display form, delete the TextBox. Add the Microsoft Rich Textbox Control Add the Microsoft Rich Textbox Control

to your project.to your project. Add a Rich Textbox to the display form.Add a Rich Textbox to the display form. Name the Rich Textbox "rtfDisplay", and Name the Rich Textbox "rtfDisplay", and

set the property ScrollBars to "3 – set the property ScrollBars to "3 – rtfBoth".rtfBoth".

Page 46: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 4646

Custom Control ExampleCustom Control Example

Instead of all the code used to read the Instead of all the code used to read the contents of the selected text file, we will contents of the selected text file, we will simply use a handy method of the Rich simply use a handy method of the Rich Textbox control – LoadFileTextbox control – LoadFilePrivate Sub lstFiles_DblClick()Dim frmDisplay As frmDisplayFile 'or As Form... Set frmDisplay = New frmDisplayFile frmDisplay.rtfDisplay.LoadFile txtPath.Text & "\" &

lstFiles.Text frmDisplay.Show Set frmDisplay = NothingEnd Sub

Page 47: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 4747

DebuggingDebugging

Debugging means finding and fixing Debugging means finding and fixing things that don't work.things that don't work.

In the early days of computers, when In the early days of computers, when ENIAC tipped the scale at 30 tons ENIAC tipped the scale at 30 tons and consisting of tubes and wires, and consisting of tubes and wires, the problem of live bugs affecting the the problem of live bugs affecting the system was real. So the term system was real. So the term "debugging" was adopted."debugging" was adopted.

Page 48: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 4848

DebuggingDebugging

Microsoft Visual Basic provides some very Microsoft Visual Basic provides some very strong tools for debugging our code:strong tools for debugging our code: Stepping through a project, line by lineStepping through a project, line by line Inspecting contents of variables (in break Inspecting contents of variables (in break

mode)mode) Change code and continue to runChange code and continue to run Modify contents of variables in break modeModify contents of variables in break mode Set BreakpointsSet Breakpoints Etc.Etc.

Page 49: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 4949

DebuggingDebugging

Open the Text File Viewer ProjectOpen the Text File Viewer Project Click Debug, Step Into (or F8)Click Debug, Step Into (or F8) Notice that the form was our "startup" for the Notice that the form was our "startup" for the

project, we haven't executed any code yet.project, we haven't executed any code yet. Modify the path (if required) and click on the Modify the path (if required) and click on the

command button. Notice how the line "command button. Notice how the line "Private Private Sub cmdListFiles_Click()Sub cmdListFiles_Click()" is highlighted." is highlighted.

Click F8 and see the highlight move to the next Click F8 and see the highlight move to the next line of code, but skips the declarations (they line of code, but skips the declarations (they are not executable statements).are not executable statements).

Page 50: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 5050

DebuggingDebugging

Click F8 twice more, and the line Click F8 twice more, and the line For Each fileX In fldrX.FilesFor Each fileX In fldrX.Files

should be highlightedshould be highlighted At this point, make sure the Immediate At this point, make sure the Immediate

window is showing. If not, click on View, window is showing. If not, click on View, Immediate Window or press ctrl+G.Immediate Window or press ctrl+G.

In the Immediate Window, type:In the Immediate Window, type: ?fldrX.Name?fldrX.Name and hit Enter and hit Enter

The value of The value of fldrX.Name fldrX.Name is displayed…is displayed…

Page 51: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 5151

DebuggingDebugging

Next, we will see how Breakpoints Next, we will see how Breakpoints work. Find the linework. Find the linesText = sText & tsX.ReadLine & vbCrLfsText = sText & tsX.ReadLine & vbCrLf

in the code window, and click in the in the code window, and click in the gray area in the left margin next to it.gray area in the left margin next to it.

You should get a red dot in the margin You should get a red dot in the margin and the line should be highlighted in and the line should be highlighted in red.red.

Page 52: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 5252

DebuggingDebugging

This means a breakpoint has been set at that This means a breakpoint has been set at that line – I.e. if and when the program tries to line – I.e. if and when the program tries to execute that statement, it will enter break execute that statement, it will enter break mode.mode.

Press F5 to continue the application running Press F5 to continue the application running without having to step through every line.without having to step through every line.

List files in the folder, and double-click one of List files in the folder, and double-click one of the text files. At that point, we should hit the the text files. At that point, we should hit the breakpoint, and we can now do more breakpoint, and we can now do more debugging.debugging.

Page 53: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 5353

DebuggingDebugging

Right Click on the word "Right Click on the word "sTextsText" in the code, " in the code, and select Add Watch…and select Add Watch…

Look at the options available, but for now Look at the options available, but for now we will just accept the defaults, so click OK.we will just accept the defaults, so click OK.

Notice how the Watches window shows up, Notice how the Watches window shows up, and lists the expression sText as well as and lists the expression sText as well as show its content.show its content.

We can watch objects as well, and then we We can watch objects as well, and then we can easily check the contents of its various can easily check the contents of its various properties. Try!properties. Try!

Page 54: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 5454

Error HandlingError Handling

Error Handling really deserves a session Error Handling really deserves a session of its own.of its own.

However, Visual Basic will intercept However, Visual Basic will intercept errors that you don't explicitly handle. errors that you don't explicitly handle. That can be a bad thing, because VB That can be a bad thing, because VB won't know what to do, and quite often won't know what to do, and quite often will just terminate the application after will just terminate the application after showing a cryptic (to the user) error showing a cryptic (to the user) error message.message.

Page 55: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 5555

Error HandlingError Handling

Implement Error Handling by using the Implement Error Handling by using the statement:statement:On Error GoTo On Error GoTo LabelNameLabelName

And add some logic, maybe logging of the And add some logic, maybe logging of the error, and display a user-friendly message error, and display a user-friendly message in code following the label.in code following the label.

Be especially ready to handle errors as a Be especially ready to handle errors as a result of invalid input, file access, result of invalid input, file access, database access, etc.database access, etc.

Page 56: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 5656

Error HandlingError Handling

Sample:Sample:On Error GoTo ErrHandlerOn Error GoTo ErrHandler……Exit SubExit SubErrHandler:ErrHandler: lErr = Err.NumberlErr = Err.Number sErr = Err.DescriptionsErr = Err.Description LogError lErr, "ModuleName.ProcName", sErrLogError lErr, "ModuleName.ProcName", sErr sMsg = "Error " & lErr & " ." & vbCr & sErrsMsg = "Error " & lErr & " ." & vbCr & sErr Resume NextResume Next

Page 57: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 5757

Error HandlingError Handling

On Error Resume NextOn Error Resume Next Check for errors between statementsCheck for errors between statements

On Error GoTo 0On Error GoTo 0 Reset error handler (turn it off)Reset error handler (turn it off)

Page 58: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 5858

Error HandlingError Handling

Resuming execution of non-error Resuming execution of non-error handler:handler: ResumeResume

In an error handler, means try the failing In an error handler, means try the failing statement againstatement again

Resume NextResume Next Skip the failed statement, but continue with Skip the failed statement, but continue with

the one following it.the one following it. Resume Resume LabelLabel

Continue execution from Continue execution from LabelLabel location location

Page 59: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 5959

Error HandlingError Handling

Watch out for errors in the error Watch out for errors in the error handler – they won't be dealt with, handler – they won't be dealt with, but caught by VB instead (usually not but caught by VB instead (usually not a good thing).a good thing).

Always end the error handler code Always end the error handler code with a form of Resume or Exit with a form of Resume or Exit Sub/Exit Function, etc.Sub/Exit Function, etc.

Page 60: 4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8

4/20/20044/20/2004PPCC - Introduction to VB6PPCC - Introduction to VB6

Copyright Copyright ©2004, Tore Bostrup©2004, Tore Bostrup Page Page 6060

The EndThe End

ThanksThanks I have enjoyed itI have enjoyed it

We will continue in the VBSIG…We will continue in the VBSIG…