A property to change many checkBoxes ForeColor property?

Hi,

I have created a user control that has 32 checkBoxes on it.  I have added this user control to my mainForm and want to create a property that changes the ForeColor of any given checkBox.  So, for example, say if a some kind of event means that checkBox1 should change its ForeColor property, how do I use a property that will take checkBox1 as the checkBox which needs to be set?
mitesh114Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mitesh114Author Commented:
I also want to create a property which gets the Checked property of a given checkBox.
edin_el_bosnewiCommented:
What do you mean bay:

how do I use a property that will take checkBox1 as the checkBox which needs to be set?
thetoolCommented:
Ok, to find out if a checkbox is checked you would use CheckBox.Checked.  This returns a bool with true or false depending on if it is checked or not.  

To change the ForeColor (text color) of the check box you would use checkBox1.ForeColor=System.Drawing.Color.Navy;  If you are actually wanting to change the color of the textbox I do not believe it is possible.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

thetoolCommented:
Sorry, I meant If you are actually wanting to change the color of the checkbox I do not believe it is possible.
mitesh114Author Commented:
What I mean is that I have more than 1 checkBox and I do not want to type for example checkBox1.ForeColor = Color.Red and then checkBox2.ForeColor = Color.Red and so on for each checkBox.  I wanted to create a property that would take each checked checkBox and change its color.  So, for example if i have checkBox1, checkBox2, checkBox3, checkBox4 etc  then i want to call a property that gets and sets the color property of each checkbox.  I hope this makes sesne.

brian78waCommented:
i cant remember the exact syntax but you could try something similar to this

public void ChangeForeColor(CheckBox chkBox)
{
       chkBox.ForeColor = color.red;
}


ChangeForeColor(CheckBox1);

like i said i cant remember the exact syntax but something along these lines might work
Bob LearnedCommented:
Or, something like this might work, too:

    public override Color ForeColor
    {
      get
      {
        return base.ForeColor;
      }
      set
      {

        base.ForeColor = value;
        foreach (Control ctl in this.Controls)
        {

          if (ctl.GetType() == typeof(CheckBox))
            ((CheckBox)ctl).ForeColor = value;
        }
      }
    }


Bob
mitesh114Author Commented:
I don't understand why I need to override the ForeColor property??
mitesh114Author Commented:
Also when i call the property in the mainForm, how do I pass the checkBox to it?
Bob LearnedCommented:
Ok, then:

private Color _checkForeColor = SystemColors.WindowText;

public override Color CheckBoxForeColor
    {
      get
      {
        return _checkForeColor;
      }
      set
      {

        _checkForeColor = value;
        foreach (Control ctl in this.Controls)
        {

          if (ctl.GetType() == typeof(CheckBox))
            ((CheckBox)ctl).ForeColor = value;
        }
      }
    }

Bob
Bob LearnedCommented:
Overriding the ForeColor property would allow you to change the ForeColor for the UserControl, and then set the check boxes to the same color.

Bob
Bob LearnedCommented:
To set the color of a specific control, then use brian78wa's answer.

Bob
Bob LearnedCommented:
I ran with the title "change many checkBoxes ForeColor" :)

Bob
mitesh114Author Commented:
I tried using the override property, but this changes the color of all the texts in my property and not just the checkBoxes.

I only want to change the color if the checked property is true for each of the checkBoxes in the user control.  I am not sure if I am making myself very clear with this question!!  Eg: if checkBox1.Checked = true then set color of that checkbox to red, if checkBox6.Checked = true then set the color of that checkbox to red.  I thought there maybe a simple way of using the number of the checkBox (i.e. checkBox1) to set the forecolor of the control.  If I have to use brian78wa's answer then I am effectively going to be calling that method for each of my checkboxes.  Would it possible to create an array that holds the value of either the color or checked property of the control and then create a property that gets these values.  How would I then do that?
Bob LearnedCommented:
Sorry for misunderstanding your requirement.

Try this untested code developed in the comment block:

public void ChangeCheckBoxForeColor(Color checked, Color unchecked)

    foreach (Control ctl in this.Controls)
        {

          if (ctl.GetType() == typeof(CheckBox))
          {
              CheckBox chk = (CheckBox(ctl);
              if (chk.Checked)
                 chk.ForeColor = checked;
              else
                 chk.ForeColor = unchecked;
        }
      }
    }

Bob

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brian78waCommented:
Should be CheckBocx chk = (CheckBox) ctl
brian78waCommented:
oops i meant CheckBox chk = (CheckBox) ctl
if you are wanting to change the color of all the checked boxes then TheLearnedOne's response would be the best approach.
otherwise if you are trying to change specific checkboxes mine would work for that.
mitesh114Author Commented:
For some reason, the CheckBoxes in the user control are not in this.Controls collection.  How do I ensure that only they are in the this.Controls collection, as at the moment, it searches through the buttons on the user control.  I think this method will work, but at the moment it does not affect any of the checkboxes.  Could you please help me out with this?!  Thanks!
Bob LearnedCommented:
Are they in a Panel control or other such container control?

Bob
mitesh114Author Commented:
Yes the checkBoxes are in a panel control, but at the moment, the code seems to access the controls within another panel control...
Bob LearnedCommented:
The Controls collection only contains the top-level controls, not the ones contained by other controls:

Try something like this instead:

foreach (Control ctl in this.panel1.Controls)

Bob
mitesh114Author Commented:
OK, I changed the this.Controls to this.panel1.Controls and this works fine!!
mitesh114Author Commented:
I have increased the points and split them between Bob and Brian...thank you for all your help! :o)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.