The only tool that you need to create a Winforms application is Visual Studio. Some die-hard coders would probably tell you that they could do it with Windows Notepad, but I prefer to use Microsoft Visual Studio, and that is what I will use in all of my blog posts.
Visual Studio Community Edition is free, but their are also some paid editions available that offer additional features. Visual Studio can be downloaded from https://visualstudio.microsoft.com/vs/.
Installing Visual Studio
Navigate to https://visualstudio.microsoft.com/vs/ and download the Visual Studio edition that you want to work with. I use Visual Studio Community Edition for personal projects and that works well for me. At work, I use Professional Edition, but the only reason for that is because of the licensing requirements. I do not choose Professional because of the additional features. Community Edition has everything I need.
By default, the installer should be downloaded to you Downloads folder. Launch the installer.
Click the Install button for the edition that you want to install. Select the .Net desktop development workload as shown below. Notice that I have removed some of the default options from the Optional components pane to the right, and I elected to install .Net Framework 4.61. The reason I chose this framework is because I use it a lot. You do not have to install .Net Framework 4.6.1. I will be using .Net Framework 4.7.2 for this demo.
You may choose to install any or all additional workloads and components. At a minimum, install .Net desktop development and .Net Framework 4.6.1 to follow along with this demo. Once you have made your choices, click the Install button in the lower right corner. Depending on your system, this installation could take a little while. By the way, you can always re-run this installer to load additional workloads or components, or remove them.
Launch Visual Studio
If Visual Studio does not automatically launch after it finishes installing, you can find it in your Start Menu. Notice that the installer is also in the Start Menu.
Go ahead and launch Visual Studio.
Let’s Create Our Hello World Project
As you can see under Get Started in the image above, you have several options for getting started on new or existing projects. One of the options is Create a New Project. Choose this option.
Right away when you stare at all of the possible choices for the type of app to create, it may seem a little overwhelming. But there are 3 combo boxes at the top of the list that will help you narrow down the choices. Choose, as I have, C# for the programming language, Windows for the Platform type, and Desktop for the Project Type.
If you installed additional Workloads, your list may look different than mine. I have the correct project template circled in green in the image above. Select “Windows Forms App (.Net Framework)” and then click the Next button.
Type your Project Name as Hello World. Change the Framework to .Net Framework 4.7.2 if it does not default to 4.7.2. Once this is done, click the Create button to create our project.
Exploring our Project / Solution
I don’t want to get into too much detail in this post because this post is just supposed to be a Hello World demo. But I do want to explain a little bit about what you are seeing as shown in the image above.
At the very top of the Solution Explorer, you see Solution ‘Hello World’ (1 of 1 project). The solution is the container for all of the related projects. Each project will compile to a single Executable (i.e. Hello World.exe) or DLL (i.e. Hello World.dll) when the solution is compiled. The default is to compile to an executable and that is what we are doing. Our solution currently contains 1 C# project called Hello World. The project also contains 1 form called Form1 and 1 class file called Program.cs.
Program.cs is the main entry point of the project. This means the when we start debugging the project, the code in Program.cs will execute first. There is code in Program.cs that will launch Form1.
App.config just contains some basic configuration info for the Hello World project. Properties are the project properties.
Let’s Rename Our Form1 to MainForm
I despise default values. Visual Studio named our form as Form1. If you had a project that contained several forms, it would be hard for any developer to keep track of the purpose of each form if they were named Form1, Form2, Form3, etc. So whenever you create a new form or button or text box, or whatever, please name it something meaningful to make it easier to remember what it is for.
Right click Form1 in the Solution Explorer and select Rename as shown in the image above. Name it MainForm with no spaces between Main and Form.
Displaying Hello World on Our Form
If the blank MainForm is not displayed in Design View as shown below, double click MainForm to open it in Design View.
Once the MainForm is open in Design View, click the background or border of the form to select it. You will see sizing handles on the right and bottom edge of the form when it is selected. You will also notice that MainForm is displayed at the top of the Properties window as shown in the image above.
When the form is selected, look at the Properties window and find the Text property. It should say Form1.
We need to change this to Hello World. Notice that in the top left border of our MainForm it says Form1. Type Hello World in place of Form1 and press Enter. Now the top left form border says Hello World.
Add a Label Control
On the left edge of the Visual Studio window, you should see the Toolbox.
If you do not see it there, go to the View Menu and Select Toolbox. Once you see it attached to the edge of the Visual Studio window, click to open it. The Toolbox contains all of the available form controls such as labels, buttons, text boxes, etc.
When the Toolbox has opened, you can see that there are various categories of controls. If you expand All Windows Forms, you will be able to view all of the available controls.
The image shows some of the available controls. The list is pretty long.
Find the Label Control and drag it to the middle of the form somewhere and then drop it.
Ok, the label is on the MainForm with a default text property of Label1. The label should also be selected by default. If you deselected it, please click the label on the form and confirm that Label1 is selected at the top of the properties window.
When you have confirmed that Label1 is selected, change the Text property to Hello World! Now find the Font Property and open it by clicking the plus sign. Change the font size to 24 and position the label at the middle of the form by dragging it.
Let’s Run Our Application!
At the top of your Visual Studio window, you should see Green Arrow button labeled Start. If you do, click it to run your application. If not, go to the View Menu and select Toolbars > Standard to display the Standard Toolbar, or press F5 to run it if you don’t want to display the Standard Toolbar.
When the application finishes compiling, you should see your form pop up and display Hello World in the middle of your form.
You can click the Minimize button in the upper right of the form to minimize the form, or the maximize button to maximize it. Of course you can close the form by clicking the X. Closing the form also stops the application from running. You can also stop the application from running by clicking the red stop button in Visual Studio.
Where Is Your Executable?
When you started the application, an Executable file was created which represents your application. In your Solution Explorer, select the top level solution node. Look in the properties window for the path to the solution file.
Navigate to that folder.
You can open your solution by double clicking the Hello World.sln file, but next time you open Visual Studio, this solution should show up as a recent solution.
Now navigate into the Hello World folder. This is the Hello World project folder.
Some of these files should seem somewhat familiar to you. You see these file in the Solution Explorer. Now open the bin (stands for binaries) folder. You will see 2 folders. One is Debug, the other is Release. We have only run our project in Debug mode, so the Debug folder is the only one that will have files right now. If we were to go back into Visual Studio and change Debug to Release and run it, then the Release folder would be populated. For now, navigate to the Debug folder.
There is your executable. Since this is a debug version of the executable, you should not distribute it. Any executables or dll’s that you want to distribute should be compiled in Release mode.
If you have any questions or comments, please leave a comment below.