Skip to content

添加控件

1.拖动添加

打开窗口文件的设计界面,可以看到右侧有一个工具箱

其中包含许多控件,鼠标按住一个控件拖动到窗口中,即可在窗口中添加该控件

image-20250106172734345

可以看到窗口代码Form.Designer.cs中多了一些内容

c#
namespace WinformTest
{
    partial class Form1
    {
        
        // .........
        #region Windows 窗体设计器生成的代码
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(431, 97);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
        }

        #endregion
        private System.Windows.Forms.Button button1;
    }
}

从我们多年的编程经验中可以看出来,多出来的代码主要是创建了一个按钮,给按钮设置了位置,样式,按钮文字等等

若想设置控件的其他属性,可以右击该控件,选择属性,在新出来的属性窗口修改

image-20250106203003475

TIP

若没有工具箱,可点击编辑器上方菜单=》视图=》工具箱

image-20250106173012057

2.观察代码

在上述代码中,我们可以看到Program.cs是程序的入口文件其中的 Application.Run(new Form1());代表程序运行,并实例化一个Form1窗体

c#
using System;
using System.Windows.Forms;

namespace WinformTest
{
    internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Form1.cs是窗体的逻辑代码,继承了Form类,目前只在构造函数中执行了InitializeComponent();函数,用来初始化窗体和控件

InitializeComponent();函数来自Form1.Desiginer.cs文件

c#
using System.Windows.Forms;

namespace WinformTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

Form1.Desiginer.cs中书写窗体和控件的的布局及样式代码,其中代码主要在InitializeComponent()函数中写

c#
namespace WinformTest
{
    partial class Form1
    {
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(324, 354);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        private System.Windows.Forms.Button button1;
    }
}

3.手动添加

可以直接在Form1.cs中书写如下代码

c#
using System.Windows.Forms;
using System.Drawing;

namespace WinformTest
{
    public partial class Form1 : Form
    {
        private Button button = new Button();
        public Form1()
        {
            InitializeComponent();

            button.Text = "确定";						// 按钮文字
            button.Location = new Point(100, 200);	 // 按钮位置,相对于窗口左上角
            button.Size=new Size(80, 35);			 // 按钮大小,单位px
            this.Controls.Add(button);				 // 添加
        }
    }
}

3.1 窗口坐标

winform中的坐标系是从窗口的左上角开始(不包含头部),向右为x轴,向下为y轴

image-20250106205743427

3.2 控件中添加控件

我们现在已经知道,给Form1添加控价时,需要使用Form1类中的ControlsAdd方法

c#
this.Controls.Add(this.button1);
this.Controls.Add(this.button2);

同样,当需要给控件中添加控件时,需要调用该控件的Controls属性中的Add方法

比如,如下给Form1窗口添加了一个Panel控件

c#
private Panel panel1;

private void InitializeComponent(){
    this.panel1= new System.Windows.Forms.Panel();
    this.Controls.Add(this.panel1);
}

然后再给这个Panel添加个button

c#
private Panel panel1;
private Button button1;

private void InitializeComponent(){
    this.panel1= new System.Windows.Forms.Panel();
    this.button1 = new System.Windows.Forms.Button();

    this.Controls.Add(this.panel1);
    this.panel1.Controls.Add(this.button1);
}

4.修改属性

右键控件打开属性栏,点击扳手图标即可看到控件的属性,旁边的闪电图标是事件栏

image-20250108151535126

MIT Licensed