Visual Basic Concepts

Using a Breakpoint to Selectively Halt Execution

At run time, a breakpoint tells Visual Basic to halt just before executing a specific line of code. When Visual Basic is executing a procedure and it encounters a line of code with a breakpoint, it switches to break mode.

You can set or remove a breakpoint in break mode or at design time, or at run time when the application is idle.

To set or remove a breakpoint

  1. In the Code window, move the insertion point to the line of code where you want to set or remove a breakpoint.

    -or-

    Click in the margin on the left edge of the Code window next to the line where you want to set or remove a breakpoint.

  2. From the Debug menu, choose Toggle Breakpoint.

    -or-

    Click the Toggle Breakpoint button on the Debug toolbar. (To display the Debug toolbar, right-click on the Visual Basic toolbar and select the Debug option.)

    -or-

    Press F9.

When you set a breakpoint, Visual Basic highlights the selected line in bold, using the colors that you specified on the Editor Format tab of the Options dialog box, available from the Tools menu.

For example, Figure 13.16 shows a procedure with a breakpoint on the fifth line. In the Code window, Visual Basic indicates a breakpoint by displaying the text on that line in bold and in the colors specified for a breakpoint.

Figure 13.16   A procedure halted by a breakpoint

Identifying the Current Statement

In Figure 13.16, a rectangular highlight surrounds the seventh line of code. This outline indicates the current statement, or next statement to be executed. When the current statement also contains a breakpoint, only the rectangular outline highlights the line of code. Once the current statement moves to another line, the line with the breakpoint is displayed in bold and in color again.

To specify the color of text of the current statement

  1. From the Tools menu, choose Options and click the Editor Format tab on the Options dialog box.

  2. Under Code Colors, select Execution Point Text, and set the Foreground, Background, and Indicator colors.

Examining the Application at a Breakpoint

Once you reach a breakpoint and the application is halted, you can examine the application's current state. Checking results of the application is easy, because you can move the focus among the forms and modules of your application, the Code window, and the debugging windows.

A breakpoint halts the application just before executing the line that contains the breakpoint. If you want to observe what happens when the line with the breakpoint executes, you must execute at least one more statement. To do this, use Step Into or Step Over.

Note*   **Although it’s possible to set a breakpoint in a MouseMove event procedure or in a Timer event, this can cause unexpected results. The normal flow of events is disrupted when entering break mode; single-stepping through the code from within these procedures may present different behavior than that which would occur in run mode.*

For More Information   See the section, "Running Selected Portions of Your Application," later in this chapter.

When you are trying to isolate a problem, remember that a statement might be indirectly at fault because it assigns an incorrect value to a variable. To examine the values of variables and properties while in break mode, use the Locals window, Quick Watch, watch expressions, or the Immediate window.

For More Information   To learn how to use the Immediate window to test the values of properties and variables, see "Testing Data and Procedures with the Immediate Window," later in this chapter. To learn more about watch expressions, see "Monitoring Data with Watch Expressions."