第二章例题:验证处理

对教材2.6.2、2.6.3、2.6.4部分进行的例题演示,代码如下:
 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        }

 

 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        }

完整例题代码下载:Validating.rar
posted @ 2007-03-18 20:21  dn  阅读(264)  评论(0)    收藏  举报