Create a “Hello World” Winforms Application

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.

Visual Studio Download Page

By default, the installer should be downloaded to you Downloads folder. Launch the installer.

Choose Edition to Install

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.

Installation Choices

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.

Visual Studio in the Start Menu

Go ahead and launch Visual Studio.

Let’s Create Our Hello World Project

Create a New Project Button

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.

Choosing the C# Windows Desktop Forms App Template

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.

Naming Your Project

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

Initial Project View

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.

Renaming Form1

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.

Form1 Renamed to MainForm

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.

Open MainForm in Design View if Not Already Open

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.

MainForm Text Property Incorrectly Says 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.

ToolBox Normal Location

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.

Some of the Windows Forms 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.

Label in the Toolbox
Label on the MainForm

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.

Label 1 Selected

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.

Setting Font Size

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.

Running Application

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.

Stopping the Application

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.

Path to the Solution File

Navigate to that folder.

Solution 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.

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.

Debug Folder Contents

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.

Powered by WordPress.com.

Up ↑

%d bloggers like this: