Walkthrough: Debugging a Windows Form
This topic applies to:
Edition |
Visual Basic |
C# |
C++ |
Web Developer |
---|---|---|---|---|
Express |
Managed only |
|||
Standard |
Managed only |
|||
Pro and Team |
Managed only |
Table legend:
Applies |
|
Does not apply |
|
Command or commands hidden by default. |
A Windows Form is one of the most common managed applications. A Windows Form creates a standard Windows application. You can complete this walkthrough using Visual Basic, C#, or C++.
First, you must close any open solutions.
To prepare for this walkthrough
- If you already have an open solution open, close it. (From the File menu, select Close Solution.)
Create a New Windows Form
Next, you will create a new Windows Form.
To create the Windows form for this walkthrough
From the File menu, choose New and click Project.
The New Project dialog box appears.
In the Project Types pane, open the Visual Basic, Visual C#, orVisual C++ node, then
For Visual Basic or Visual C#, select the Windows node, then select Windows Form Application in the Templates pane.
For Visual C++, select the CLR node, then select Windows Form Application in the Templates pane..
In the Templates pane, select Windows Application.
In the Name box, give the project a unique name (for example, Walkthrough_SimpleDebug).
Click OK.
Visual Studio creates a new project and displays a new form in the Windows Forms designer. For more information, see Windows Forms Designer.
From the View menu, select Toolbox.
The Toolbox opens. For more information, see Toolbox.
In the Toolbox, click on the Button control and drag the control to the Form design surface. Drop the button on the form.
In the Toolbox, click on the TextBox control and drag the control to the Form design surface. Drop the TextBox on the form.
On the form design surface, double-click the button.
This takes you to the code page. The cursor should be in button1_Click.
In the function button1_Click., add the following code:
' Visual Basic textBox1.Text = "Button was clicked!" // C# textBox1.Text = "Button was clicked!"; // C++ textBox1->Text = "Button was clicked!";
From the Build menu, select Build Solution.
The project should build with no errors.
Debug Your Form
Now, you are ready to begin debugging.
To debug the Windows Form created for this walkthrough
In the source window, click the left margin on the same line as the text you added:
' Visual Basic textBox1.Text = "Button was clicked!" // C# textBox1.Text = "Button was clicked!"; // C++ textBox1->Text = "Button was clicked!";
A red dot appears and the text on the line is highlighted in red. The red dot represents a breakpoint. For more information, see Breakpoints. When you run the application under the debugger, the debugger will break execution at that location when the code is hit. You can then view the state of your application and debug it.
From the Debug menu, choose Start.
The Windows Form starts running.
On the Windows Form, click the button you added.
In Visual Studio, this takes you to the line where you set your breakpoint on the code page. This line should be highlighted in yellow. You can now view the variables in your application and control its execution. Your application has now stopped executing, waiting for an action from you.
Note
If you have set the Active Solution Configuration to Release, execution will not stop at the breakpoint. This simulates the behavior of an actual release program. The circle that marks the breakpoint will contain a white question mark. If necessary, use the Configuration Manager dialog box to change this setting back to Debug.
From the Debug menu, choose Windows, then Watch, and click Watch1.
In the Watch1 window, click on a blank row. In the Name column, type textBox1.Text (if you are using Visual Basic, Visual C#, or J#) or textBox1->Text (if you are using C+), then press ENTER.
The Watch1 window shows the value of this variable in quotation marks as:
""
From the Debug menu, choose Step Into.
The value of textBox1.Text changes in the Watch1window to:
Button was clicked!
From the Debug menu, choose Continue to resume debugging your program.
On the Windows Form, click the button again.
Visual Studio breaks execution again.
Click on the red dot that represents the breakpoint.
This removes the breakpoint from your code.
From the Debug menu, choose Stop Debugging.
Attach to Your Form for Debugging
In Visual Studio 2005, you can attach the debugger to a running process. If you are using an Express Edition, this feature is not supported.
To attach to the Windows Form for debugging
In the project you created above, click in the left margin to once again set a breakpoint at the line you added:
' Visual Basic textBox1.Text = "Button was clicked!" // C# textBox1.Text = "Button was clicked!" // C++ textBox1->Text = "Button was clicked!";
From the Debug menu, select Start Without Debugging.
The Windows Form starts running under Windows, just as if you had double-clicked its executable. The debugger is not attached.
From the Debug menu, select Attach to Process. (This command is also available from the Tools menu.)
The Attach to Process dialog box appears.
In the Available Processes pane, find the process name (Walkthrough_SimpleDebug.exe) in the Process column and click it.
Click the Attach button.
In your Windows Form, click the one and only button.
The debugger breaks execution of the Windows Form at the breakpoint.