C# All Check box Appearance

In my WinForms Visual Studio application i have a checkbox styled as a Flat Button with this C# code:

 private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (TestBox.Checked == true)
        {
            TestBox.Image = Image.FromFile("M:\\CheckBox_52x.png");
            TestBox.ImageAlign = ContentAlignment.MiddleCenter;
            TestBox.FlatAppearance.BorderSize = 0;
            // make all four (!) BackColors transparent!
            TestBox.BackColor = System.Drawing.Color.Transparent;
            TestBox.FlatAppearance.CheckedBackColor = Color.Transparent;
            TestBox.FlatAppearance.MouseDownBackColor = Color.Transparent;

        }
        else
        {
            TestBox.Image = Image.FromFile("M:\\CheckBoxUncheck_52x.png");
            TestBox.ImageAlign = ContentAlignment.MiddleCenter;
            TestBox.FlatAppearance.BorderSize = 0;
            // make all four (!) BackColors transparent!
            TestBox.BackColor = System.Drawing.Color.Cyan;
            TestBox.FlatAppearance.CheckedBackColor = Color.Cyan;
            TestBox.FlatAppearance.MouseDownBackColor = Color.Cyan;
        }
    }

I was wondering, if instead of doing this to every single checkbox in my application, can i make the "UNCHECKED" version i have coded the default checkbox style for this applicatiom - eg - every time i create a new one it appears with these properties.

Please keep in mind that i am brand new to coding in C#.

Jon Skeet
people
quotationmark

If you want multiple controls to use the same eventhandler, that's easy - just use the same event handler. Change your code to something like:

private void HandleCheckBoxCheckedChanged(object sender, EventArgs e)
{
    CheckBox checkBox = (CheckBox) sender;
    string imageFile;
    Color color;
    if (checkBox.Checked == true)
    {
        // TODO: Use resources embedded within your app
        imageFile = "M:\\CheckBox_52x.png";
        color = Color.Transparent;
    }
    else
    {
        imageFile = "M:\\CheckBoxUncheck_52x.png";
        color = Color.Cyan;
    }
    // TODO: Load each file once and reuse the bitmap, I suspect.
    checkBox.Image = Image.FromFile(imageFile);
    checkBox.ImageAlign = ContentAlignment.MiddleCenter;
    checkBox.FlatAppearance.BorderSize = 0;
    checkBox.BackColor = color;
    checkBox.FlatAppearance.CheckedBackColor = color;
    checkBox.FlatAppearance.MouseDownBackColor = color;
}

You can then attach the same handler to all your checkboxes.

If you have multiple classes, you could make that a public static method. At that point you may need to add the event handler in code rather than getting the designer to do it - I don't know whether the designer knows how to use static methods for event handlers. But it would just be something like:

TestBox.CheckedChanged += CheckBoxUtilities.HandleCheckBoxCheckedChanged;

That's if you really just want the same code to be used for event handlers. Other things to consider are:

  • Constructing a subclass of CheckBox as suggested by rakatherock. My own experience with creating custom controls in Windows Forms has not been great, but from an OO perspective it feels fine. An initial implementation could just derive from CheckBox and implicitly add an event handler which does exactly what your current code does.
  • If you want to find all the CheckBox controls in a form at some point, you can use the Controls property and then recurse through any control which itself a container. I won't go into the details of that now though, as it sounds like you don't really want this - unless you did it just to find all the CheckBox controls and add the same event handler to all of them.

people

See more on this question at Stackoverflow