2012-06-19 125 views
2

我有近20个文本框,并检查我必须使用errorprovider每次调用20次验证事件。除此之外,还有其他有效的方法吗?C#验证多个文本框?

+0

我认为你可以得到一个回答你的问题在这里:http://stackoverflow.com/questions/1959018/validating-several-textboxes-on-ac-sharp-windows-form在同一时间 –

+0

谢谢,但我想检查运行时验证,链接在提交时提供有关验证的信息。 –

回答

1
this.textBox1.Validating += new System.ComponentModel.CancelEventHandler(this.textBox_Validating); 
this.textBox2.Validating += new System.ComponentModel.CancelEventHandler(this.textBox_Validating); 
this.textBox3.Validating += new System.ComponentModel.CancelEventHandler(this.textBox_Validating); 

// And so on for the 20 boxes. 
private void textBox_Validating(object sender, CancelEventArgs e) 
{ 
    TextBox textbox = (TextBox)sender; 

    // Do whatever yo need to do with textbox here. 
} 
0

为它们中的每一个创建验证器(RequiredFieldValidator或whatnot),然后将它们全部分配给相同的ValidationGroup。您可以一次执行该组中所有控件的验证。

http://msdn.microsoft.com/en-us/library/ms227424.aspx

+0

你可能会更具体一些,因为链接重定向到asp.net,我正在处理桌面应用程序。 –

+0

对不起,我假设网络! – LesterDove