如何验证在C#中的组合框的值 [英] How to validate combo box values in c#

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

问题描述

我在C#的形式6组合框,所有来自同一列的数据库填补。即制作学生成绩组合框填写主题名称。我现在想的是如何从第二个组合框中删除使用者名称如果对象是在previous组合已选定box.and我使用组合框属性从数据库填充每个组合框。

I have 6 combo boxes in c# form that all fill from database from same column. i-e for making student result combo boxes fill subject names. now I want is how to delete Subject name from 2nd combo box if the Subject is already selected in the previous combo box.and i use combo box property to fill each combo box from database.

推荐答案

Firsty您可以创建一个名单,其中,组合框> 并为每个组合框添加项目,并以这种方式 SelectedValueChanged 事件处理程序:

Firsty you can create a List<ComboBox> and for each ComboBox add the items and the SelectedValueChanged event handler in this way:

List<ComboBox> comboBoxes = new List<ComboBox>();
//here add your comboboxes to the list using the Add() method

foreach(ComboBox cb in comboboxes){
   cb.SelectedValueChanged += new EventHandler(comboBox_SelectedValueChanged);
   //add items to the ComboBox
}

然后当 SelectedValueChanged 事件被触发使用的SelectedItem 财产和其他组合框删除所选的项目是 Items.Remove()方法:

Then when the SelectedValueChanged event is fired delete the selected item it from the other ComboBoxes using SelectedItem property and Items.Remove() method:

private void comboBox_SelectedValueChanged(object sender, EventArgs e){
    //Fill the comboboxes
    ComboBox comboBox = (ComboBox)sender;
    if(comboBox.SelectedItem != null){
      foreach(ComboBox cb in comboboxes)
        cb.Items.Remove(comboBox.SelectedItem);
    }
}

PS:这没有工作,以及如果用户不是一次选择从ComboBox的元素更多,因为它会从另一个组合框删除它们。所以,这只是一个例子,让你对如何开发它的一些建议。综上所述,你必须进行改进。

PS: This didn't work as well if the user select more than one time the elements from the ComboBox because it will delete them from the other ComboBoxes. So this is just an example which gives you some advices on how to develop it. To sum up, you have to improve it.

编辑: 因为你可以有一些问题要解决这个问题,我提高了它你:

Because you could have some problems to solve this issue, I improved it for you:

private void comboBox_SelectedValueChanged(object sender, EventArgs e){
   foreach(ComboBox cb in comboBoxes){
      //Here listItems is a list of elements you added firstly to the comboboxes
      if(cb.Items.Count < listItems.Count)
         foreach(object item in listItems){ 
             if(!cb.Items.Contains(item))
               cb.Items.Add(item);
         }
      }
   }

   //Remove the selected items in all the comboboxes
   ComboBox comboBox = (ComboBox)sender;
   if(comboBox.SelectedItem != null){
      for(int i = 0; i < comboBoxes.Count; i++){
         for(int j = 0; j < comboBoxes.Count; j++){
            if(i != j && comboBoxes[j].SelectedItem != null && comboBoxes[j].Contains(comboBoxes[j].SelectedItem))
              comboBoxes[i].Items.Remove(comboBoxes[j].SelectedItem);
         }
      }
   }
}

如果你有太多的组合框这可能会很慢,所以我想,使其更快地。但在这种情况下,你有6个组合框控件它应该很好地工作。

This could be very slow if you have too many ComboBoxes, so I tried to make it faster as possible. But in this case where you have 6 ComboBox controls it should work very well.

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

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