This article covers the following learning aspects:
- How to Create a New Desktop App in Visual Studio Using Windows Forms?
- What is a Toolbox in Windows Forms?
- How to Customize the Controls in Windows Forms?
- How to Create an Event While Building Desktop Apps Using Windows Forms?
- How to Run Desktop Apps in Visual Studio Using Windows Forms?
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.
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.