The Watch Window

The Watch Window allows you to watch certain expressions, in certain modules and/or procedures, and then stop code execution when it changes. To show the Watch Window, select Watch Window from the View menu.

Adding a Watch

To add a watch, select Add Watch from the Debug Menu. The Add Watch dialog will be displayed:

The Add Watch Dialog

Next, enter a Variable name or expression into the Expression Box. All the expressions listed below are valid

gNum = True
gNum < 10

Select the context of the expression: Select the procedure you want the expression to be tested in, or select (All Procedures). Then select the module you want it to be tested in, or select (All Modules). You can also select which project, if you have more than one project open.

Now select a watch type. These are listed below:

Watch Expression - Code will not be stopped, but when you enter break mode the value of the expression will be displayed in the Watch Window

Break Whan Value Is True - Visual Basic will enter Break Mode when the expression is True (a non-zero value)

Break When Value Changes - Visual Basic will enter Break Mode whenever the value of the expression changes.

As an example, create a new project with two command buttons and enter the following code:

Public gTest As Integer
Sub Command1_click()
    gTest = 1
End Sub
Sub Command2_click()
    gTest = 0
End Sub

Now, add a watch with the expression 'gTest', that is valid in all procedures and that will break when the value is true (ie a non-zero value). Run the Project, and press Command1. Visual Basic will now enter Break Mode, as gTest is now a non-zero value (ie 1). You will also see the gTest watch in the Watch Window, with its value 1. Press F5, and now press Command2. Visual Basic will not enter Break Mode because gTest = 0. Now try pressing Command1, pressing F5 to resume code execution and pressing Command1 again. Visual Basic will not enter Break Mode because the value of gTest is still a non-zero value.

As another example, change the expression to gTest = 0, and the Watch Type to break when the value changes. When you press Command1 Visual Basic will enter break mode, as the expression gTest = 0 is now False. Press F5, and now press Command2. Visual Basic will enter break mode again, because now the expression gTest = 0 is True.

You might also like...


About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone” - Bjarne Stroustrup