namespace AppFirstWin { public partial classForm1 : Form { private Button btnClick; // 声明按钮控件 private TextBox txtInput; // 声明文本输入框 private Label lblResult; // 声明标签控件 publicForm1() { InitializeComponent(); InitializeControls(); //初始化控件,这块一样不用这么搞 SetupEventHandlers(); // 设置事件处理 } privatevoidInitializeControls() { // 设置窗体基本属性 this.Text = "我的第一个控件程序"; this.Size = new Size(400, 300); this.StartPosition = FormStartPosition.CenterScreen; // 创建并配置按钮 btnClick = new Button { Text = "点击处理", Location = new Point(50, 50), Size = new Size(100, 30), BackColor = Color.LightBlue, Font = new Font("微软雅黑", 10) }; // 创建并配置文本框 txtInput = new TextBox { Location = new Point(50, 100), Size = new Size(200, 25), PlaceholderText = "请输入一些文字..." }; // 创建并配置标签 lblResult = new Label { Text = "结果将显示在这里", Location = new Point(50, 150), Size = new Size(300, 50), ForeColor = Color.DarkGreen, Font = new Font("微软雅黑", 12) }; // 将所有控件添加到窗体 this.Controls.AddRange(new Control[] { btnClick, txtInput, lblResult }); } privatevoidSetupEventHandlers() { // 按钮点击事件 btnClick.Click += (sender, e) => { string userInput = txtInput.Text; if (string.IsNullOrWhiteSpace(userInput)) { lblResult.Text = "请先输入一些内容!"; lblResult.ForeColor = Color.Red; } else { lblResult.Text = $"你输入了:{userInput}"; lblResult.ForeColor = Color.DarkGreen; } }; // 文本框内容改变事件 txtInput.TextChanged += (sender, e) => { if (txtInput.Text.Length > 50) { MessageBox.Show("输入内容不能超过50个字符!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }; } } }
🎯 代码解析:每行都有讲究
引用命名空间
System.Drawing - 提供颜色、字体等UI相关类
System.Windows.Forms - 包含所有控件类
控件初始化的黄金法则
btnClick = new Button { Text = "点击处理", // 显示文本 Location = new Point(50, 50), // 位置坐标 Size = new Size(100, 30), // 控件大小 BackColor = Color.LightBlue // 背景颜色 };
⚠️ 常见坑点提醒:
忘记调用this.Controls.Add() - 控件创建了但不显示
事件处理程序内存泄漏 - 使用Lambda表达式要注意作用域
控件重叠 - Location设置不当导致控件互相遮挡
千万不要以为控件布局都要这么手写,实际业务上大多是根组态一样也是拖拉拽实现的。
🛠️ 控件属性完全掌握
📊 核心属性速查表
属性名
作用
实用技巧
Text
显示文本
支持换行符\n
Location
位置坐标
使用Point(x, y)设置
Size
控件大小
Size(width, height)
Enabled
是否可用
false时控件变灰
Visible
是否可见
动态显示/隐藏控件
Anchor
锚定方式
响应式布局的关键
🎨 美化控件的高级技巧
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace AppFirstWin { public partial classForm2 : Form { publicForm2() { InitializeComponent(); } // 设置渐变背景(需要重写Paint事件) protected override voidOnPaint(PaintEventArgs e) { using (var brush = new LinearGradientBrush( this.ClientRectangle, Color.LightBlue, Color.DarkBlue, 45f)) { e.Graphics.FillRectangle(brush, this.ClientRectangle); } base.OnPaint(e); } } }
publicclassControlFactory { publicstatic Button CreateStandardButton(string text, Point location) { returnnew Button { Text = text, Location = location, Size = new Size(100, 30), BackColor = Color.LightBlue, FlatStyle = FlatStyle.Flat, Font = new Font("微软雅黑", 9) }; } publicstatic TextBox CreateValidatedTextBox(Point location, int maxLength = 50) { var textBox = new TextBox { Location = location, Size = new Size(200, 25), MaxLength = maxLength }; // 添加验证逻辑 textBox.KeyPress += (sender, e) => { if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) { e.Handled = true; // 只允许数字输入 } }; return textBox; } }
使用工厂创建控件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace AppFirstWin { public partial classForm3 : Form { publicForm3() { InitializeComponent(); // 使用工厂创建控件 var saveButton = ControlFactory.CreateStandardButton("保存", new Point(100, 50)); var numberInput = ControlFactory.CreateValidatedTextBox(new Point(100, 100)); this.Controls.AddRange(new Control[] { saveButton, numberInput }); } } }