实现效果:
1,创建一个WinFrom程序
2,添加一个类库
3,类库中添加一个组件类
4,从工具箱中向组件类中拖拽一个ErrorProvider组件
代码部分:
MyTextBox
using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; //添加Forms引用 using System.Windows.Forms; namespace ClassLibrary2 { public partial class MyTextBox : TextBox { public MyTextBox() { InitializeComponent(); } public MyTextBox(IContainer container) { container.Add(this); InitializeComponent(); } public int IsNull(string info) { if (this.Text.Trim().Length == 0) { this.Error.SetError(this, info); return 0; } else { this.Error.SetError(this, string.Empty); return 1; } } } }
Form1
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 控件 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int Result = this.myTextBox1.IsNull("不允许传送空值"); if (Result == 1) { this.myTextBox2.Text = this.myTextBox1.Text; } else { //否则执行的代码 } } } }
生成不了时,将dll文件拖拽过去即可。