如何验证多年的多个组合框? [英] how to validate multiple comboboxes containing years?

查看:63
本文介绍了如何验证多年的多个组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hiii,

我有4个组合框,每个都填充年份。我想验证comboBox1中的值必须大于其他3,然后comboBox2中的值必须大于下两个组合框和所以on.Plz帮助我....

Hiii,
I have 4 comboBoxes each filled with years.I want to validate such that value in comboBox1 must be greater than other 3,then value in comboBox2 must be greater than next two comboBoxes and so on.Plz help me out....

推荐答案

首先,我个人不喜欢对每个控件的验证事件的控件进行交叉验证 - 它''在你甚至可以退出表格之前你必须输入有效的数据有点太容易陷入正确的混乱 - 小心这一点(我的个人偏好是对特定事件进行交叉验证,比如按下按钮 - Ok 或提交)



说过......这样的事情应该有用......



通用验证函数

Firstly I personally dislike cross-validation between controls on the validating events of each of the controls - it''s a little bit too easy to get into a right mess where you have to enter valid data before you can even exit the form - be careful of this (My personal preference is cross-validation on a specific event on say a button push - "Ok" or "Submit")

Having said that ... something like this should work...

A generic validation function
private bool InvalidYear(object sender)
       {
           // cast the sender object to a ComboBox so that we can access it's properties
           ComboBox cb = (ComboBox)sender;

           /* Find out the number of this combo box  - Note there are loads of ways of doing this but
            * I've assumed they're called comboBox1, comboBox2, comboBox3, comboBox4
            * and I'm just removing the text "combBox" from the name
            * I'm also not assuming that their tabindexes are consecutive
            * and remember the user can select them in any order
           */
           int cbNum = int.Parse(cb.Name.Replace("comboBox", ""));

           // Locate the comboBoxes before and after the one we're validating
           // Taking care at either end of the list of comboBoxes...
           ComboBox prev = null, post = null;
           if (cbNum > 1)
               prev = (ComboBox)this.Controls.Find("comboBox" + (cbNum - 1).ToString(), true)[0];

           if (cbNum < 4)
               post = (ComboBox)this.Controls.Find("comboBox" + (cbNum + 1).ToString(), true)[0];

           bool errorFound = false;

           // don't validate against a checkbox the user hasn't picked yet!
           if (prev != null && prev.Text != "")
           {
               if (cb.Text.CompareTo(prev.Text) <= 0)  // this one must be greater than previous
                   errorFound = true;
           }
           if (post != null && post.Text != "")
           {
               if (cb.Text.CompareTo(post.Text) >= 0) // this one must be less than the one after
                   errorFound = true;
           }


           if (errorFound) MessageBox.Show("Invalid year");

           return errorFound;
       }





然后你可以从这样的每个组合框中调用它...





And then you can call this from each of the comboBoxes like this ...

private void comboBox1_Validating(object sender, CancelEventArgs e)
 {
     if (InvalidYear(sender))
         e.Cancel = true;
 }


这篇关于如何验证多年的多个组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆