-2

Is there anyway to change the color of the box containing the check mark in checkbox for winform? Something like this.

enter image description here

Question is to change the color of the background of the box not the check mark.

Lightsout
  • 3,454
  • 2
  • 36
  • 65

1 Answers1

1

There is no a straight way to do it.

The easiest way if you necessarily have to use winforms is to mimic a Button and use images.

To do it, edit your CheckBoxes properties to the following.

  • Appearance: Normal -> Button
  • FlatStyle: Standard -> Flat
  • FlatAppearance, BorderSize: 1 -> 0
  • FlatAppearance, All the colors: the background color of the parent container, usually is Control
  • TextImageRelation: Overlay -> ImageBeforeText
  • Image: load an image of an unchecked box

Then in the event handler CheckedChanged of each CheckBox, we change the image from an unchecked box to a checked box and viceversa, something like this:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    CheckBox currentCheckBox = (sender as CheckBox);
    if (currentCheckBox.Checked)
    {
        currentCheckBox.Image = Properties.Resources._checked;
    }
    else
    {
        currentCheckBox.Image = Properties.Resources._unchecked;
    }
}

Or, shorter:

private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
    (sender as CheckBox).Image = (sender as CheckBox).Checked ? Properties.Resources._checked : Properties.Resources._unchecked;
}

The result should be something like this: Result

Raul289
  • 11
  • 1