When creating a new Windows Form application, you’ll experience issues or bugs in your code. This is part of the normal development process, and you’ll need to debug your code to find the cause of any problems.

Visual Studio has many tools you can use to help you debug parts of your code. This includes adding breakpoints to certain lines of code and stepping through them line by line. This will help you to identify the values of variables at certain points.

You can also use the immediate window to print certain variables onto the command line.

How to Add Breakpoints to the C# File

Before running your application, you can add multiple breakpoints to certain lines of code, within the C# code-behind files. Once you run the application, the program will pause when it hits a breakpoint. You can then view a list of variables and all their values at that point.

  1. Create a new Windows Form application.
  2. Search for a button using the Toolbox. Drag a new button from the Toolbox onto the canvas.
    New button on a Winforms canvas
  3. Highlight the button, and navigate to the properties window. Change its properties to the following new values:

    Property

    New Value

    Name

    btnConvert

    Size

    200, 80

    Text

    Convert

    Button selected on canvas with properties window opened
  4. Click and drag a text box onto the canvas from the Toolbox. Place it to the left of the Convert button.
    New textbox on Winforms canvas
  5. Highlight the new text box, and navigate to the properties window. Change its properties to the following new values:

    Property

    New Value

    Property

    New Value

    Name

    txtCelcius

  6. Click and drag another text box onto the canvas from the Toolbox. Place it to the right of the Convert button.
    New textbox on Winforms canvas
  7. Highlight the new text box, and navigate to the properties window. Change its properties to the following:

    Property

    New Value

    Name

    txtFahrenheit

    Enabled

    False

  8. On the canvas, double-click on the Convert button. This will generate a new function in the code-behind file, called btnConvert_Click(). Events in a Windows Form application can trigger certain functions to execute. For instance, this function will trigger when the user clicks on the Convert button at runtime.
            private void btnConvert_Click(object sender, EventArgs e)
    {
    }
  9. Add the following code in the function. This will get the value entered into the first text box, and use the CelciusToFahrenheit() function to convert it to Fahrenheit. It will then display the final result back to the user.
            private void btnConvert_Click(object sender, EventArgs e)
    {
        // Gets the value entered into the first text box
        double celsiusValue = Double.Parse(txtCelcius.Text);
     
        // Calculation
        double result = CelciusToFahrenheit(celsiusValue);
     
        // Display the result
        txtFahrenheit.Text = result.ToString();
    }
  10. Create the CelciusToFahrenheit() function. This function will convert a number from Celsius to Fahrenheit, and return the result.
            private double CelciusToFahrenheit(double value)
    {
        // Formula to convert Celcius to Fahrenheit
        double result = (value * 9 / 5) + 32;
     
        return result;
    }
  11. Add a breakpoint for the first line of the btnConvert_Click() function. Add the breakpoint by clicking on the space located on the leftmost side of the code window.
    New breakpoint added in function

How to Step Through the Functions and View Local Variable Values

When you run your application, it will stop at the first breakpoint. You can use the Step Into, Step Over, or Step Out buttons to debug each line of the application.

  1. Click on the green play button at the top of the Visual Studio window.
    Green play button at the top of Visual Studio
  2. Enter a number into the text box, and click on the Convert button.
    Textbox input at runtime on Winforms app
  3. The program will pause, and open the C# file where your breakpoint is. In this case, it will pause in the btnConvert_Click() function, as you triggered this function by clicking on Convert.
    Breakpoint in Winforms code
  4. At the top of the Visual Studio window, you can use the Step Into (F11), Step Over (F10), and Step Out (Shift + F11) buttons to click through the program, line by line. This allows you to debug all variables and values for a highlighted line.
    Step Over Into and Out buttons
  5. Click on the Step Over button to highlight the next line in the code.
    Breakpoint in Winforms code
  6. At the bottom of the Visual Studio application, click on the Locals tab. This allows you to view the values of all the variables at that point in the program.
    Local variables tab in Winforms application
  7. The highlighted line now contains a call to the CelciusToFahrenheit() function. Click on the Step Into button to enter this function.
    Breakpoint in Winforms app
  8. Click on the Step Out button to exit the CelciusToFahrenheit() function and go back to the btnConvert_Click() function.
    Breakpoint in Winforms app

How to View Values Using the Immediate Window

The immediate window tab allows you to print the values of any existing variables, and can be a very useful tool while debugging. You can also write one-line freestyle code, to experiment with getting different results and values.

  1. Inside the CelciusToFahrenheit() function, modify the calculation. This will result in the wrong answer.
            double result = value * 9 / 5;
        
  2. Remove all existing breakpoints by clicking on them.
  3. Add a new breakpoint in the CelciusToFahrenheit() function, at the point where you are returning the result.
    Breakpoint in Winforms app
  4. With the application running, re-enter a number into the text box and click on the Convert button.
    Winforms app at runtime with value entered into the textbox
  5. Once the program stops at your new breakpoint, click on the Immediate Window tab. You can find this at the bottom-right of the Visual Studio application.
    Immediate window tab opened in Visual Studio
  6. Type in the name of an existing variable, such as "result". Press Enter on your keyboard to print its current value at that point.
    Immediate window tab printing the result variable
  7. You can also type your own lines of code, pressing Enter on your keyboard after each line to view the result. Type in the following line of code into the immediate window tab, and press enter:
            value * 9 / 5
        
  8. In the immediate window, type in the correct calculation to see the correct result. Typing lines of code this way allows you to quickly and conveniently view new values, and to find what could be causing the issue. Here, you can see that the incorrect result is due to the incorrect calculation.
            (value * 9 / 5) + 32
        
    Immediate window tab printing values

Debugging Winforms Applications to Investigate Errors

Hopefully, you now understand how to debug a Windows Form application. By using breakpoints, and stepping through each line, you should now be able to determine the root cause of any issues that may be occurring.

Now you can continue to maintain and keep any apps you build almost bug-free!