This page will build your understanding for the Debug window, also called the Immediate window. I believe that we used this window in the past. So let's begin.
Open a new project and name it whatever you would like.
Name the form frmDebug.
Set the form's BorderStyle property to 1-Fixed Single and its MinButton property to True.
Double click the form to open the code window.
Type the code that centers the form on the screen.
Recall:
frmLoop.Top = (Screen.Height - frmLoop.Height) / 2
frmLoop.Left = (Screen.Width - frmLoop.Width) / 2
Below those lines, type the following lines:
Dim lngLine as Long
For lngLong = 0 To 500000 Step 500
Debug.Print "This is line "; lngLine
Next lngLine
Line 1: Begins For loop; starts variable lngLine
at zero and ends at 500000. Step 500: Increments lngLine in intervals of
500.
Line 2: Prints (writes) This is line, then the value of lngLine
in the immediate window.
Line 3: Goes to the next value of lngLine.
The Step keyword is new, but it simply means to take steps by the value whatever you specify (in this case 500). It will go from 0 to 500 to 1000 to 1500 and so forth up to 500000.
Try running this program. If you do not see the Immediate window, press Ctrl + Break, then Ctrl + G. Ctrl + G shows the immediate window. If the window was closed during runtime, it sometimes does not show the next time the program is run.
To clear the Immediate window, press Ctrl + G in the development environment (in Visual Basic when the program you write is not running), then select all the text in the Immediate window, then press Delete.
Now let's try another exercise.
Save this program only if YOU want to.
Open a new project.
Name the project Debugger, then name the form frmDebug.
Erase the form's caption with the Caption property.
Set the BorderStyle property to 1-FixedSingle and its MinButton property to True.
Double-click the command button tool in the Toolbox to add one to the form.
Name the button cmdClick.
Set the button's caption to &Click Me.
Set the button's Default property to True. This allows the user to press the enter key to use the button. Notice that a black border is placed around the button.
Now let's code the application.
Double-click the form to open the code window.
Select (General) in the code window's object box.
Type the following line:
Dim bytClicks as Byte
This simply declares the variable bytClicks as the Byte datatype. This variable will not go above 5, so we can use this datatype.
Go to the form's Load sub-procedure.
Type the following line:
bytClicks = 0
Line 1: Assigns an initial value to the variable bytClicks
so that it has a base to increment from.
Line 2: Sets the form's caption to the value of bytClicks when the
form loads.
Type the code to center the form on the screen.
Recall:
frmDebug.Top = (Screen.Height - frmDebug.Height) / 2
frmDebug.Left = (Screen.Width - frmDebug.Width) / 2
Open the code window for the object cmdClick.
Type the following lines:
bytClicks = bytClicks + 1
frmDebug.Caption = bytClicks
Line 1: Increment the variable bytClicks by 1.
Line 2: Display the value of bytClicks in the form's caption.
If you run the application, the form's caption will display the value of the Clicks variable and increment it each time you click the button or press the enter key. If you hold the enter key, the variable will increment very quickly. If you go above 255, you will get an error message saying Overflow, because the data type Byte only goes up to the value 255. Try the Integer data type to go up to 32,767 or the Long data type to go up to two billion.
Use the integer data type for the variable bytClicks.
Change the name to intClicks and the data type to Integer.
Change the lines in the form's Load procedure to read as follows:
intClicks = intClicks + 1
frmDebug.Caption = intClicks
Run the application. Press the enter key to increment the variable and have the form's caption change.
Press the Pause button to Break the application. You can also press Ctrl-Break to do the same
The Immediate window should be showing. If it is not, press Ctrl-G.
Type Print intClicks in the window. This displays the current value of the variable intClicks in the Immediate window.
If you would like, continue the application by pressing the Play button or the F5 key on the keyboard. Increment the variable some more by holding the enter key or clicking the button with the mouse.
Pause the application again and type Print intClicks in the Immediate Window again to see a different value.
Now we will use another debugging tool called a watch. A watch simply keeps track of an expression, or watches an expression, thus the name watch.
Go into the code window for the cmdClick object.
Find the line intClicks = intClicks + 1.
Highlight intClicks by double-clicking it.
Right click on the highlight and select Add Watch in the pop-up menu.
Be sure that the Break when value changes option button is selected, then press OK.
Run the application.
Notice that there is nothing in the Watch window at first.
Click the button on the program to add to the value Clicks.
The line of code frmDebug.Caption = intClicks is highlighted in the code window, and the watches window says that the Value of the variable intClicks is 1.
Press F5 to continue the application and press the button again. Notice that the same code is highlighted and the value of intClicks is now 2.
If you would like, keep going through the same process to increment the intClicks variable.
To remove the watch we created, stop the program.
Select the line in the watches window.
You can either Right-Click and select Delete, or select Edit Watch and press the Delete button.
I hope you learned a bit about debugging applications and watching expressions.
Now it is time to move on to Page 16.