第二章例题:验证处理

对教材2.6.2、2.6.3、2.6.4部分进行的例题演示,代码如下:
 1 None.gif // 对控件验证事件的处理
 2 None.gif          private   void  minValueTextBox_Validating( object  sender, System.ComponentModel.CancelEventArgs e)
 3 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 4InBlock.gif            if(Convert.ToInt32(minValueTextBox.Text) >= Convert.ToInt32(maxValueTextBox.Text))
 5ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 6InBlock.gif                e.Cancel = true;
 7InBlock.gif                MessageBox.Show("You must enter a minimum value that is less than the Maximum value");
 8ExpandedSubBlockEnd.gif            }

 9ExpandedBlockEnd.gif        }

10 None.gif
11 None.gif // 利用ErrorProvider控件完成验证
12 None.gif          private   void  textBox1_Validating( object  sender, System.ComponentModel.CancelEventArgs e)
13 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
14InBlock.gif            try
15ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
16InBlock.gif                int x= Int32.Parse(textBox1.Text);
17InBlock.gif                errorProvider1.SetError(textBox1,"");
18ExpandedSubBlockEnd.gif            }

19InBlock.gif            catch
20ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
21InBlock.gif                errorProvider1.SetError(textBox1,"Not a integer value.");
22ExpandedSubBlockEnd.gif            }

23ExpandedBlockEnd.gif        }

 

 1 None.gif // 窗体级别的验证
 2 None.gif          private   void  btnValidate_Click( object  sender, System.EventArgs e)
 3 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 4InBlock.gif            foreach(System.Windows.Forms.Control aControl in this.Controls)
 5ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 6InBlock.gif                if(aControl is System.Windows.Forms.TextBox && aControl.Text=="")
 7ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 8InBlock.gif                    aControl.Focus();
 9InBlock.gif                    return;
10ExpandedSubBlockEnd.gif                }

11ExpandedSubBlockEnd.gif            }

12InBlock.gif            MessageBox.Show("通过了验证!");
13InBlock.gif
14ExpandedBlockEnd.gif        }

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

转载于:https://www.cnblogs.com/hhdn/archive/2007/03/18/679107.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值