0

So I have a for each loop that gets all of the checkboxes on a panel, it successfully loops through them and returns the name and checked state. However in my if statement "cb->Checked" also tried "cb->Checked == true" it doesn't seem to enter this if statement. What am I doing wrong?

    int counter = 0;

    for each (Control^ c in page_General->Controls)
    {
        if (c->GetType() == CheckBox::typeid)
        {
            CheckBox^ cb = static_cast<CheckBox^>(c->Parent);
            MessageBox::Show("test " + counter);
            if (cb->Checked)
            {
                MessageBox::Show("test " + counter);
                generalSkill[counter].canAccess = true;
                counter++;
            }
        }
    }
  • @πάντα ῥεῖ: Are you sure that's a duplicate? The question you're pointing to sums up as "cast to CheckBox and use the Checked property", which he's doing. – David Yaw Feb 10 '17 at 21:27
  • Why are you casting `c->Parent` to checkbox? Try casting `c` itself instead. Also, consider doing a `dynamic_cast` and check for null, rather than checking `GetType == Checkbox`: dynamic cast will work better if the object is a subclass of the one you're interested in, which shouldn't make a difference here, but is a good habit to get into. – David Yaw Feb 10 '17 at 21:59
  • @DavidYaw Thankyou very much! I had a feeling it was something to do with that, calling c itself fixed it. Extra thanks for helping even though it got marked as duplicate.. – kyleduder Feb 10 '17 at 22:15

0 Answers0