jeudi 31 mars 2016

C# For-Each Controls

i have this code within my Public Form1():

foreach (Control c in Controls)
            {
                if (c is CheckBox)
                {

                }
            }

what i'm looking to do is trigger an event when any of the CheckBoxes are clicked, it doesn't matter which one. so, for example:

if(CheckBox.Checked == True) {
 //do something to the checked checkbox 
}

but of course, this throws the error, because i havent specified which checkbox:

An object reference is required for the non-static field, method, or property 'CheckBox.Checked'

is there a way around this? Because i have the same event applied to every single checkbox like this:

 if (TestBox.Checked == true)
 {//Do Something}
 else {//Do Something}

 if (TestBox2.Checked == true)
 {//Do Something}
 else {//Do Something}

which makes my code horridly inefficient.




Aucun commentaire:

Enregistrer un commentaire