Reputation: 17382
I have a tab control in my Windows Form, and I want to iterate over each element in two different tabs. When a file is opened, I want all the elements of both to be enabled, and when the file is closed, all to be disabled.
I have no clue how to accomplish this, however, because the controls aren't in an array or list, but in a ControlsCollection. I asked a second ago about foreach statements and learned a bit about lambda, but I don't know how I can apply it here.
Here's what I have:
List<Control.ControlCollection> panels = new List<Control.ControlCollection>();
panels.Add(superTabControlPanel1.Controls);
panels.Add(superTabControlPanel2.Controls);
foreach(Control.ControlCollection ctrlc in panels){
foreach (Control ctrl in ctrlc) {
}
}
Is this possible with one foreach statement, or somehow simpler?
Upvotes: 4
Views: 2437
Reputation: 17508
I would probably Union the two collections and avoid using a Generic List<T>
all together.
var controls = superTabControlPanel1.Controls.Union(superTabControlPanel2.Controls);
foreach (var control in controls) {
//...
}
Or you could chain multiple collections together.
var controls =
(superTabControlPanel1.Controls)
.Union
(superTabControlPanel2.Controls)
.Union
(superTabControlPanel3.Controls);
Upvotes: 0
Reputation: 15810
Did you know that if you disable a parent control, then all nested controls are disabled too? Simply disabling the two tab panels will disable all children too. Enabling the panel reverts the effect.
I know this doesn't answer your question, but it is a better solution.
Upvotes: 1
Reputation: 35116
I would do something like this:
List<Control> controls = new List<Control>();
controls.AddRange(superTabControlPanel1.Controls.GetControls());
controls.AddRange(superTabControlPanel2.Controls.GetControls());
foreach(Control ctrl in controls){
//Do something
}
I don't have VS handy at the moment, and not sure if GetControls() exists. But you can use the general idea: instead of collection of panels, have a collection of controls, and iterate through controls in one loop.
Upvotes: 0