温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.muo.com/windows-forms-application-debug-visual-studio/
点击访问原文链接

Winforms: The Basics of Debugging Using Visual Studio

Winforms: The Basics of Debugging Using Visual Studio Menu Sign in now Close Trending Submenu Productivity Android Smart TVs Networking Windows 11 Entertainment PC & Mobile Submenu Windows2 Linux Android Apple Technical Submenu Tech Explained Security Software Submenu Productivity Internet Creative Screen Submenu Entertainment Streaming Home Submenu Smart Home Home News Sign in Newsletter Menu Follow Followed Like More Action Sign in now Productivity Android Smart TVs Networking Windows 11 Entertainment Close
Winforms: The Basics of Debugging Using Visual Studio Unsplash - no attribution
Source - https://unsplash.com/photos/w7ZyuGYNpRQ  Follow Followed Like Link copied to clipboard Add us on By  Shay Lynn Khan Published Nov 5, 2022, 10:00 AM EDT Shay loves learning new things through personal projects. Outside coding, Shay also loves gaming and playing the piano. Sign in to your MakeUseOf account

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.

Create a new Windows Form application. Search for a button using the Toolbox. Drag a new button from the Toolbox onto the canvas. Screenshot by Sharlene Von Drehnen 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

Screenshot by Sharlene Von Drehnen Click and drag a text box onto the canvas from the Toolbox. Place it to the left of the Convert button. Screenshot by Sharlene Von Drehnen 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

Click and drag another text box onto the canvas from the Toolbox. Place it to the right of the Convert button. Screenshot by Sharlene Von Drehnen Highlight the new text box, and navigate to the properties window. Change its properties to the following:

Property

New Value

Name

txtFahrenheit

Enabled

False

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)
{
}
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();
}
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;
} 
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. Screenshot by Sharlene Von Drehnen 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.

Click on the green play button at the top of the Visual Studio window. Screenshot by Sharlene Von Drehnen Enter a number into the text box, and click on the Convert button. Screenshot by Sharlene Von Drehnen 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. Screenshot by Sharlene Von Drehnen 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. Screenshot by Sharlene Von Drehnen Click on the Step Over button to highlight the next line in the code. Screenshot by Sharlene Von Drehnen 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. Screenshot by Sharlene Von Drehnen The highlighted line now contains a call to the CelciusToFahrenheit() function. Click on the Step Into button to enter this function. Screenshot by Sharlene Von Drehnen Click on the Step Out button to exit the CelciusToFahrenheit() function and go back to the btnConvert_Click() function. Screenshot by Sharlene Von Drehnen 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.

Inside the CelciusToFahrenheit() function, modify the calculation. This will result in the wrong answer.
double result = value * 9 / 5;
Remove all existing breakpoints by clicking on them. Add a new breakpoint in the CelciusToFahrenheit() function, at the point where you are returning the result. Screenshot by Sharlene Von Drehnen With the application running, re-enter a number into the text box and click on the Convert button. Screenshot by Sharlene Von Drehnen 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. Screenshot by Sharlene Von Drehnen Type in the name of an existing variable, such as "result". Press Enter on your keyboard to print its current value at that point. Screenshot by Sharlene Von Drehnen 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
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
Screenshot by Sharlene Von Drehnen 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!

Close
Recommended Debugging in JavaScript: Logging to the Browser Console I replaced Windows Search with a free tool and my files are now instant to find I ditched my iPad for comics thanks to this new e-reader Claude just released a Mythos-level model, but you only have 10 days to try it Join Our Team Our Audience About Us Press & Events Media Coverage Contact Us Follow Us Advertising Careers Terms Privacy Policies MakeUseOf is part of the Valnet Publishing Group Copyright © 2026 Valnet Inc.

Winforms: The Basics of Debugging Using Visual Studio,AI智能索引,全网链接索引,智能导航,网页索引

    Bugs are an inevitability; it