I'm reposting this because I didn't get a reply on, hope to get some help sooon.
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
Loading
Rano AHPosted Oct 31, 2013, 2:12 AM
I agree, but this is the requirement, and also this will enable the user to set the format that he needs of the selected item from eachcomboBox to display the related records from DB based on his format / selection.
To make it more clear, here is the example:
comboBox1 has (Item1, Item2, Item3, Item4, Item5)
comboBox2 has (Item1, Item2, Item3, Item4, Item5)
ComboBox3 has (Item1, Item2, Item3, Item4, Item5)
ComboBox4 has (Item1, Item2, Item3, Item4, Item5)
now maybe the user wants to display the related records as per the following format
Item2 from ComboBox4, and item1 from ComboBox3 then the output of the records will be as follows:
Records from Item1 , Records for Item2 // This is as per the seelcted item from ComboBox3 and comboBox4 respecitvely
Later if the user wants to change the selection he migh needs four records and therfore he went to change the selection as
per the follwing format
Item3, Item 5, Item 2, item 4 from CB1,CB2,CB3 and CB4 comboBoxes respecitvely.
them the records will be displayed like this:
Records for Item3, records for Item5, records for item 2, and records for item 4 respecitvely.
This the challenge here (the order of the item that is currently selected from eachcomboBox and also same item cannot be
selected multiple times if it was selected from one comboBox because this will make duplication of the records.
And so on... The user can change the format multiple times and the records will be displayed based on the selection.
Jaganathan BantheswaranPosted Oct 31, 2013, 1:18 AM
Why you are having the same set of items in all the combobox? you can have single combobox with multiselect option right?
Rano AHPosted Oct 30, 2013, 1:50 PM
Is it possible to get solution/help on this within next few hours? I'm tired of trying several logics and codes. I cannot even think one more time of the possible solution which can get the correct result...
I need your help based on your board experience. Please help.
Rano AHPosted Oct 30, 2013, 9:54 AM
Because I'm gonna pass what I've selected from each comboBox as a parameter to a function which reads some data from a database / file
So, If I passed wrong selectedItem then this will display incorrect information. But all what I need is to allow the user to change the format of the selection from the four comboBoxes to choose how many records he needs to see. if he has chosen any two items from any comboBoxes then he can see only the correspondence information/records
if he changed the selection later to select four items from the four comboBoxes then four records will be displayed for him based on the format that he selected.
Hope this will be clear to get the right solution for this.
Jaganathan BantheswaranPosted Oct 30, 2013, 8:57 AM
Rano AHPosted Oct 30, 2013, 6:58 AM
I tried and didn't work :-(
First, the duplication is there, especially when you try to select one item multiple times from first comboBox down to the fourth one. if first comboBox contains 'Select" then it will be replaced of what you have chosen form comboBox4.
Second thing is that the textbox still shows -select- in some cases.
As I said before, I'm going to pass the selected item from each comboBox as a parameter to another function for further processes.
If the selected item is still there for some comboBoxes that contain -select- then this will give incorrect output of the linked processes.
Please help to set the index of the old comboBox to -1without getting exception handling error henever a duplication is occurred. I need only the selected item from the current comboBox to be shown only.
each time I need to set the old combo to -1, exception on null value occurred.
Maybe there is other possible solution/logic that I might try?
Hope to find the solution of this problem by the end of today. I couldn't find the right solution although I tried one million thousand times but no luck :-(
Jaganathan BantheswaranPosted Oct 30, 2013, 6:19 AM
Declare a readonly global variable at top like this,
private readonly List
Replace the entire code of UpdateComboBox method with below,
var selectedItem = comboBox.SelectedItem;
selectedItems.Clear();
foreach (var combo in _comboBoxes)
{
if (combo.SelectedItem == selectedItem)
{
combo.Text = "-Select-";
comboBox.Text = selectedItem.ToString();
comboBox.SelectedItem = selectedItem;
selectedItems.Add(comboBox.Text);
}
else
{
comboBox.SelectedItem = selectedItem;
selectedItems.Add(combo.Text);
}
}
in Button clik,
textBox1.Text = string.Join(", ", selectedItems.Distinct());