使用 Visual C# 以编程方式在运行时将控件添加到 Windows 窗体

本文介绍如何使用 Visual C# 以编程方式在 Windows 窗体上添加和配置控件。

原始产品版本: Visual C#
原始 KB 数: 319266

总结

本分步文章介绍如何以编程方式在 Windows 窗体上添加和配置一些常用控件。 已从示例代码中省略事件处理。

Microsoft .NET Framework 软件开发工具包(SDK)提供了许多视觉控件,可用于生成Windows 窗体应用程序。 可以在 Visual Studio .NET 或 Visual Studio 的设计时添加和配置控件。 可以在运行时以编程方式添加和配置控件。

要求

本文假定你熟悉以下主题:

  • Visual C# 语法
  • Visual Studio .NET 环境、Visual Studio 环境
  • 常见 Visual C# 控件的用途

创建 Windows 窗体应用程序

  1. 启动 Visual Studio .NET 或 Visual Studio,并创建新的名为 WinControls 的 Visual C# Windows 应用程序项目。 Form1 默认添加到项目中。

  2. 双击 Form1 以创建和查看 Form1_Load 事件过程。

  3. 将专用实例变量添加到 Form1 类,以便使用常见的 Windows 控件。 该 Form1 类按如下所示开始:

    public class Form1 : System.Windows.Forms.Form
    {
         //Controls.
         private TextBox txtBox = new TextBox();
         private Button btnAdd = new Button();
         private ListBox lstBox = new ListBox();
         private CheckBox chkBox = new CheckBox();
         private Label lblCount = new Label();
        //Other code.
    }
    

    注意

    应在 Visual Studio 中更改代码。 创建Windows 窗体项目时,默认情况下,Visual C# 会将一个窗体添加到项目中。 此窗体名为 Form1。 表示窗体的两个文件命名 为Form1.csForm1.designer.cs。 在 Form1.cs编写代码。 Designer.cs文件是Windows 窗体设计器编写代码,用于实现通过添加控件执行的所有操作。 有关 Visual C# 中的Windows 窗体设计器的详细信息,请参阅“创建项目”(Visual C#)。

自定义窗体和控件属性

  1. Form1_Load找到事件过程,并将以下代码添加到过程以自定义窗体控件的外观:

     //Set up the form.
     this.MaximizeBox = false;
     this.MinimizeBox = false;
     this.BackColor = Color.White;
     this.ForeColor = Color.Black;
     this.Size = new System.Drawing.Size(155, 265);
     this.Text = "Run-time Controls";
     this.FormBorderStyle = FormBorderStyle.FixedDialog;
     this.StartPosition = FormStartPosition.CenterScreen;
    
  2. 将以下代码添加到 Form1_Load 事件过程以自定义 Button 控件的外观:

     //Format controls. Note: Controls inherit color from parent form.
     this.btnAdd.BackColor = Color.Gray;
     this.btnAdd.Text = "Add";
     this.btnAdd.Location = new System.Drawing.Point(90, 25);
     this.btnAdd.Size = new System.Drawing.Size(50, 25);
    
  3. 添加以下代码以自定义 TextBox 控件 Form1_Load的外观:

     this.txtBox.Text = "Text";
     this.txtBox.Location = new System.Drawing.Point(10, 25);
     this.txtBox.Size = new System.Drawing.Size(70, 20);
    
  4. 添加以下代码以自定义 ListBox 控件的外观 Form1_Load

     this.lstBox.Items.Add("One");
     this.lstBox.Items.Add("Two");
     this.lstBox.Items.Add("Three");
     this.lstBox.Items.Add("Four");
     this.lstBox.Sorted = true;
     this.lstBox.Location = new System.Drawing.Point(10, 55);
     this.lstBox.Size = new System.Drawing.Size(130, 95);
    
  5. 添加以下代码以自定义 CheckBox 控件 Form1_Load的外观:

     this.chkBox.Text = "Disable";
     this.chkBox.Location = new System.Drawing.Point(15, 190);
     this.chkBox.Size = new System.Drawing.Size(110, 30);
    
  6. 添加以下代码以自定义 Label 控件 Form1_Load的外观:

     this.lblCount.Text = lstBox.Items.Count.ToString() + " items";
     this.lblCount.Location = new System.Drawing.Point(55, 160);
     this.lblCount.Size = new System.Drawing.Size(65, 15);
    

向窗体添加控件

  1. 添加以下代码,将每个对象添加到 Controls 窗体的数组的末尾 Form1_Load

     //Add controls to the form.
     this.Controls.Add(btnAdd);
     this.Controls.Add(txtBox);
     this.Controls.Add(lstBox);
     this.Controls.Add(chkBox);
     this.Controls.Add(lblCount);
    
  2. 保存该项目。

验证它是否正常工作

若要验证示例是否正常工作,请选择“调试”菜单上的开始”。

注意

  • 虽然窗体和控件显示,但它们当前不执行任何操作,因为你尚未编写任何事件处理程序。
  • 请记住,这些控件的位置是静态的。 若要在拉伸窗体时使其更可靠,请使点相对于窗体位置动态。 如果控件是静态的,则拉伸窗体可能会干扰窗体上其他控件的显示。

参考

有关以编程方式使用控件的详细信息,请参阅 Visual Studio .NET Online 帮助文档的 Visual C# 部分中的 Windows 应用程序 主题,或 Visual Studio Online 帮助文档。