1
//
对控件验证事件的处理
2
private
void
minValueTextBox_Validating(
object
sender, System.ComponentModel.CancelEventArgs e)
3
{
4
if(Convert.ToInt32(minValueTextBox.Text) >= Convert.ToInt32(maxValueTextBox.Text))
5
{
6
e.Cancel = true;
7
MessageBox.Show("You must enter a minimum value that is less than the Maximum value");
8
}
9
}
10
11
//
利用ErrorProvider控件完成验证
12
private
void
textBox1_Validating(
object
sender, System.ComponentModel.CancelEventArgs e)
13
{
14
try
15
{
16
int x= Int32.Parse(textBox1.Text);
17
errorProvider1.SetError(textBox1,"");
18
}
19
catch
20
{
21
errorProvider1.SetError(textBox1,"Not a integer value.");
22
}
23
}

2

3



4

5



6

7

8

9

10

11

12

13



14

15



16

17

18

19

20



21

22

23

1
//
窗体级别的验证
2
private
void
btnValidate_Click(
object
sender, System.EventArgs e)
3
{
4
foreach(System.Windows.Forms.Control aControl in this.Controls)
5
{
6
if(aControl is System.Windows.Forms.TextBox && aControl.Text=="")
7
{
8
aControl.Focus();
9
return;
10
}
11
}
12
MessageBox.Show("通过了验证!");
13
14
}

2

3



4

5



6

7



8

9

10

11

12

13

14

完整例题代码下载: Validating.rar