【重构】C#用GroupBox快速清除多个TextBox控件中的值

问题

在重构的时候经常会遇到清除多个TextBox控件的值,好多个控件的话,每次都得写好长的代码。都是重复的动作很繁琐,后来想了想,挺能用GroupBox这个控件,难道就只是为了美观吗,其实不然,除了美观,还有别的作用。

解决

1、在窗体上放一个GroupBox,将多个TextBox放在GroupBox控件上,这样TextBox会随着GroupBox的移动而移动
这里写图片描述
2、遍历GroupBox中的TextBox,代码实现

foreach (Control i in groupBox1.Controls)
{
       if (i is TextBox)
       {
            i.Text = "";
       }
}

总结

很多重复的动作,是可以简单化的。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
要使一个 WinForm 含有多个圆角的 GroupBoxTextBox、RichTextBox,可以通过以下步骤实现: 1. 创建一个新的类,继承自对应的控件类。例如,要创建一个圆角的 GroupBox,可以创建一个新的类,继承自 GroupBox。 ```csharp public class RoundGroupBox : GroupBox { // ... } ``` 2. 在新类的构造函数设置控件的 BorderStyle 属性为 None,并设置控件的大小、颜色等属性。 ```csharp public RoundGroupBox() { this.BackColor = Color.Transparent; this.ForeColor = Color.White; this.Font = new Font("微软雅黑", 12F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(134))); this.Size = new Size(200, 100); this.TabStop = false; this.FlatStyle = FlatStyle.Flat; this.BorderStyle = BorderStyle.None; } ``` 3. 重写控件的 OnPaint 方法,实现圆角效果。可以使用 Graphics 类的 DrawArc 和 FillPath 方法来绘制圆角。 ```csharp protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; using (GraphicsPath path = new GraphicsPath()) { path.AddArc(new Rectangle(0, 0, 20, 20), 180, 90); path.AddLine(20, 0, this.Width - 20, 0); path.AddArc(new Rectangle(this.Width - 20, 0, 20, 20), -90, 90); path.AddLine(this.Width, 20, this.Width, this.Height - 20); path.AddArc(new Rectangle(this.Width - 20, this.Height - 20, 20, 20), 0, 90); path.AddLine(this.Width - 20, this.Height, 20, this.Height); path.AddArc(new Rectangle(0, this.Height - 20, 20, 20), 90, 90); path.AddLine(0, this.Height - 20, 0, 20); path.CloseFigure(); this.Region = new Region(path); } using (Pen pen = new Pen(Color.FromArgb(255, 204, 204, 204), 1)) { e.Graphics.DrawPath(pen, path); } } ``` 4. 在 WinForm 使用新类替换原有的控件。 ```csharp RoundGroupBox roundGroupBox1 = new RoundGroupBox(); roundGroupBox1.Location = new Point(50, 50); roundGroupBox1.Text = "Round GroupBox"; this.Controls.Add(roundGroupBox1); ``` 对于 TextBox 和 RichTextBox 控件,也可以采用类似的方法实现圆角效果。在 OnPaint 方法使用 Graphics 类的 FillRectangle 方法绘制控件的背景,然后再使用 GraphicsPath 绘制圆角。 ```csharp protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; using (GraphicsPath path = new GraphicsPath()) { path.AddArc(new Rectangle(0, 0, 20, 20), 180, 90); path.AddLine(20, 0, this.Width - 20, 0); path.AddArc(new Rectangle(this.Width - 20, 0, 20, 20), -90, 90); path.AddLine(this.Width, 20, this.Width, this.Height - 20); path.AddArc(new Rectangle(this.Width - 20, this.Height - 20, 20, 20), 0, 90); path.AddLine(this.Width - 20, this.Height, 20, this.Height); path.AddArc(new Rectangle(0, this.Height - 20, 20, 20), 90, 90); path.AddLine(0, this.Height - 20, 0, 20); path.CloseFigure(); this.Region = new Region(path); } using (SolidBrush brush = new SolidBrush(Color.White)) { e.Graphics.FillRectangle(brush, new Rectangle(0, 0, this.Width, this.Height)); } using (Pen pen = new Pen(Color.FromArgb(255, 204, 204, 204), 1)) { e.Graphics.DrawPath(pen, path); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值