Hello, Vulpes
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 :-(
public partial class Form1 : Form
{
string[] Items = { "Name", "Age", "ID", "Country", "Address"};
bool[] allowEdit = { false, false, false, false };
ComboBox[] comboBoxes;
bool ignoreChanges;
string selectedItem;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.comboBoxes = new[] { this.comboBox1, this.comboBox2, this.comboBox3, this.comboBox4 };
foreach (var comboBox in this.comboBoxes)
{
comboBox.Items.AddRange(this.Items);
}
this.comboBox1.SelectedItem = "Name";
this.comboBox2.SelectedItem = "Age";
this.comboBox3.SelectedItem = "ID";
this.comboBox4.SelectedItem = "Country";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = comboBox1.Text + " , " + comboBox2.Text + " , " + comboBox3.Text + " , " + comboBox4.Text;
}
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);
}
Description of the problem:When you run the application, all the four comboBoxes must populate/show 5 items in which the user can select any item from any comboBox... He doesn't need to go with a specific order of selection... However, there is a default selected item from each comboBox when the application runs..
Please follow the below example with comments: When you run the application, comboBox1, comboBox2, comboBox3 and comboBox4 must have all 5 items
Item1, item2, item3, item4, item5 also comboBox1 will have by default item1 as selected item, combxbox2 will have item2, comboBox3 will have item3 as selected item, and comboBox4 will have item4 as the selected item..
Now, if the user wants to change the selection ( I mean if he wants to select different items from some or all comboBoxes then he can do that, he just need to go to the one of the comboBoxes and change the selected item..
When this is occurred, if the item has been selected before by other comboBoxes, then the old comboBox.text must be set to -Select- and the selected index must be set to -1.. Means that from the old comboBox no item is currently selected and it will have 6 items available to the user if he wants to select one...
I just want to enable the user to display the order of the selected items from each comboBox in a text box each time he clicks on show the order button,,,but I don't need duplication of the same item,, no item can be selected and displayed twice...
Examples of Output Item1, item2 // if the user selects item1 from comboBox1 and item2 from combo Box2
Item1, item2, item3, item4 // if the user selects item1, item2, item3, item4 from ComboBox1, ComboBox2, comboBox3, comboBox4 respectively
Item3, item1, item4, item2, in case a user selects item 3, item1, item4 and item2 from comboBoxes respectively
Item4, if a user selects item4 from comboBox3
Item4, item3 a case if a user selects item4 from comboBox2 And item3 from comboBox4 for example