Download presentation
Presentation is loading. Please wait.
Published bySamuel Robertson Modified over 9 years ago
1
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 8
2
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 2 The End of the Road Today, we will briefly review Today, we will briefly review String Manipulation Exercise String Manipulation Exercise More Controls More Controls Containers Containers Control Array Exercise Control Array Exercise
3
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 3 The End of the Road We will address We will address Variable Scope Variable Scope Using more than one form Using more than one form A Graphics Viewer Example A Graphics Viewer Example Loops Loops Custom Controls and Object References Custom Controls and Object References FileSystemObject FileSystemObject Rich Text Box Rich Text Box Debugging Code Debugging Code Error Handling Error Handling
4
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 4 Review: String Manipulation Exercise You have received a mailing list with names entered in different ways. You have received a mailing list with names entered in different ways. There are no middle names in the list, but some names include titles, etc. There are no middle names in the list, but 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 as Names may be entered as [Title] First Last[Title] [Title] First Last[Title] [Title] Last, First[Title] [Title] Last, First[Title]
5
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 5 Review: String Manipulation Exercise Our job is to write a subroutine that receives the name string as one parameter, and outputs the name into four separate TextBoxes Our job is to write a subroutine that receives the name string as one parameter, and outputs the name into four separate TextBoxes txtPrefix, txtFirst, txtLast, txtSuffix txtPrefix, txtFirst, txtLast, txtSuffix We also need to create a "test harness" for the subroutine We also need to create a "test harness" for the subroutine In this case, a project with a form that allows us to type in an arbitrary name entry, calls the subroutine, and contains the TextBoxes for the output. In this case, a project with a form that allows us to type in an arbitrary name entry, calls the subroutine, and contains the TextBoxes for the output.
6
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 6 Review: String Manipulation Exercise Questions? Questions? Solutions? Solutions? Label1(0) txtFullName cmdSplitName txtPrefixtxtFirstNametxtLastNametxtSuffix
7
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 7 Review: More controls (Option Button, CheckBox, Frame) Look at a typical Print dialog box: Look at a typical Print dialog box: Checkboxes Frames Option Buttons
8
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 8 Review: Containers Containers are controls (or windows) that can contain other controls (members) Containers are controls (or windows) that can contain other controls (members) Place a control on a container, and move the container – the member controls follow the container. Place a control on a container, and move the container – the member controls follow the container. Container Controls: Container Controls: PictureBox PictureBox Frame Frame Tabbed Dialog Tabbed Dialog CoolBar CoolBar
9
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 9 Review: Control Array Exercise Create a New Project Create a New Project Add a Label, a TextBox and a CommandButton Add a Label, a TextBox and a CommandButton Label Name: "Label1", Caption: "Character" Label Name: "Label1", Caption: "Character" TextBox Name: "txtChar", Text: "" TextBox Name: "txtChar", Text: "" CommandButton Name: "cmdIsBitSet", Caption: "Is Bit n Set?" CommandButton Name: "cmdIsBitSet", Caption: "Is Bit n Set?"
10
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 10 Review: Control Array Exercise Add an Option Button Add an Option Button Name: "optBitNo", Caption: "Bit 0" Name: "optBitNo", Caption: "Bit 0" Copy the Option Button and Paste Copy the Option Button and Paste Answer YES to create a Control Array Answer YES to create a Control Array Move the new Option Button to its location Move the new Option Button to its location Modify Caption to "Bit 1" Modify Caption to "Bit 1" Repeat Paste & Move new option button Repeat Paste & Move new option button Modify Caption to "Bit X" where X is 1 greater than in the previous option button's caption. Modify Caption to "Bit X" where X is 1 greater than in the previous option button's caption. Repeat until you have 8 option buttons Repeat until you have 8 option buttons
11
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 11 Review: Control Array Exercise Add a second label Add a second label Name: lblResult Name: lblResult Caption: "" Caption: ""
12
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 12 Review: Control Array Exercise Double Click the button in design mode Double Click the button in design mode You should be in the code window inside You should be in the code window inside Private Sub cmdIsBitSet_Click() Private Sub cmdIsBitSet_Click() End Sub End Sub Enter the code: Enter the code: If miBitMask and ASC(txtChar.Text) <> 0 Then If miBitMask and ASC(txtChar.Text) <> 0 Then lblResult.Caption = "True" lblResult.Caption = "True" Else Else lblResult.Caption = "False" lblResult.Caption = "False" End If End If
13
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 13 Review: Control Array Exercise But we don't have a variable named miBitMask… But we don't have a variable named miBitMask… Drop down the control selection in code window and select (General) Drop down the control selection in code window and select (General) Type in (under Option Explicit): Type in (under Option Explicit): Private miBitMask As Integer Private miBitMask As Integer
14
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 14 Review: Control Array Exercise Next, we need some code to set the bitmask: Next, we need some code to set the bitmask: Double click one of the Option Buttons in the form designer, or drop down the control selection combo in the code window and select optBitNo Double click one of the Option Buttons in the form designer, or drop down the control selection combo in the code window and select optBitNo This brings up the Click event for the options This brings up the Click event for the options
15
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 15 Review: Control Array Exercise Add the following code: Add the following code: Private Sub optBitNo_Click(Index As Integer) miBitMask = 2 ^ Index miBitMask = 2 ^ Index End Sub This results in the bitmask being set when we click one of the options. This results in the bitmask being set when we click one of the options.
16
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 16 Review: Control Array Exercise Two things remain: Two things remain: Initializing the bitmask and option Initializing the bitmask and option Input Validation Input Validation
17
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 17 Review: Control Array Exercise Initializing the bitmask and option Initializing the bitmask and option We should set one of the options as selected We should set one of the options as selected By setting it in the Form_Load event (code), we ensure that miBitMask also gets initialized By setting it in the Form_Load event (code), we ensure that miBitMask also gets initialized Private Sub Form_Load() Private Sub Form_Load() optBitNo(0).Value = True optBitNo(0).Value = True End Sub End Sub
18
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 18 Review: Control Array Exercise Input Validation Input Validation Make sure txtChar.Text is not empty Make sure txtChar.Text is not empty A space is a valid character for this app, so we should not Trim() the text A space is a valid character for this app, so we should not Trim() the text Private Sub cmdIsBitSet_Click() Private Sub cmdIsBitSet_Click() If txtChar.Text = "" Then If txtChar.Text = "" Then MsgBox "You must first specify a character" MsgBox "You must first specify a character" Exit Sub Exit Sub End If End If … the remainder of the code is already entered … the remainder of the code is already entered
19
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 19 Review: Control Array Exercise Run the application Run the application Enter a character Enter a character Select a bit number Select a bit number Click "Is Bit N Set?" Click "Is Bit N Set?" Try a few subsequent characters with Bit 0 selected Try a few subsequent characters with Bit 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!!!
20
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 20 Review: Control Array Exercise 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?
21
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 21 Review: Control Array Exercise It turns out that our IF-test is wrong because of OPERATOR PRECEDENCE. It turns out that our IF-test is wrong because of OPERATOR PRECEDENCE. If miBitMask And Asc(txtChar.Text) <> 0 If miBitMask And Asc(txtChar.Text) <> 0 Remember: Arithmetic operators are evaluated first, next the Comparison operators, and last the Logical Operators Remember: Arithmetic operators are evaluated first, next the Comparison operators, and last the Logical Operators So we get: So we get: 1: Asc(txtChar.Text) <> 0 => True 1: Asc(txtChar.Text) <> 0 => True 2: miBitmask (=1) And True (-1) => 1 2: miBitmask (=1) And True (-1) => 1 3: 1 is different from False (0), and the expression is therefore considered True 3: 1 is different from False (0), and the expression is therefore considered True Add parentheses to enforce order of operations Add parentheses to enforce order of operations If (miBitMask And Asc(txtChar.Text)) <> 0 If (miBitMask And Asc(txtChar.Text)) <> 0
22
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 22 Review: Control Array Exercise Instead of using the statement Instead of using the statement miBitMask = 2 ^ Index in the optBitNo_Click event, we could set the Index properties of each of the options to the corresponding value: 1, 2, 4, 8, 16, 32, 64, 128 and use miBitMask = Index Try it! Try it!
23
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 23 The End of the Road We will address We will address
24
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 24 Variable Scope "Variable Scope" means "where in my code is this variable name valid?" "Variable Scope" means "where in my code is this variable name valid?" Local variables Local variables Only accessible in the procedure where they are declared. Only accessible in the procedure where they are declared. Module level (Private) Module level (Private) Accessible anywhere in the form or module it is declared. Accessible anywhere in the form or module it is declared. Public or Global variables Public or Global variables Accessible throughout the application, but may need a reference to the form where it is declared. Accessible throughout the application, but may need a reference to the form where it is declared.
25
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 25 Variable Scope Local variables Local variables Variables declared in a procedure are only valid in that procedure. Parameters can also be considered local, but beware of ByRef parameters (WHY?). Variables declared in a procedure are only valid in that procedure. Parameters can also be considered local, but beware of ByRef parameters (WHY?). Private Sub MySub(ByRef sName As String) Dim sFirstName As String … End Function
26
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 26 Variable Scope Module level (Private) Module level (Private) Variables declared in the "General Declarations" section of a module or form, etc. using either of the statements: Variables declared in the "General Declarations" section of a module or form, etc. using either of the statements: Private VariableName As SomeType Dim VariableName As SomeType (Dim defaults to Private for variable declarations)
27
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 27 Variable Scope Public or Global variables Public or Global variables Variables declared in the "General Declarations" section of a module or form, etc. using the statement: Variables declared in the "General Declarations" section of a module or form, etc. using the statement: Public VariableName As SomeType In a module, this obsolete syntax is also allowed: In a module, this obsolete syntax is also allowed: Global VariableName As SomeType
28
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 28 More about Scope In addition to Variables, Scope applies to procedure names, declared constants (Const ConstantName = Value), etc. In addition to Variables, Scope applies to procedure names, declared constants (Const ConstantName = Value), etc. Variables declared with the same name in different scopes are different variables! Variables declared with the same name in different scopes are different variables!
29
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 29 Using More than One Form Load FormX Load FormX This loads the form (FormX) into memory, but does not display it. This loads the form (FormX) into memory, but does not display it. FormX.Show [ModalFlag] FormX.Show [ModalFlag] This shows FormX. If it is not already loaded, it will be loaded and then displayed. This shows FormX. If it is not already loaded, it will be loaded and then displayed. The optional [ModalFlag] parameter specifies if we want the new form to keep the other forms in the application from getting focus: The optional [ModalFlag] parameter specifies if we want the new form to keep the other forms in the application from getting focus: FormX.Show vbModal makes this happen.
30
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 30 Using More than One Form To reference multiple instances of the same form, or keep track of instances of different forms, use a Form reference variable instead of the form's name: To reference multiple instances of the same form, or keep track of instances of different forms, use a Form reference variable instead of the form's name: Dim frmX as Form, frmY as Form Set frmX = New FormX frmX.Show frmX.Caption = "Form X" Set frmY = New FormX frmY.Caption = "Form Y" frmY.Show vbModal MsgBox frmY.Caption
31
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 31 Custom Controls and Object References FileSystemObject FileSystemObject Rich Text Box Rich Text Box
32
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 32 Adding an Object Reference Click the Project menu, References… Click the Project menu, References… This shows a list of all the object references on your system. Not all are (legally) available for you to use in development. This shows a list of all the object references on your system. Not all are (legally) available for you to use in development. We will use the FileSystemObject, which is part of the "Microsoft Scripting Runtime" object library. We will use the FileSystemObject, which is part of the "Microsoft Scripting Runtime" object library. Scroll down to "Microsoft Scripting Runtime" and select it, and click OK. Scroll down to "Microsoft Scripting Runtime" and select it, and click OK.
33
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 33 Using Referenced Objects After adding the reference, we can declare object reference variable pointing to objects that are defined in that reference. After adding the reference, we can declare object reference variable pointing to objects that are defined in that reference. In our case: In our case: Dim fso as Scripting.FileSystemObject The FileSystemObject has several useful properties and methods. The FileSystemObject has several useful properties and methods.
34
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 34 The FileSystem Object We will use the FileSystemObject only to list files in a folder: We will use the FileSystemObject only to list files in a folder: Dim fso as Scripting.FileSystemObject Dim fldrX as Scripting.Folder Dim fileX as Scripting.File Set fso = New Scripting.FileSystemObject Set fldrX = fso.GetFolder(txtPath.Text) For Each fileX in fldrX.Files lstFiles.AddItem fileX.Name Next Set fldrX = Nothing Set fso = Nothing
35
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 35 A Graphics Viewer Example We can very easily make this a simple graphics viewer application. We can very easily make this a simple graphics viewer application. Add an Image control Add 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() Set Image1.Picture = LoadPicture(txtPath.Text _ Set Image1.Picture = LoadPicture(txtPath.Text _ & "\" & lstFiles.Text) End Sub
36
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 36 A Graphics Viewer Example If we were going to for instance draw on the picture, we would have used a PictureBox instead, but the Image control has an interesting property called "Stretch". Add the following code to the top of the form's code window (after Option Explicit). If we were going to for instance draw on the picture, we would have used a PictureBox instead, but the Image control has an interesting property called "Stretch". Add the following code to the top of the form's code window (after Option Explicit).
37
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 37 A Graphics Viewer Example Private lImageHeight As Long Private lImageWidth As Long Private Sub Form_Load() lImageHeight = Image1.Height lImageHeight = Image1.Height lImageWidth = Image1.Width lImageWidth = Image1.Width End Sub Private Sub chkStretch_Click() Image1.Height = lImageHeight Image1.Height = lImageHeight Image1.Width = lImageWidth Image1.Width = lImageWidth Image1.Stretch = chkStretch.Value = vbChecked Image1.Stretch = chkStretch.Value = vbChecked End Sub
38
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 38 Loops We will use a We will use a Do While Expression …Loop …in our Text File Viewer application. …in our Text File Viewer application.
39
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 39 A Text File Viewer Example Copy the graphics viewer project Copy the graphics viewer project Delete the Image control and all related code Delete the Image control and all related code Add a second form, name it "frmDisplayFile" and add a TextBox on the form (fill the form with it). Add a second form, name it "frmDisplayFile" and add a TextBox on the form (fill the form with it). Set TextBox name to "txtDisplay", MultiLine to True and ScrollBars to "2 – Vertical". Set TextBox name to "txtDisplay", MultiLine to True and ScrollBars to "2 – Vertical".
40
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 40 A Text File Viewer Example We need some code to Read the file: We need some code to Read the file: Dim fso As Scripting.FileSystemObject Dim tsX As Scripting.TextStream Dim sText As String Dim 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
41
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 41 A Text File Viewer Example … and some code to load the display form and show the content of the file: … and some code to load the display form and show the content of the file: Set frmDisplay = New frmDisplayFile frmDisplay.txtDisplay.Text = sText frmDisplay.Show Set frmDisplay = Nothing
42
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 42 Using Custom Controls Custom Controls are controls that are not normally in the ToolBox. Custom Controls are controls that are not normally in the ToolBox. To add a Custom Control (that is installed on your system) to the ToolBox, select the Project menu, Components… To add a Custom Control (that is installed on your system) to the ToolBox, select the Project menu, Components… Again, you may not have legal rights to use all of the controls listed. Again, you may not have legal rights to use all of the controls listed. Typically, controls used by various applications installed on your system may show up in this list. Typically, controls used by various applications installed on your system may show up in this list.
43
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 43 Using Custom Controls Each component may contain multiple controls. Each component may contain multiple controls. Once you find the component that you wish to use, select it and click OK. The controls in that component will be added to your ToolBox, and you can place them on your form just like any other controls. Once you find the component that you wish to use, select it and click OK. The controls in that component will be added to your ToolBox, and you can place them on your form just like any other controls.
44
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 44 Custom Controls Useful components (controls) include Useful components (controls) include Microsoft Calendar Control Microsoft Calendar Control Microsoft Common Dialog Control Microsoft Common Dialog Control Microsoft DataGrid Control Microsoft DataGrid Control Microsoft Rich Textbox Control Microsoft Rich Textbox Control Microsoft Tabbed Dialog Control Microsoft 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.
45
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 45 Custom Control Example Copy the Text File Viewer project Copy 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 to your project. Add the Microsoft Rich Textbox Control 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 set the property ScrollBars to "3 – rtfBoth". Name the Rich Textbox "rtfDisplay", and set the property ScrollBars to "3 – rtfBoth".
46
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 46 Custom Control Example Instead of all the code used to read the contents of the selected text file, we will simply use a handy method of the Rich Textbox control – LoadFile Instead of all the code used to read the contents of the selected text file, we will simply use a handy method of the Rich Textbox control – LoadFile Private 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 = Nothing End Sub
47
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 47 Debugging Debugging means finding and fixing things that don't work. Debugging means finding and fixing things that don't work. In the early days of computers, when ENIAC tipped the scale at 30 tons and consisting of tubes and wires, the problem of live bugs affecting the system was real. So the term "debugging" was adopted. In the early days of computers, when ENIAC tipped the scale at 30 tons and consisting of tubes and wires, the problem of live bugs affecting the system was real. So the term "debugging" was adopted.
48
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 48 Debugging Microsoft Visual Basic provides some very strong tools for debugging our code: Microsoft Visual Basic provides some very strong tools for debugging our code: Stepping through a project, line by line Stepping through a project, line by line Inspecting contents of variables (in break mode) Inspecting contents of variables (in break mode) Change code and continue to run Change code and continue to run Modify contents of variables in break mode Modify contents of variables in break mode Set Breakpoints Set Breakpoints Etc. Etc.
49
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 49 Debugging Open the Text File Viewer Project Open 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 project, we haven't executed any code yet. Notice that the form was our "startup" for the project, we haven't executed any code yet. Modify the path (if required) and click on the command button. Notice how the line "Private Sub cmdListFiles_Click()" is highlighted. Modify the path (if required) and click on the command button. Notice how the line "Private Sub cmdListFiles_Click()" is highlighted. Click F8 and see the highlight move to the next line of code, but skips the declarations (they are not executable statements). Click F8 and see the highlight move to the next line of code, but skips the declarations (they are not executable statements).
50
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 50 Debugging Click F8 twice more, and the line Click F8 twice more, and the line For Each fileX In fldrX.Files should be highlighted At this point, make sure the Immediate window is showing. If not, click on View, Immediate Window or press ctrl+G. At this point, make sure the Immediate window is showing. If not, click on View, Immediate Window or press ctrl+G. In the Immediate Window, type: In the Immediate Window, type: ?fldrX.Name and hit Enter ?fldrX.Name and hit Enter The value of fldrX.Name is displayed… The value of fldrX.Name is displayed…
51
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 51 Debugging Next, we will see how Breakpoints work. Find the line Next, we will see how Breakpoints work. Find the line sText = sText & tsX.ReadLine & vbCrLf in the code window, and click in the gray area in the left margin next to it. in the code window, and click in the gray area in the left margin next to it. You should get a red dot in the margin and the line should be highlighted in red. You should get a red dot in the margin and the line should be highlighted in red.
52
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 52 Debugging This means a breakpoint has been set at that line – I.e. if and when the program tries to execute that statement, it will enter break mode. This means a breakpoint has been set at that line – I.e. if and when the program tries to execute that statement, it will enter break mode. Press F5 to continue the application running without having to step through every line. Press F5 to continue the application running without having to step through every line. List files in the folder, and double-click one of the text files. At that point, we should hit the breakpoint, and we can now do more debugging. List files in the folder, and double-click one of the text files. At that point, we should hit the breakpoint, and we can now do more debugging.
53
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 53 Debugging Right Click on the word "sText" in the code, and select Add Watch… Right Click on the word "sText" in the code, and select Add Watch… Look at the options available, but for now we will just accept the defaults, so click OK. Look at the options available, but for now we will just accept the defaults, so click OK. Notice how the Watches window shows up, and lists the expression sText as well as show its content. Notice how the Watches window shows up, and lists the expression sText as well as show its content. We can watch objects as well, and then we can easily check the contents of its various properties. Try! We can watch objects as well, and then we can easily check the contents of its various properties. Try!
54
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 54 Error Handling Error Handling really deserves a session of its own. Error Handling really deserves a session of its own. However, Visual Basic will intercept errors that you don't explicitly handle. That can be a bad thing, because VB won't know what to do, and quite often will just terminate the application after showing a cryptic (to the user) error message. However, Visual Basic will intercept errors that you don't explicitly handle. That can be a bad thing, because VB won't know what to do, and quite often will just terminate the application after showing a cryptic (to the user) error message.
55
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 55 Error Handling Implement Error Handling by using the statement: Implement Error Handling by using the statement: On Error GoTo LabelName And add some logic, maybe logging of the error, and display a user-friendly message in code following the label. And add some logic, maybe logging of the error, and display a user-friendly message in code following the label. Be especially ready to handle errors as a result of invalid input, file access, database access, etc. Be especially ready to handle errors as a result of invalid input, file access, database access, etc.
56
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 56 Error Handling Sample: Sample: On Error GoTo ErrHandler … Exit Sub ErrHandler: lErr = Err.Number lErr = Err.Number sErr = Err.Description sErr = Err.Description LogError lErr, "ModuleName.ProcName", sErr LogError lErr, "ModuleName.ProcName", sErr sMsg = "Error " & lErr & "." & vbCr & sErr sMsg = "Error " & lErr & "." & vbCr & sErr Resume Next Resume Next
57
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 57 Error Handling On Error Resume Next On Error Resume Next Check for errors between statements Check for errors between statements On Error GoTo 0 On Error GoTo 0 Reset error handler (turn it off) Reset error handler (turn it off)
58
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 58 Error Handling Resuming execution of non-error handler: Resuming execution of non-error handler: Resume Resume In an error handler, means try the failing statement again In an error handler, means try the failing statement again Resume Next Resume Next Skip the failed statement, but continue with the one following it. Skip the failed statement, but continue with the one following it. Resume Label Resume Label Continue execution from Label location Continue execution from Label location
59
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 59 Error Handling Watch out for errors in the error handler – they won't be dealt with, but caught by VB instead (usually not a good thing). Watch out for errors in the error handler – they won't be dealt with, but caught by VB instead (usually not a good thing). Always end the error handler code with a form of Resume or Exit Sub/Exit Function, etc. Always end the error handler code with a form of Resume or Exit Sub/Exit Function, etc.
60
4/20/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore BostrupPage 60 The End Thanks Thanks I have enjoyed it I have enjoyed it We will continue in the VBSIG… We will continue in the VBSIG…
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.