This implementation gives exactly the required order of the items whenever I change the selection from any comboBox and if there is duplication, it will replace the text of the old comboBox to -select-. and this is what I want. This way the user will know that he changed the selection of this comboBox, and later he can select any item from that comboBox to get the item selection order.
But one important thing is remaining, I don't need -select- to be shown in the Text field because this reflects the selectedItem..
Infact, if you change the textBox.Text implementation to: textBox1.Text = comboBox1.SelectedItem.ToString() + "," + comboBox2.SelectedItem.ToString() + "," + comboBox3.SelectedItem.ToString() + "," + comboBox4.SelectedItem.ToString(); then the textBox will contain duplication of items, and later if I need to use the selected item from each comboBox as a parameter to pass it to a function it will give incorrect result.
You can compare the output of the text with the comboBoxes texts if you tried to run it.
As a solution, I'm trying to assign the oldComboBox.selectedIndex to -1, but it gives exception handling error. And if I left my code this way, then this will give some other logical error in the future
I've even tried try and catch if the selected index is -1, but didn't help. So what to do, please help me to solve this. It took me hours and days to solve, but couldn't :-(
private void UpdateComboBox(ComboBox comboBox) { // Remember the item currently selected. var selectedItem = comboBox.SelectedItem; foreach (var combo in comboBoxes) { if (combo.SelectedItem == selectedItem) { combo.Text = "-Select-"; defaulItem = selectedItem.ToString(); comboBox.Text = selectedItem.ToString(); comboBox.SelectedItem = selectedItem;
}
else { comboBox.SelectedItem = selectedItem;
} } }
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { UpdateComboBox(comboBox1); } private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) { UpdateComboBox(comboBox2); }
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) { UpdateComboBox(comboBox3); } private void comboBox4_SelectedIndexChanged(object sender, EventArgs e) { UpdateComboBox(comboBox4); }