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);
});
});
}
}
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.
– Felipe Avelar
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.
– bfavaretto
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.
– Miguel Angelo
I’ll rephrase the question.... but that’s exactly what Miguel Angelo said....
– AndersonBP