Reputation: 754
I have this code so far:
foreach (Control child in TableNamePanel.Controls)
{
if (!(child is CheckBox))
continue;
if ((child as CheckBox).Checked)
{
Code...
}
}
My design has the CheckBoxes
on top of each other and the other does matter. I would like to check the first checkbox
, then the next and so on. Currently it's starting from the bottom. Any way to change that, or to set the order of how it loops through the control items?
Upvotes: 3
Views: 338
Reputation: 39966
Change your foreach
like this:
foreach (Control child in TableNamePanel.Controls.Cast<Control>().OrderBy(c => c.TabIndex))
{
}
This will sort Controls
as per TabIndex
in the form. You might need to change OrderBy
to OrderByDescending
.
Upvotes: 3
Reputation: 176946
I think when you loop through they are in order according to they are in desing
but they are not than you can set CheckBox.TabIndex
property on checkbox to order it
var orderedcheckbox= TableNamePanel.Controls.OfType<CheckBox>()
.OrderBy(c => c.TabIndex);
Here I assumeed that tabindex property is avaialbe as per my knowledge this property is available in .net.
Including other solution suggested in comment by @dotNET
TabOrder of your controls at design-time, which he should have anyway. The other way around could be to use the Location property of your checkboxes instead of TabOrder in the above code.
Upvotes: 4