Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Walkthrough: Debugging a Windows Form

A Windows Form is one of the most common managed applications. A Windows Form creates a standard Windows application. This walkthrough uses Visual Basic, Visual C#, or Visual 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.)

Next, you will create a new Windows Form in Visual Basic, Visual C#, or Visual C++.

To create the Windows form for this walkthrough

  1. From the File menu, choose New and click Project.

    The New Project dialog box appears.

  2. In the Project Types pane, select Visual Basic Projects, Visual C# Projects, or Visual C++ Projects.
  3. In the Templates pane, select Windows Application for Visual Basic and Visual C# projects, or select Windows Forms Application (.NET) for Visual C++ projects.
  4. In the Name box, give the project a unique name (for example, Walkthrough_SimpleDebug).
  5. 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.

  6. From the View menu, select Toolbox.

    The Toolbox opens. For more information, see Toolbox.

  7. In the Toolbox, click on the Button control and drag the control to the Form design surface. Drop the button on the form.
  8. In the Toolbox, click on the TextBox control and drag the control to the Form design surface. Drop the TextBox on the form.
  9. On the form design surface, double-click the button.

    This takes you to the code page. The cursor should be in button1_Click.

  10. 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 = S"Button was clicked!";
    
  11. From the Build menu, select Build Solution.

    The project should build with no errors.

Now, you are ready to begin debugging.

To debug the Windows Form created for this walkthrough

  1. 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 = S"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.

  2. From the Debug menu, choose Start.

    The Windows Form starts running.

  3. 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 the Active Solution Configuration is set 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.
  4. From the Debug menu, choose Windows, then Watch, and click Watch1.
  5. In the Watch1 window, click on a blank line. In the Name column, type textBox1.Text in Visual Basic or Visual C#, or type textBox1->Text in Visual C++; then press ENTER.

    The Watch1 window shows the value of this variable in quotation marks as:

    textBox1
    
  6. From the Debug menu, choose Step Into.

    The value of textBox1.Text changes in the Watch1window to:

    Button was clicked!
    
  7. From the Debug menu, choose Continue to resume debugging your program.
  8. On the Windows Form, click the button again.

    Visual Studio breaks execution again.

  9. Click on the red dot that represents the breakpoint.

    This removes the breakpoint from your code.

  10. From the Debug menu, choose Stop Debugging.

In Visual Studio .NET, you can attach the debugger to a running process. For most effective debugging, compile the executable as a Debug version with symbol files (PDB).

To attach to the Windows Form for debugging

  1. 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 = S"Button was clicked!";
    
  2. 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.

  3. From the Debug menu, select Processes. (You can also use Debug Processes from the Tools menu.)
  4. In the Available Processes pane, find the process name (Walkthrough_SimpleDebug.exe) in the Process column and click it.
  5. Click the Attach button.
  6. In the Attach to Process dialog box, select Common Language Runtime under Choose the project types you want to debug, then click OK.

    You have attached the debugger to your running Windows Form. Notice that your process has been added to the Debugged Processes pane of the Processes dialog box.

  7. Click Close.
  8. In your Windows Form, click the one and only button.

    The debugger breaks execution of the Windows Form at the breakpoint.

See Also

Debugging Managed Code

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft