Windows OS

How to Build Desktop Apps Using Windows Forms

Windows Forms” is a UI-based framework used to create apps for Microsoft Windows. It is rich in features, and with “Visual Studio”, a simple drag-and-drop feature it becomes much easier to design the UI. In “Visual Studio”, users can enjoy their development of “Windows Forms” using the features such as user input, controls, data binding, and graphics. It is also called the “Event Driven” approach because it requires user input for execution. Developing “Windows Forms” desktop apps is relatively easier, with everything being straightforward.

This article covers the following learning aspects:

How to Create a New Desktop App in “Visual Studio” Using “Windows Forms”?

v“Visual Studio” provides native support for “Windows Forms” apps, and users can create the apps with ease by following these steps:

Step 1: Create a New Project
Firstly, launch “Visual Studio”, and the users will be greeted with the following screen from where the “Create a new project” option can be selected to create a new project:

Step 2: Select Framework
From the next window, (scroll down or search) and choose the framework as “Windows Forms App (.NET Framework) and hit “Next”:

Step 3: Specify the Name and Location of the Project
Here, users must specify the project’s name and location in which the project will be saved for later use:

After that, select the “.NET Framework” version (Long Term Support) and click on the “Create” button to create a new project:

Here’s how you’d be welcomed after the project is created, and the things to notice here are as follows:

  • Form1.cs(Design)” is the main form’s GUI, where you can place all the controls (which will be discussed below).
  • Solution Explorer” shows all the files related to the project, which you can edit by double-clicking on them.

Following is the demonstration:

Now, the desktop app using “Windows Forms” will be created, and now we will learn about the “Toolbox” and “Controls” for customizing the created app.

What is a “Toolbox” in “Windows Forms”?

A “Toolbox” is defined as a collection of different tools in “Visual Studio” that provides easy access to the controls. It is usually found in the “Visual Studio’s” middle-left pane. However, if it isn’t found, click the “View” option from the “Menu Bar” and hit “Toolbox”. It can also be placed on the screen using the combination of “CTRL + W” keys and then hitting “X”:

Now, it will show a list of tools/controls that you can use in the project:

Suppose you want to use a “Button” in your app. To add it, drag and drop the “Button” from the “Toolbox”, as follows:

The same process can be applied for all the other controls; however, the functionality may differ.

How to Customize the Controls in “Windows Forms”?

The “Controls” can be customized by changing text or other properties in the “Windows Forms” app. To do that, right-click on the control you want to customize, and select “Properties”:

The “Properties” tab will be visible in the right-middle pane of the screen. Here, you can customize different aspects of the selected “Control”:

To change the button text, scroll down from the “Properties” tab and find “Text”, and add the new “Text” in the box next to it:

How to Create an “Event” While Building “Desktop Apps” Using the “Windows Forms”?

An “Event” is referred to as something that might happen. In “Windows Forms”, almost everything is event-driven, so you must create “Events” to give a proper flow to your application. To add an event, double-click on the particular control, and it will open a new window, which corresponds to the backend code of the GUI against the app:

Here, you can add the functionality like what happens when a user clicks on this button. Suppose we added a new “Textbox” from the “Toolbox” and we want to only show it when we click on the button; we’d add this code in the “Generated Event”:

How to Run “Desktop App” in “Visual Studio” Using “Windows Forms”?

After creating the desktop app in “Visual Studio” using “Windows Forms”, run/launch it by pressing the “ ” button from the upper-middle portion next to the project’s name:

Once executed, you will be presented with the same GUI you’ve created; test it out by clicking the button:

This article provided a basic tutorial for building “Desktop” apps using “Windows Forms”, and if you want to learn more, we have a vast library of working with “C#” that also covers most of the related topics.

Conclusion

Building “Desktop” apps using “Windows Forms” requires creating a new project and configuring the framework, or users can work with an already created project. After that, users can have their way with the “Controls” and customize them accordingly. Since it is “Event Driven”, users can generate events by double-clicking on “Control”. This guide demonstrated building desktop apps using Windows Forms.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.