Nerdynosaur
Nerdynosaur

Reputation: 1888

How to remove the Wpf combo box item using C#

i have 3 combo boxes with the same 3 items(a,b,c). If i select "a" in combobox1, "a" will remove from combobox2, items left in combobox2 will be "b" & "c". And then i select "b" in combobox2, "b" will remove from combobox3 and the item in combobox3 will be "a" & "c". The removed item will restore back again into the combo box if the previous combobox going through the selectionChanged. I tried some codes i found on the internet, but doesnt work...the selected item from the previos combobox is not being removed.

My code for combo boxes:

<ComboBox Name="firstCombo" SelectionChanged="firstCombo_SelectionChanged">
    <ComboBoxItem Content="A"></ComboBoxItem>
    <ComboBoxItem Content="B"></ComboBoxItem>
    <ComboBoxItem Content="C"></ComboBoxItem>
</ComboBox>

<ComboBox Name="secondCombo" SelectionChanged="secondCombo_SelectionChanged">
    <ComboBoxItem Content="A"></ComboBoxItem>
    <ComboBoxItem Content="B"></ComboBoxItem>
    <ComboBoxItem Content="C"></ComboBoxItem>
</ComboBox>

<ComboBox Name="thirdCombo" >
    <ComboBoxItem Content="A"></ComboBoxItem>
    <ComboBoxItem Content="B"></ComboBoxItem>
    <ComboBoxItem Content="C"></ComboBoxItem>
</ComboBox>

my C# code:

private void firstCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    secondCombo.Items.Remove(firstCombo.SelectionBoxItem);         
}

private void secondCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    thirdCombo.Items.Remove(secondCombo.SelectionBoxItem);         
}

Upvotes: 0

Views: 4269

Answers (4)

ChrisF
ChrisF

Reputation: 137148

Rather than adding and removing items you could just change the visibility of the individual items.

If you bind it in the XAML (through a converter) the "removal" and "readding" would happen automatically.

<ComboBox Name="firstCombo">
    <ComboBoxItem Content="A"></ComboBoxItem>
    <ComboBoxItem Content="B"></ComboBoxItem>
    <ComboBoxItem Content="C"></ComboBoxItem>
</ComboBox>

<ComboBox Name="secondCombo">
    <ComboBoxItem Content="A"
                  Visiblity="{Binding ElementName=firstCombo Path=SelectedIndex,
                              Converter=IndexToVisibiltyConverter,
                              ConverterParameter=A></ComboBoxItem>
    <ComboBoxItem Content="B"
                  Visiblity="{Binding ElementName=firstCombo Path=SelectedIndex,
                              Converter=IndexToVisibiltyConverter,
                              ConverterParameter=B></ComboBoxItem>
    <ComboBoxItem Content="C"
                  Visiblity="{Binding ElementName=firstCombo Path=SelectedIndex,
                              Converter=IndexToVisibiltyConverter,
                              ConverterParameter=C></ComboBoxItem>
</ComboBox>

<ComboBox Name="thirdCombo">
    <ComboBoxItem Content="A"
                  Visiblity="{Binding ElementName=secondCombo Path=SelectedIndex,
                              Converter=IndexToVisibiltyConverter,
                              ConverterParameter=A></ComboBoxItem>
    <ComboBoxItem Content="B"
                  Visiblity="{Binding ElementName=secondCombo Path=SelectedIndex,
                              Converter=IndexToVisibiltyConverter,
                              ConverterParameter=B></ComboBoxItem>
    <ComboBoxItem Content="C"
                  Visiblity="{Binding ElementName=secondCombo Path=SelectedIndex,
                              Converter=IndexToVisibiltyConverter,
                              ConverterParameter=C></ComboBoxItem>
</ComboBox>

*NOTE: Converter definition not legal syntax - illustrative only!

You could bind to the displayed text or selected value - whatever was most convenient.

The converter would check the index/text/value against the parameter and return Visibility.Visible or Visibility.Collapsed as appropriate.

Upvotes: 0

bonCodigo
bonCodigo

Reputation: 14361

That to say, perhaps.

String strCombo1 = comboBox1.SelectedItem.ToString(); 
comboBox2.Items.Remove(strCombo1);

Upvotes: 0

NEO
NEO

Reputation: 213

You can use SelectedIndex to remove it but be aware of it if you have deleted something before or not because if you delete it already then the index isn't the same:

private void firstCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    secondCombo.Items.RemoveAt(firstCombo.SelectedIndex);         
}

Upvotes: 0

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174319

I guess the problem is that those are actually different ComboBoxItem instances. They have the same text but they are still different instances. So, SelectionBoxItem from secondCombo will not be found in thirdCombo.Items and thus it won't be removed.

You need to remove it based on the displayed text.

Upvotes: 1

Related Questions