在C# WinForms开发中,当内置的标准控件无法完全匹配业务场景的功能需求时,创建自定义控件是扩展界面交互能力的有效方式,其中用户控件是最常用的自定义控件实现形式,适合组合多个现有控件实现复合功能。

WinForms自定义控件的两种常见形式
WinForms中的自定义控件主要分为两类,开发者可以根据需求选择合适的实现方式:
- 用户控件:继承自
UserControl类,适合将多个现有控件组合成一个新的复合控件,开发成本低,适合快速实现业务相关的复合功能。 - 自定义控件:直接继承自
Control类或者某个现有控件类,需要自己处理控件的绘制、交互逻辑,适合实现完全个性化的控件外观和功能。
创建WinForms用户控件的完整步骤
1. 新建用户控件项目项
打开已有的WinForms项目,在解决方案资源管理器中右键点击项目,选择添加,然后选择用户控件(Windows 窗体),设置好控件的名称,比如MyCustomControl,点击添加即可生成用户控件的设计文件和代码文件。
2. 设计用户控件界面
打开用户控件的设计界面,从工具箱中拖拽需要的现有控件到设计面板,比如拖拽一个Label和一个TextBox,调整好布局和大小,这两个控件会作为用户控件的组成部分。
3. 定义用户控件的属性和方法
切换到用户控件的代码文件,在类中定义公开的属性和方法,方便外部调用和配置控件。比如我们可以定义一个LabelText属性来设置内部Label的显示文本,定义一个GetInputValue方法获取TextBox的输入内容。
using System;
using System.Windows.Forms;
namespace WinFormsCustomControlDemo
{
public partial class MyCustomControl : UserControl
{
public MyCustomControl()
{
InitializeComponent();
}
// 定义属性,设置内部Label的显示文本
public string LabelText
{
get { return label1.Text; }
set { label1.Text = value; }
}
// 定义属性,获取或设置内部TextBox的输入内容
public string InputValue
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
// 定义方法,清空输入框内容
public void ClearInput()
{
textBox1.Text = string.Empty;
}
}
}
4. 添加自定义事件
如果需要在用户控件内部发生特定操作时通知外部容器,可以定义自定义事件。比如当内部TextBox的内容发生变化时,触发一个自定义的事件。
using System;
using System.Windows.Forms;
namespace WinFormsCustomControlDemo
{
public partial class MyCustomControl : UserControl
{
// 定义自定义事件,当输入框内容变化时触发
public event EventHandler InputValueChanged;
public MyCustomControl()
{
InitializeComponent();
// 订阅内部TextBox的文本变化事件
textBox1.TextChanged += TextBox1_TextChanged;
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
// 触发自定义事件,传递当前实例作为 sender
InputValueChanged?.Invoke(this, EventArgs.Empty);
}
public string LabelText
{
get { return label1.Text; }
set { label1.Text = value; }
}
public string InputValue
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
public void ClearInput()
{
textBox1.Text = string.Empty;
}
}
}
使用自定义用户控件
用户控件创建完成后,需要先生成项目,之后用户控件会自动出现在工具箱的当前项目分类下,直接拖拽到窗体的设计界面即可使用,也可以手动在代码中实例化并添加到窗体。
设计器中使用
生成项目后,打开目标窗体的设计界面,在工具箱中找到刚才创建的MyCustomControl,拖拽到窗体上,然后在属性面板中可以设置LabelText等自定义属性,也可以订阅InputValueChanged事件。
代码中使用
也可以手动编写代码实例化用户控件并添加到窗体,示例代码如下:
using System;
using System.Windows.Forms;
namespace WinFormsCustomControlDemo
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
// 实例化自定义用户控件
MyCustomControl customControl = new MyCustomControl();
// 设置控件位置和大小
customControl.Location = new System.Drawing.Point(50, 50);
customControl.Size = new System.Drawing.Size(200, 50);
// 设置自定义属性
customControl.LabelText = "用户名:";
// 订阅自定义事件
customControl.InputValueChanged += CustomControl_InputValueChanged;
// 将控件添加到窗体
this.Controls.Add(customControl);
}
private void CustomControl_InputValueChanged(object sender, EventArgs e)
{
MyCustomControl control = sender as MyCustomControl;
if (control != null)
{
MessageBox.Show($"输入内容已变化,当前值为:{control.InputValue}");
}
}
}
}
注意事项
- 用户控件的属性如果需要支持设计器修改,尽量使用简单的可序列化类型,复杂的类型可能无法在设计器中正常显示和编辑。
- 如果自定义控件需要重绘外观,需要重写
OnPaint方法,使用Graphics对象绘制控件内容。 - 用户控件内部的子控件默认是私有访问级别,如果需要外部直接访问,需要修改访问修饰符为
public或者提供对应的公开属性封装。
C#_WinForms自定义控件用户控件控件开发修改时间:2026-07-06 12:12:31