title | description | author | ms.author | manager | ms.subservice | ms.topic | dev_langs | ms.date | ms.custom | |
---|---|---|---|---|---|---|---|---|---|---|
Tutorial: Create Windows Forms app (Visual Basic) |
Create a Windows Forms app in Visual Studio with Visual Basic, add a button to the form, add a label and code, and run the application. |
anandmeg |
meghaanand |
mijacobs |
general-ide |
tutorial |
|
01/24/2025 |
vs-acquisition |
In this tutorial, you create a Visual Basic application that has a Windows Forms user interface. The Visual Studio integrated development environment (IDE) includes all the tools you need to create a Windows Forms app.
In this tutorial, you learn how to:
[!div class="checklist"]
- Create a project
- Add a button to the form
- Add a label and code
- Run the application
::: moniker range="vs-2019"
- You need Visual Studio to complete this tutorial. Visit Visual Studio downloads for a free version.
- The .NET desktop development workload. To verify or install this workload in Visual Studio, select Tools > Get Tools and Features. For more information, see Change workloads or individual components. ::: moniker-end ::: moniker range=">=vs-2022"
- You need Visual Studio to complete this tutorial. Visit Visual Studio downloads for a free version.
- The .NET desktop development workload. To verify or install this workload in Visual Studio, select Tools > Get Tools and Features. For more information, see Change workloads or individual components. ::: moniker-end
Create a Visual Basic application project. The project type comes with all the template files that you need.
::: moniker range="vs-2019"
-
Open Visual Studio, then select Create a new project on the start window.
:::image type="content" source="../get-started/media/vs-2019/create-new-project-dark-theme.png" alt-text="Screenshot showing the Visual Studio 2019 start window with Create a new project selected.":::
-
On the Create a new project window, select the Windows Forms App (.NET Framework) template for Visual Basic.
You can refine your search to quickly get to the template you want. For example, enter Windows Forms App in the search box. Next, select Visual Basic from the languages list, and then Windows from the platform list.
:::image type="content" source="../get-started/visual-basic/media/vs-2019/vb-create-new-project-search-winforms-filtered.png" alt-text="Screenshot showing the Create a new project window with Windows Forms App (.NET Framework) selected." lightbox="../get-started/visual-basic/media/vs-2019/vb-create-new-project-search-winforms-filtered.png":::
-
In the Configure your new project window, enter HelloWorld as the Project name. Then, select Create.
:::image type="content" source="../get-started/visual-basic/media/vs-2019/vb-name-your-winform-project-helloworld.png" alt-text="Screenshot shows the Configure your new project window with the name HelloWorld entered.":::
Visual Studio opens your new project.
::: moniker-end
::: moniker range=">=vs-2022"
-
Open Visual Studio, then select Create a new project on the start window.
:::image type="content" source="../get-started/media/vs-2022/create-new-project.png" alt-text="Screenshot that shows the Visual Studio 2022 start window with Create a new project selected.":::
-
On the Create a new project window, select the Windows Forms App (.NET Framework) template for Visual Basic.
You can refine your search to quickly get to the template you want. For example, enter Windows Forms App in the search box. Next, select Visual Basic from the language list, and then Windows from the platform list.
:::image type="content" source="../get-started/visual-basic/media/vs-2022/vb-create-new-project-search-winforms-filtered.png" alt-text="Screenshot that shows the Create a new project window with Windows Forms App (.NET Framework) selected." lightbox="../get-started/visual-basic/media/vs-2022/vb-create-new-project-search-winforms-filtered.png":::
-
In the Configure your new project window, enter HelloWorld as the Project name. Then, select Create.
:::image type="content" source="../get-started/visual-basic/media/vs-2022/vb-name-your-winform-project-helloworld.png" alt-text="Screenshot shows the Configure your new project window with the name HelloWorld entered.":::
Visual Studio opens your new project.
::: moniker-end
After you select your Visual Basic project template and name your file, Visual Studio opens a form for you. A form is a Windows user interface. You create a Hello World application by adding controls to the form.
-
On the left side of the Visual Studio IDE, select the Toolbox tab. If you don't see it, select View > Toolbox from the menu bar or Ctrl+Alt+X.
:::image type="content" source="media/create-a-visual-basic-winform-in-visual-studio/toolbox-tab.png" alt-text="Screenshot shows the Toolbox tab that opens Toolbox window.":::
If you want, select the Pin icon to dock the Toolbox window.
-
In the Common Controls section, select the Button control and then drag it onto the form.
:::image type="content" source="media/create-a-visual-basic-winform-in-visual-studio/toolbox-button-form.png" alt-text="Screenshot shows the Button control added to the form.":::
-
In the Appearance section of the Properties window, for Text, type Click this, and then select Enter.
:::image type="content" source="media/create-a-visual-basic-winform-in-visual-studio/button-text-property.png" alt-text="Screenshot shows the Text property with the value Click this.":::
If you don't see the Properties window, you can open it from the menu bar. Select View > Properties Window or F4.
-
In the Design section of the Properties window, change the name from Button1 to btnClickThis, and then select Enter.
:::image type="content" source="media/create-a-visual-basic-winform-in-visual-studio/button-name-property.png" alt-text="Screenshot shows the Name property with a value of btnClickThis.":::
[!NOTE] If you've alphabetized the list in the Properties window, Button1 appears in the (DataBindings) section, instead.
After you add a button control to create an action, add a label control to receive the text.
-
Select the Label control in the Toolbox window, and then drag it onto the form. Place it beneath the Click this button.
-
In either the Design section or the (DataBindings) section of the Properties window, change the name Label1 to lblHelloWorld, and then press Enter.
-
In the Form1.vb [Design] window, double-click the Click this button to open the Form1.vb window.
Another option is to expand Form1.vb in Solution Explorer. Then select Form1.
-
In the Form1.vb window, between the
Private Sub
andEnd Sub
lines, enter lblHelloWorld.Text = "Hello World!" as shown in the following screenshot::::image type="content" source="media/create-a-visual-basic-winform-in-visual-studio/click-handle-code-visual-basic.png" alt-text="Screenshot shows a class in the Form1.vs tab where you can add Visual Basic code.":::
Your application is ready to build and run.
-
Select Start to run the application.
:::image type="content" source="media/create-a-visual-basic-winform-in-visual-studio/visual-studio-start-debug.png" alt-text="Screenshot shows the Start button that runs your app.":::
Several things happen. In the Visual Studio IDE, the Diagnostics Tools window opens, and an Output window opens. Outside of the IDE, a Form1 dialog box appears. It includes your Click this button and text that says Label1.
-
Select the Click this button in the Form1 dialog box.
:::image type="content" source="media/create-a-visual-basic-winform-in-visual-studio/visual-studio-dialog-hello-world.png" alt-text="Screenshot shows dialog box titled Form 1 that displays the text Hello World!":::
The Label1 text changes to Hello World!.
-
Close the Form1 dialog box to stop running the app.
To learn more about Windows Forms, continue with the following tutorial:
[!div class="nextstepaction"] Tutorial: Create a picture viewer
Or try these other tutorials: