संपादित करें

इसके माध्यम से साझा किया गया


Tutorial: Create a Windows Forms app in Visual Studio with C#

In this tutorial, you create a simple C# application that has a Windows-based user interface (UI). The app has a button that changes the text of a label. This simple app has all the components used for more complicated Forms programs.

  • Create a C# project in Visual Studio
  • Create an application
  • Run your application

Prerequisites

Create a project

First, create a C# application project. The project type comes with all the template files you need to create your application.

  1. Open Visual Studio.

  2. On the start window, choose Create a new project.

    Screenshot shows the Create a new project window.

  3. In Create a new project, choose the Windows Forms App (.NET Framework) template for C#.

    Screenshot shows the  Windows Forms App (.NET Framework) for C#.

    You can refine your search to quickly get to the template you want. For example, enter Windows Forms App in the search box. Next, choose C# from the language list, and then choose Windows from the platform list.

  4. In Configure your new project, for Project name, enter HelloWorld and select Create.

    Screenshot shows the Configure your new project window, where you name your project HelloWorld.

    Visual Studio opens your new project.

  1. Open Visual Studio.

  2. On the start window, select Create a new project.

    Screenshot shows the Create a new project option.

  3. In Create a new project, select the Windows Forms App (.NET Framework) template for C#.

    You can refine your search to quickly get to the template you want. For example, type Windows Forms App in the search box. Next, select C# from the language list, and then select Windows from the platform list.

    Screenshot shows the C# template for the Windows Forms App (.NET Framework).

  4. In the Configure your new project window, in Project name, enter HelloWorld, and select Create.

    Screenshot shows the Configure your new project window for your project named HelloWorld.

    Visual Studio opens your new project.

Create the application

After you select your C# project template and name your project, Visual Studio opens a form for you. A form is a Windows user interface. Create a Hello World application by adding controls to the form. Then run the app.

Add a button to the form

  1. Choose Toolbox to open the Toolbox flyout window.

    Screenshot shows the Toolbox option to open the Toolbox window.

    If you don't see the Toolbox option, you can open it from the menu bar. Select View > Toolbox or Ctrl+Alt+X.

  2. Choose the Pin icon to dock the Toolbox window.

    Screenshot shows the Pin icon that pins the Toolbox window to the IDE.

  3. Choose the Button control and then drag it onto the form.

    Screenshot shows the form with a button.

  4. In the Properties window, locate Text, change the name from Button1 to Click this, and then select Enter.

    Screenshot shows where to change text for the button on the form.

    If you don't see the Properties window, you can open it from the menu bar. Select View > Properties Window or F4.

  5. In the Design section of the Properties window, change the name from Button1 to btnClickThis, and then select Enter.

    Screenshot shows the new name for the button.

    Note

    If you alphabetized the list in the Properties window, Button1 appears in the (DataBindings) section, instead.

Add a label to the form

After you add a button control to create an action, add a label control to send text to.

  1. Select the Label control from the Toolbox. Then drag it onto the form and drop it beneath the Click this button.

  2. In either the Design section or the (DataBindings) section of the Properties window, change the name of Label1 to lblHelloWorld. Then select Enter.

Add code to the form

  1. In the Form1.cs [Design] window, double-click the Click this button to open the Form1.cs window.

    Alternatively, you can expand Form1.cs in Solution Explorer, and then choose View Code or select F7 from the shortcut menu on Form1.cs.

  2. In the Form1.cs window, after the private void line, type or enter lblHelloWorld.Text = "Hello World!"; as shown in the following screenshot.

    Screenshot shows the code window where you add code to the form.

After you select your C# project template and name your project, Visual Studio opens a form for you. A form is a Windows user interface. Create a Hello World application by adding controls to the form. Then run the app.

Add a button to the form

  1. Select Toolbox to open the Toolbox flyout window.

    Screenshot shows how to select the Toolbox to open the Toolbox window.

    If you don't see the Toolbox option, you can open it from the menu bar. Select View > Toolbox or Ctrl+Alt+X.

  2. Expand Common Controls and select the Pin icon to dock the Toolbox window.

    Screenshot shows the Pin icon to pin the Toolbox window to the IDE.

  3. Select the Button control and then drag it onto the form.

    Screenshot shows the form with a button just added.

  4. In the Properties window, locate Text. Change the name from button1 to Click this, and then select Enter.

    Screenshot shows the Text field in the Properties window.

    If you don't see the Properties window, you can open it from the menu bar. Select View > Properties Window or F4.

  5. In the Design section of the Properties window, change the name from button1 to btnClickThis, and then select Enter.

    Screenshot shows the new name for the button.

    Note

    If you alphabetized the list in the Properties window, button1 appears in the (DataBindings) section, instead.

Add a label to the form

After you add a button control to create an action, add a label control to send text to.

  1. Select the Label control from the Toolbox. Then drag it onto the form and drop it beneath the Click this button.

  2. In either the Design section or the (DataBindings) section of the Properties window, change the name of label1 to lblHelloWorld. Then select Enter.

Add code to the form

  1. In the Form1.cs [Design] window, double-click the Click this button to open the Form1.cs window.

    Alternatively, you can expand Form1.cs in Solution Explorer, and then choose Form1.

  2. In the Form1.cs window, after the private void line, type or enter lblHelloWorld.Text = "Hello World!"; as shown in the following screenshot.

    Screenshot that shows where to add code to the form.

Run the application

  1. Select the Start button to run the application.

    Screenshot shows the Start button to debug and run the app.

    Several things happen. In the Visual Studio IDE, the Diagnostics Tools window opens, and an Output window opens, too. But outside of the IDE, a Form1 dialog box appears. It includes your Click this button and text that says Label1.

  2. Choose the Click this button in the Form1 dialog box. Notice that the Label1 text changes to Hello World!.

    Screenshot shows your app, which is a Form1 dialog box that includes Label1 text.

  3. Close the Form1 dialog box to stop running the app.

  1. Select the Start button to run the application.

    Screenshot shows the Start button to debug and run the app.

    Several things happen. In the Visual Studio IDE, the Diagnostics Tools window opens, and an Output window opens, too. But outside of the IDE, a Form1 dialog box appears. It includes your Click this button and text that says label1.

  2. Select the Click this button in the Form1 dialog box. Notice that the label1 text changes to Hello World!.

    Screenshot shows the Form1 dialog box that includes the button and a label.

  3. Close the Form1 dialog box to stop running the app.

Congratulations on completing this tutorial. To learn more, continue with the following tutorial:

Or try these other tutorials: