Windows Forms is a framework available in Visual Studio that allows you to create desktop applications with the help of a graphical user interface. It allows you to click and drag widgets such as buttons or labels directly onto the canvas, and manipulate each widget’s properties such as its font-size, color, or border.
In this article, a simple Celsius to Fahrenheit converter will be used as an example to learn the basics of how to set up a Windows Forms application. Visual Studio 2019 Community Edition is the version used for this tutorial.
Add a name and location for the project, and click Next. Location is the directory where the code files will be stored.
How to Add Elements to the Project Canvas
The canvas is the white area at the top-left side of the screen. Click and drag points on the bottom, right, or bottom-right of the canvas to resize it if necessary.
To create the UI of the application, add widgets such as buttons or text boxes to the canvas.
Drag two more text boxes with three labels (two labels for each text box, and one label for the title at the top of the application) onto the canvas.
Each widget on the canvas has properties associated with them. Highlight the widget to display the Properties window at the bottom right of Visual Studio, which lists all of the widget’s properties. These properties can include the text, name, font size, border, or alignment of the highlighted widget.
For the time being, the text on these widgets still says label1, label2, or button1. Select the Label 1 widget and edit the Text property to say “Celsius to Fahrenheit” in the Properties window. Change the font size to 22pt.
How to handle events and write code in code-behind
Widgets on the canvas can be attached to events. Events can include things like clicking a button, changing text inside a text box, or selecting a particular radio button. When these events occur, it can cause a piece of code in the code-behind to be triggered.
C# is the language used when creating Windows Forms. If you haven’t used C# already, there are many practical reasons to learn C# programming.
For this particular application, add an event to the Calculate button, to trigger a section of code to run when this button is pressed.
This is where you’ll add the code that will calculate Celsius to Fahrenheit, and display the result in the Fahrenheit text box. To do this, you need to be able to modify the Fahrenheit text box to read the value from the Celsius text box and display the result.
Go back to the canvas, and edit the properties again as shown before. This time, edit the Name property for both the Celsius and Fahrenheit text boxes. These names can be used to refer to text boxes in code.
The celsiusValue variable now stores the value that the user entered in the Celsius text box. The formula to convert Celsius to Fahrenheit is (CelsiusValue * 9/5) + 32. Therefore, the result can be calculated and stored in the Fahrenheit text box.
If the program is blurry at runtime, it’s likely that your application is not DPI aware. This can cause scaling and resolution issues, so it will need to be enabled. You can also read more about configuring display scaling on Windows 10 for High-DPI monitors.
For this change to take effect, run the program again. Select the red Stop button at the top of the toolbar, then select the green Play button again.
Running a program using an executable file
If you don’t want to run your program through Visual Studio, use a standalone executable file for the program. It is generated automatically.
Adding More to Your Windows Forms
Hope you now have a basic understanding of the basic structure of a Windows Forms application. You can continue to discover additional Windows Forms features by experimenting with new widgets and diving deeper into the various other developments that can be handled.
Once you become more familiar with Windows Forms, you can start building more complex applications. You can also explore many other ways to create applications on the Windows desktop.