Click here to Skip to main content
15,867,962 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have three combobox with same values in each like X1, X2, X3, X4, X5. when Combobox1 select X1, combobox2 and combobox3 should not have X1. same way for Combobox2 and Combobox3.

What I have tried:

if (comboBox2.SelectedItem.ToString() != "--")
            {
                foreach (string val in digital)
                {
                    if (val == comboBox2.SelectedItem.ToString())
                    {
                        comboBox1.Items.Remove(val);
                        comboBox3.Items.Remove(val);
                    }
                }
            }
            else
            {
                foreach (string mis in digital)
                {
                    if (!comboBox1.Items.Contains(mis))
                    {
                        comboBox1.Items.Add(mis);
                    }
                    if (!comboBox3.Items.Contains(mis))
                    {
                        comboBox3.Items.Add(mis);
                    }
                }
            }
Posted
Updated 3-Mar-21 22:34pm
Comments
BillWoodruff 4-Mar-21 4:40am    
Given the user can select up to 3 distinct values from 5 values, are ComboBoxes the best UI ? Perhaps use a CheckedListBox where selection is limited to 3 choices ?

In this case you have to remove the selected Item from ComboBox1 from the other 2 Comboboxes. The same with each of the others.
But of course you also need an Init-method (or a default-Item) which sets the Item-list of each Combobox back to default ...

What is "digital" in your code-snippet ? The List of the default-Items ?

Because it isn't as trivial as I thought at the first sight here is my Code - but apologize I'm a VB-programmer. But I think you can easyly convert it to C#.

VB.Net-Version :
VB
Dim cb_Items As String() = {"--", "X1", "X2", "X3", "X4", "X5"}
Dim cb_lastItem As String() = {"--", "--", "--"}

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    ComboBox1.Items.Clear()
    ComboBox1.Items.AddRange(cb_Items)
    ComboBox2.Items.Clear()
    ComboBox2.Items.AddRange(cb_Items)
    ComboBox3.Items.Clear()
    ComboBox3.Items.AddRange(cb_Items)
End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    Dim sel As String = ComboBox1.SelectedItem

    If sel <> cb_Items(0) Then
        ComboBox2.Items.Remove(sel)
         ComboBox3.Items.Remove(sel)
    End If

    If cb_lastItem(0) <> cb_Items(0) Then
        ComboBox2.Items.Add(cb_lastItem(0))
        ComboBox3.Items.Add(cb_lastItem(0))
    End If

    cb_lastItem(0) = sel
End Sub

Private Sub ComboBox2_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
    Dim sel As String = ComboBox2.SelectedItem

    If sel <> cb_Items(0) Then
        ComboBox1.Items.Remove(sel)
        ComboBox3.Items.Remove(sel)
      End If

    If cb_lastItem(1) <> cb_Items(0) Then
        ComboBox2.Items.Add(cb_lastItem(1))
        ComboBox3.Items.Add(cb_lastItem(1))
    End If

    cb_lastItem(1) = sel
End Sub

Private Sub ComboBox3_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
    Dim sel As String = ComboBox3.SelectedItem

    If sel <> cb_Items(0) Then
        ComboBox1.Items.Remove(sel)
         ComboBox2.Items.Remove(sel)
     End If

    If cb_lastItem(2) <> cb_Items(0) Then
        ComboBox1.Items.Add(cb_lastItem(2))
        ComboBox2.Items.Add(cb_lastItem(2))
    End If

    cb_lastItem(2) = sel
End Sub


C#-Version :
C#
private string[] cb_Items = new[] { "--", "X1", "X2", "X3", "X4", "X5" };
private string[] cb_lastItem = new[] { "--", "--", "--" };

private void Button1_Click(System.Object sender, System.EventArgs e)
{
    ComboBox1.Items.Clear();
    ComboBox1.Items.AddRange(cb_Items);
    ComboBox2.Items.Clear();
    ComboBox2.Items.AddRange(cb_Items);
    ComboBox3.Items.Clear();
    ComboBox3.Items.AddRange(cb_Items);
}

private void ComboBox1_SelectedIndexChanged(System.Object sender, System.EventArgs e)
{
    string sel = ComboBox1.SelectedItem;

    if (sel != cb_Items[0])
    {
        ComboBox2.Items.Remove(sel);
        ComboBox3.Items.Remove(sel);
    }

    if (cb_lastItem[0] != cb_Items[0])
    {
        ComboBox2.Items.Add(cb_lastItem[0]);
        ComboBox3.Items.Add(cb_lastItem[0]);
    }

    cb_lastItem[0] = sel;
}

private void ComboBox2_SelectedIndexChanged(System.Object sender, System.EventArgs e)
{
    string sel = ComboBox2.SelectedItem;

    if (sel != cb_Items[0])
    {
        ComboBox1.Items.Remove(sel);
        ComboBox3.Items.Remove(sel);
    }

    if (cb_lastItem[1] != cb_Items[0])
    {
        ComboBox2.Items.Add(cb_lastItem[1]);
        ComboBox3.Items.Add(cb_lastItem[1]);
    }

    cb_lastItem[1] = sel;
}

private void ComboBox3_SelectedIndexChanged(System.Object sender, System.EventArgs e)
{
    string sel = ComboBox3.SelectedItem;

    if (sel != cb_Items[0])
    {
        ComboBox1.Items.Remove(sel);
        ComboBox2.Items.Remove(sel);
    }

    if (cb_lastItem[2] != cb_Items[0])
    {
        ComboBox1.Items.Add(cb_lastItem[2]);
        ComboBox2.Items.Add(cb_lastItem[2]);
    }

    cb_lastItem[2] = sel;
}
 
Share this answer
 
v4
Comments
Member 10738387 4-Mar-21 4:53am    
yes digital is array[] with values {"X1","X2","X3","X4","X5"}
Ralf Meier 4-Mar-21 7:02am    
I have added some code for you ... perhaps it helps ...
Member 10738387 5-Mar-21 2:24am    
following got failed: if X3 is selected in combobox3, x3 will be removed from combobox1 and combobox2. but when user again try to select X4 in combobox3, the X3 is not adding back into all three combobox.
Ralf Meier 5-Mar-21 2:35am    
With my code that works ... perhaps youi had made a conversion or copying-mistake.
Did you realized that the re-added Entry is positioned at the end of the List of the ComboBox ? If you want to have it at it's origin place you should make a kind of sorting after re-adding ...
Ralf Meier 5-Mar-21 2:36am    
I have added the C#-Version to the code too (but it is not tested) ...
The simplest way is:
C#
//all values used in all comboboxes
//this should be "global" variable scope of form
List<string> allValues = new List<string>(){"X1", "X2", "X3", "X4", "X5"};
//for combobox3 
//combobox1 has selected "X1" and combobox2 has selected "X5"
List<string> usedValues = new List<string>(){Combobox1.Text, Combobox2.Text};
//then the source list for combobox3 can be assigned this way:
List<string> comboValues = allValues.Except(usedValues).ToList()


Do not forget change the list of used values when any of comboboxes selected value has been changed.

More details at MSDN: Enumerable.Except Method (System.Linq) | Microsoft Docs[^]
 
Share this answer
 
Comments
Member 10738387 4-Mar-21 5:16am    
what will be for combobox2? if any value change in combobox3 then, the exist value of combobox3 should add into combobox2 and combobox1?
Maciej Los 4-Mar-21 9:34am    
C'mon! Think of it!
You need to create only one subroutine, which loops through the comboboxes...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900