Code in trong windows form

Chuyển đến nội dung chính

Trình duyệt này không còn được hỗ trợ nữa.

Hãy nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, bản cập nhật bảo mật và hỗ trợ kỹ thuật.

Add a control to a form (Windows Forms .NET)

  • Bài viết
  • 05/27/2021
  • 2 phút để đọc

Trong bài viết này

Most forms are designed by adding controls to the surface of the form to define a user interface (UI). A control is a component on a form used to display information or accept user input.

The primary way a control is added to a form is through the Visual Studio Designer, but you can also manage the controls on a form at run time through code.

Important

The Desktop Guide documentation for .NET 6 and .NET 5 (including .NET Core 3.1) is under construction.

Add with Designer

Visual Studio uses the Forms Designer to design forms. There is a Controls pane which lists all the controls available to your app. You can add controls from the pane in two ways:

Add the control by double-clicking

When a control is double-clicked, it is automatically added to the current open form with default settings.

Code in trong windows form

Add the control by drawing

Select the control by clicking on it. In your form, drag-select a region. The control will be placed to fit the size of the region you selected.

Code in trong windows form

Add with code

Controls can be created and then added to a form at run time with the form's Controls collection. This collection can also be used to remove controls from a form.

The following code adds and positions two controls, a Label and a TextBox:

Label label1 = new Label()
{
    Text = "&First Name",
    Location = new Point(10, 10),
    TabIndex = 10
};

TextBox field1 = new TextBox()
{
    Location = new Point(label1.Location.X, label1.Bounds.Bottom + Padding.Top),
    TabIndex = 11
};

Controls.Add(label1);
Controls.Add(field1);
Dim label1 As New Label With {.Text = "&First Name",
                              .Location = New Point(10, 10),
                              .TabIndex = 10}

Dim field1 As New TextBox With {.Location = New Point(label1.Location.X,
                                                      label1.Bounds.Bottom + Padding.Top),
                                .TabIndex = 11}

Controls.Add(label1)
Controls.Add(field1)

See also

  • Set the Text Displayed by a Windows Forms Control
  • Add an access key shortcut to a control
  • System.Windows.Forms.Label
  • System.Windows.Forms.TextBox
  • System.Windows.Forms.Button

Phản hồi

Gửi và xem ý kiến phản hồi dành cho


Tài nguyên bổ sung

Tài nguyên bổ sung

Trong bài viết này

Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Create a Windows Forms app in Visual Studio with C#

  • Article
  • 09/01/2022
  • 6 minutes to read

In this article

Applies to:

Code in trong windows form
Visual Studio
Code in trong windows form
Visual Studio for Mac
Code in trong windows form
Visual Studio Code

In this short introduction to the Visual Studio integrated development environment (IDE), you'll create a simple C# application that has a Windows-based user interface (UI).

Create a project

First, you'll create a C# application project. The project type comes with all the template files you'll need, before you've even added anything.

  1. Open Visual Studio.

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

    Code in trong windows form

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

    (If you prefer, you can refine your search to quickly get to the template you want. For example, enter or type Windows Forms App in the search box. Next, choose C# from the Language list, and then choose Windows from the Platform list.)

    Code in trong windows form

    Note

    If you do not see the Windows Forms App (.NET Framework) template, you can install it from the Create a new project window. In the Not finding what you're looking for? message, choose the Install more tools and features link.

    Code in trong windows form

    Next, in the Visual Studio Installer, choose the .NET desktop development workload.

    Code in trong windows form

    After that, choose the Modify button in the Visual Studio Installer. You might be prompted to save your work; if so, do so. Next, choose Continue to install the workload. Then, return to step 2 in this "Create a project" procedure.

  4. In the Configure your new project window, type or enter HelloWorld in the Project name box. Then, choose Create.

    Code in trong windows form

    Visual Studio opens your new project.

  1. Open Visual Studio.

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

    Code in trong windows form

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

    (If you prefer, you can refine your search to quickly get to the template you want. For example, enter or type Windows Forms App in the search box. Next, select C# from the Language list, and then select Windows from the Platform list.)

    Code in trong windows form

    Note

    If you do not see the Windows Forms App (.NET Framework) template, you can install it from the Create a new project window. In the Not finding what you're looking for? message, select the Install more tools and features link.

    Code in trong windows form

    Next, in the Visual Studio Installer, select the .NET desktop development workload.

    Code in trong windows form

    After that, select the Modify button in the Visual Studio Installer. You might be prompted to save your work; if so, do so. Next, select Continue to install the workload. Then, return to step 2 in this "Create a project" procedure.

  4. In the Configure your new project window, type or enter HelloWorld in the Project name box. Then, select Create.

    Code in trong windows form

    Visual Studio opens your new project.

Create the application

After you select your C# project template and name your file, Visual Studio opens a form for you. A form is a Windows user interface. We'll create a "Hello World" application by adding controls to the form, and then we'll run the app.

Add a button to the form

  1. Select Toolbox to open the Toolbox fly-out window.

    Code in trong windows form

    (If you don't see the Toolbox fly-out option, you can open it from the menu bar. To do so, View > Toolbox. Or, press Ctrl+Alt+X.)

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

    Code in trong windows form

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

    Code in trong windows form

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

    Code in trong windows form

    (If you don't see the Properties window, you can open it from the menu bar. To do so, select View > Properties Window. Or, press F4.)

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

    Code in trong windows form

    Note

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

Add a label to the form

Now that we've added a button control to create an action, let's add a label control to send text to.

  1. Select the Label control from the Toolbox window, and 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, and then press 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:

    Code in trong windows form

Run the application

  1. Select the Start button to run the application.

    Code in trong windows form

    Several things will happen. In the Visual Studio IDE, the Diagnostics Tools window will open, and an Output window will open, too. But outside of the IDE, a Form1 dialog box appears. It will include 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!.

    Code in trong windows form

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

Create the application

After you select your C# project template and name your file, Visual Studio opens a form for you. A form is a Windows user interface. We'll create a "Hello World" application by adding controls to the form, and then we'll run the app.

Add a button to the form

  1. Choose Toolbox to open the Toolbox fly-out window.

    Code in trong windows form

    (If you don't see the Toolbox fly-out option, you can open it from the menu bar. To do so, View > Toolbox. Or, press Ctrl+Alt+X.)

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

    Code in trong windows form

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

    Code in trong windows form

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

    Code in trong windows form

    (If you don't see the Properties window, you can open it from the menu bar. To do so, choose View > Properties Window. Or, press F4.)

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

    Code in trong windows form

    Note

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

Add a label to the form

Now that we've added a button control to create an action, let's add a label control to send text to.

  1. Select the Label control from the Toolbox window, and 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, and then press 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:

    Code in trong windows form

Run the application

  1. Choose the Start button to run the application.

    Code in trong windows form

    Several things will happen. In the Visual Studio IDE, the Diagnostics Tools window will open, and an Output window will open, too. But outside of the IDE, a Form1 dialog box appears. It will include 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!.

    Code in trong windows form

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

Next steps

To learn more, continue with the following tutorial:

See also

  • More C# tutorials
  • Visual Basic tutorials
  • C++ tutorials

Feedback

Submit and view feedback for


Additional resources

Additional resources

In this article