How to remove the option selected in multiple Comboboxes?

Asked

Viewed 428 times

3

I have several components of the type ComboBox with identical options. The user must select one of them(e.g. "Name"). How to do so when an option is selected, in any one ComboBox, she disappears from all ComboBox?

Do I need a bank? Or a IList?

  • It seems to be two questions. The first on how to save user data and the other on how not to display after it is selected once.

  • 4

    Trying to portray my unfriendly posture in the previous comment: could you edit the question to make it clearer what you are trying to do, and where are you having problems? It’s easier to get an accurate answer if the problem is clear.

  • 1

    I think I understand... he wants to display several combos, with the same values listed, and when selecting a value in any one of them, the same sum value of the other combos, so that only one of the combos can have a specific value at a time.

  • I’ll rephrase the question.... but that’s exactly what Miguel Angelo said....

2 answers

2


Assuming you have three combos in your form with the names of comboBox1, comboBox2, comboBox3. Make sure the code below meets you. Note that in the example I do not use the DataSource and so I have the freedom to change your Items.

public partial class Form1 : Form
{
    private List<ComboBox> _combos;
    private List<string> _originalSource = new List<string> { "um", "dois", "três", "quatro", "cinco" };
    private List<object> _selectedItems = new List<object>();

    public Form1()
    {
        InitializeComponent();
        InitializeCombos();
    }

    private void InitializeCombos()
    {
        _combos = new List<ComboBox> { comboBox1, comboBox2, comboBox3 };
        _combos.ForEach(combo =>
        {
            _originalSource.ForEach(item => combo.Items.Add(item));
            combo.SelectedIndexChanged += RemoveOptionFromCombo;
        });
    }

    private void RemoveOptionFromCombo(object sender, EventArgs e)
    {
        var selectedItem = ((ComboBox)sender).SelectedItem;
        _selectedItems = new List<object> 
        {
            comboBox1.SelectedItem, comboBox2.SelectedItem, comboBox3.SelectedItem
        };

        _combos.ForEach(combo =>
        {
            _originalSource.ForEach(item =>
            {
                if (!combo.Items.Contains(item) && !_selectedItems.Contains(item))
                    combo.Items.Add(item);
                if (combo.Items.Contains(item) && _selectedItems.Contains(item) && !item.Equals(combo.SelectedItem))
                    combo.Items.Remove(item);
            });
        });
    }
}

2

One simple way to do this is by setting the values you need in an array and making a loop that fills the combobox.

Done this you can make the event Combobox.Selectedindexchanged he:

  • Clean other combobox using Combobox.Items.Clear() - or only where it is duplicated as required.
  • Run a new loop to fill the combobox which test and do not include the already selected item.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.