问题

回答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