创建一个观察者,它将订阅所有 textBox.TextChanged 事件。
public class IputValidator
{
public event Action ValidationDone;
private List<TextBox> boxes = new List<TextBox>();
public void RegisterTextBox(TextBox tb)
{
tb.TextChanged += (s,e) => Validate;
boxes.Add(tb);
}
public void Validate()
{
foreach(var t in boxes)
{
if(string.IsNullOrEmpty(t.Text)) return;
}
//all data inputed. fire validationDone event.
}
}
它将等待输入所有必要的数据。然后,它将启用确定按钮。
使用
public partial class YourForm : Form
{
private InputValidator _validator;
public YourForm()
{
InitializeComponents(); //I don't remember the right name in WinForms
_validator = new InputValidator();
_validator.RegisterTextBox(_textBox1);
_validator.ValidatonDone += () => { _okButton.Enable = true;}
}
}