-1

I have this code and it seems like it should work but for some reason it doesnt.

foreach(Control c in Controls.OfType<CheckBox>())  {              
                Check_checked += 1;
            }

** Check edit: When I step through the code the Control c is picking up all of the textboxes but nothing else.

Any help would be appreciated.

**I realise that ofType should not be picking up text/labels/watever.

Im getting text/labels/watever.

I moved my code to another computer also running visual studio. It doesnt pickup checkboxes and it doesnt seem to be bugging about picking up text/label/watever on that system. So I think one of the issues is my VS is bugger-up. Will re-install now.

GenGen
  • 103
  • 1
  • 12
  • I think the Checkbox control is a child control so it may be on a panel or something. So you need to iterate the parent control on which Checkbox's are or iterate all children on all the controls to find all Checkbox's. – Mayank Sep 10 '16 at 23:24
  • the checked variable is actually called Check_checked I just re-wrote the code again in the form entry to make it smaller and a bit easier to read. I will change it to its original to help. – GenGen Sep 10 '16 at 23:25
  • How are you concluding that it's looping on textboxes? I'll be blunt: I don't believe it. Try executing `Console.WriteLine(c.GetType().Name);` inside the loop, what do you get? – sstan Sep 10 '16 at 23:32
  • + c {System.Windows.Forms.Label, Text: Base Price : $10.00} System.Windows.Forms.Control {System.Windows.Forms.Label} – GenGen Sep 10 '16 at 23:36

2 Answers2

0

look at following answer

Get all controls of a specific type

Code check if the control is a ContainerControl and then iterates over all the children of that control to find the controls of type.

public static IEnumerable<T> GetControlsOfType<T>(Control root)
    where T : Control
{
    var t = root as T;
    if (t != null)
        yield return t;

    var container = root as ContainerControl;
    if (container != null)
        foreach (Control c in container.Controls)
            foreach (var i in GetControlsOfType<T>(c))
                yield return i;
}

Then you could do something like this:

foreach (var pictureBox in GetControlsOfType<CheckBox>(form)) {

}
Community
  • 1
  • 1
Mayank
  • 8,777
  • 4
  • 35
  • 60
  • @GrantWinney: just got a mail from workmate suggesting a reinstall of visual studio? Taken him an hour to look through the code and he cannot figure out why its looking at textboxes – GenGen Sep 10 '16 at 23:31
  • 1
    @GrantWinney I think his code isn't what he's describing. I think he's getting TextBox's as he's just iterating over Controls not Controls.OfType (which may be empty) – Mayank Sep 10 '16 at 23:31
0

This code is a simple solution for counting checkboxes on a form that are checked.

    private int CountChecks(IEnumerable controls)
    {
        var result = 0;
        foreach (Control xControl in controls)
        {

            if (xControl.HasChildren) result += CountChecks(xControl.Controls);

            if (!(xControl is CheckBox)) continue;
            if (!(xControl as CheckBox).Checked) continue;
            result++;
        }
        return result;
    }

you might use this in this manner:

var howManyAreChecked = CountChecks(Controls);

This would have to be in a form to use this syntax. You must pass a forms Controls into the method in order to work correctly.

Marc Lyon
  • 336
  • 2
  • 7