AndyBernard
AndyBernard

Reputation: 115

Disable (#) of comboboxes located below another combobox based on its selected value

Say I have multiple comboboxes stacked on top of one another.

**[_______] Combobox #1  
  [_______] Combobox #2  
  [_______] Combobox #3  
  [_______] Combobox #4  
  [_______] Combobox #5  
  [_______] Combobox #6  
  [_______] Combobox #7**  

If at any time I select the drop down from a combobox which is fed from some list of values (for simplicity sake, lets say numbers 1-5), the value the combobox selects will disable that # of comboboxes below it. So if Combobox #1 selected a value "3" Comboboxes #2, #3, & #4 will be disabled or have some value associated like "0" to know it is now out. And Subsequently I can continue and say I select combobox #5 and select a value of "2" then in the end it would look something like this:

**[___3___] Combobox #1**  
  [_______] Combobox #2  
  [_______] Combobox #3  
  [_______] Combobox #4  
**[___2___] Combobox #5**  
  [_______] Combobox #6  
  [_______] Combobox #7  

(Comboboxes 2,3,4,6,7 being disabled). I would also need to reset the process, meaning changing the selected value will change and update the number of disabled boxes accordingly.

Rough Idea in code of what I'd like to achieve:

private void combobox#_SelectedValueChanged(object sender, Eventargs e)  
{  
    for (x in combobox#.SelectedValue)  
        combobox[#+x].Enabled = false;  
}  

Any ideas how to make this possible? Thank you! Ideally in C# winform but open to WPF if it provides a better solution.

Upvotes: 0

Views: 272

Answers (1)

Flydog57
Flydog57

Reputation: 7111

Put your combo boxes in a list or an array, something like:

var comboBoxes = new List<ComboBox> { comboBox1, comboBox2, comboBox3, ... };

Then use code like you have, but use an array index instead of trying to compose the name. Remember that a collection of combo boxes (or of any other reference type) is just a collection of object references. They don't cost very much.

Upvotes: 1

Related Questions