How to validate combo box values in c# [closed]

匆匆过客 提交于 2019-12-11 13:47:26

问题


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.

回答1:


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
}

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: 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.


EDIT: 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);
         }
      }
   }
}

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.




回答2:


You need to have a list of items which is selected, then after event selectedValueChanged() you must remove old combobox value and fill each combobox again.

    List<object> coll = new List<Object>();
    List<Object> dbItems = new List<Object>();
    private void FillComboBox(object currSelected, ComboBox control)
    {
        control.Items.Clear();
        foreach (object c in dbItems)
        {
            if ((coll.Contains(c) == false) || (c == currSelected))
            {
                control.Items.Add(c);
            }
        }
        control.SelectedItem = currSelected;
    }
    private void SelectedItemChanged()
    {
        foreach (object c in coll) {
            if ((comboBox1.SelectedItem != null && c.Equals(comboBox1.SelectedItem)) ||
                (comboBox2.SelectedItem != null && c.Equals(comboBox2.SelectedItem)) ||
                (comboBox3.SelectedItem != null && c.Equals(comboBox3.SelectedItem)))
            {
            }else{
                coll.Remove(c);
            }
        }
    }

    private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
    {
        // That code must be in every combobox_selectedValueChanged
        SelectedItemChanged();
        FillComboBox(coll, comboBox1);
        FillComboBox(coll, comboBox2);
        FillComboBox(coll, comboBox3);
    }

PS: Sorry, but I don`t remember how to get previously selected item in combobox, so I wrote additional method when any combobox selected item have been changed.



来源:https://stackoverflow.com/questions/13258986/combo-box-selected-value-validating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!