Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this tutorial, you learn how to use Visual Studio to create a Windows Forms app. By using Visual Studio, you add controls to a form and handle events. By the end of this tutorial, you have a simple app that adds names to a list box.
In this tutorial, you:
- Create a new Windows Forms app.
- Add controls to a form.
- Handle control events to provide app functionality.
- Run the app.
Prerequisites
- Visual Studio 2026
- Select the .NET desktop development workload
- Select the .NET 10 individual component
Create a Windows Forms app
The first step to creating a new app is opening Visual Studio and generating the app from a template.
Open Visual Studio.
Select Create a new project.
In the Search for templates box, type winforms, and wait for the search results to appear.
In the code language dropdown, choose C# or Visual Basic.
In the list of templates, select Windows Forms App and then select Next.
Important
Don't select the Windows Forms App (.NET Framework) template.
The following image shows both C# and Visual Basic .NET project templates. If you applied the code language filter, the corresponding template is listed.
In the Configure your new project window, set the Project name to Names and select Next.
You can also save your project to a different folder by adjusting the Location path.
Finally, in the Additional information window, select .NET 10.0 (Long Term Support) for the Framework setting, and then select Create.
After Visual Studio generates the app, it opens the designer window for the default form, Form1. If the form designer isn't visible, double-click on the form in the Solution Explorer window to open the designer window.
Enable the Toolbox and Properties windows
By default, Visual Studio doesn't open the Toolbox and Properties windows. Visual Studio does remember that you opened these windows and keeps them open in the future.
To open the windows:
- Select the View > Toolbox menu item.
- Select the View > Properties Window menu item.
Important parts of Visual Studio
Support for Windows Forms in Visual Studio has four important components that you interact with as you create an app:
Solution Explorer
All of your project files, code, forms, and resources appear in this window.
Properties
This window shows property settings you can configure based on the context of the item selected. For example, if you select an item from Solution Explorer, you see settings related to the file. If you select an object in the Designer, you see the properties of the control or form.
Form Designer
This is the designer for the form. It's interactive and you can drag and drop objects from the Toolbox. By selecting and moving items in the designer, you can visually compose the user interface (UI) for your app.
Toolbox
The toolbox contains all of the controls you can add to a form. To add a control to the current form, double-click a control or drag and drop the control to the form.
Add controls to the form
With the Form1 form designer open, use the Toolbox window to add the following controls to the form by dragging them from the toolbox and dropping them on the form:
- Button
- Label
- ListBox
- TextBox
Position and size the controls according to the following image:
You can either move and resize the controls with the mouse to match the previous image, or use the following table to configure each control. To configure a control, select it in the designer, then set the appropriate setting in the Properties window. When configuring the form, select the form's title bar.
| Object | Setting | Value |
|---|---|---|
| Label | Location | 12, 9 |
| Text | Names |
|
| ListBox | Name | lstNames |
| Location | 12, 27 |
|
| Size | 120, 94 |
|
| TextBox | Name | txtName |
| Location | 138, 26 |
|
| Size | 100, 23 |
|
| Button | Name | btnAdd |
| Location | 138, 55 |
|
| Size | 100, 23 |
|
| Text | Add Name |
|
| Form | Text | Names |
| Size | 268, 180 |
Handle events
After you lay out all the controls on the form, add event handlers to respond to user input. Go to the form designer and perform the following steps:
Select the Add Name button control on the form.
In the Properties window, select the events icon
to list the events of the button.Find the Click event and double-click it to generate an event handler.
This action adds the following code to the form:
private void btnAdd_Click(object sender, EventArgs e) { }Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click End SubThe code for this handler adds the name specified by the
txtNametextbox to thelstNameslistbox. However, you want to add two conditions for adding the name: the name can't be blank, and the name can't already exist.The following code demonstrates adding a name to the
lstNamescontrol:private void btnAdd_Click(object sender, EventArgs e) { if (!string.IsNullOrWhiteSpace(txtName.Text) && !lstNames.Items.Contains(txtName.Text)) lstNames.Items.Add(txtName.Text); }Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click If Not String.IsNullOrWhiteSpace(txtName.Text) And Not lstNames.Items.Contains(txtName.Text) Then lstNames.Items.Add(txtName.Text) End If End Sub
Run the app
After you handle the event, run the app by pressing the F5 key or by selecting Debug > Start Debugging from the menu. When the app starts, the form is displayed. Enter a name in the textbox and select the button.
Related content
.NET Desktop feedback