0

Just a silly display question, but how do I edit the shape of my checkbox in WinForms?

To be specific, instead of the checkmark when I click on the 3 state checkbox, I would like a square. I saw this in a homework assignment and it's purely display, but I just can't find where to edit it.

I'm using Visual Studio C# for Windows Forms btw.

enter image description here

https://i.stack.imgur.com/XzbPe.jpg

This is what the "Big" checkbox should look like

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
user7115764
  • 63
  • 1
  • 9

3 Answers3

4

You can try this code:

    private void checkBox1_Paint(object sender, PaintEventArgs e)
    {
        CheckState cs = checkBox1.CheckState;
        if (cs == CheckState.Indeterminate)
        {
            using (SolidBrush brush = new SolidBrush(checkBox2.BackColor))
                e.Graphics.FillRectangle(brush, 0, 1, 14, 14);
            e.Graphics.FillRectangle(Brushes.Green, 3, 4, 8, 8);
            e.Graphics.DrawRectangle(Pens.Black, 0, 1, 13, 13);
        }
    }

enter image description here

This should be easy to modify if you want something else..

Note that you may need to adapt it when changing fonts and surely will have to modify it when changing the alignments..! Also when changing DPI. Or themes. Or Windows versions. Or half a dozen other things. So this is more an example than a recommendation!

You may also read the interesting comments here.. and this example of more involved checkbox drawing..

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111
  • 1
    There is no doubt that you'll need to adapt it when changing fonts. Or DPI. Or themes. Or Windows versions. Or half a dozen other things that are the reason why this kind of hackish custom-drawing is such a terrible idea. Sorry, this is a reasonable answer that appears to do what is being asked for, so I am not trying to pick on you. This is just a soapbox of mine, a major usability blunder, and somewhere where I see so many inexperienced developers going wrong. – Cody Gray - on strike Dec 08 '16 at 17:34
  • Full Ack my friend! Updated with more reasons/limits of its usefulness. – TaW Dec 08 '16 at 17:42
1

In order to modify shape any control you need to use Paint event. For example if you add two radio buttons at form, and for each Paint event bind following code:

private void radioButton_Paint(object sender, PaintEventArgs e)
{
        Graphics graphics = e.Graphics;
        graphics.Clear(BackColor);

        int offset = 2;
        SizeF stringMeasure = graphics.MeasureString(radioButton1.Name, Font);
        // calculate offsets
        int leftOffset = offset + Padding.Left;
        int topOffset = (int)(ClientRectangle.Height - stringMeasure.Height) / 2;

        if (topOffset < 0)
        {
            topOffset = offset + Padding.Top;
        }
        else
        {
            topOffset += Padding.Top;
        }

        graphics.FillRectangle(new SolidBrush(Color.AliceBlue), 0, 0, leftOffset + 10, topOffset + 10);
        graphics.DrawRectangle(new Pen(Color.Green), new Rectangle(0, 0, leftOffset + 10, leftOffset + 10));

        graphics.DrawString(radioButton1.Text, (sender as RadioButton).Font, new SolidBrush(Color.IndianRed), 15, 0);

        if( (sender as RadioButton).Checked)
        {
            graphics.FillRectangle(new SolidBrush(Color.Yellow), 1, 1, leftOffset + 8, 10);
        }

}

you'll see following picture:

Radio button behaviour code demo

Yuriy Zaletskyy
  • 4,983
  • 5
  • 34
  • 54
0

You have to play with the CheckState property of the checkbox using the Checked, Unchecked or Indeterminate state

a pretty str.forw example:

private void AdjustMyCheckBoxProperties()
 {
    // Change the ThreeState and CheckAlign properties on every other click.
    if (!checkBox1.ThreeState)
    {
       checkBox1.ThreeState = true;
       checkBox1.CheckAlign = ContentAlignment.MiddleRight;
    }
    else
    {
       checkBox1.ThreeState = false;
       checkBox1.CheckAlign = ContentAlignment.MiddleLeft;
    }

    // Concatenate the property values together on three lines.
    label1.Text = "ThreeState: " + checkBox1.ThreeState.ToString() + "\n" +
                  "Checked: " + checkBox1.Checked.ToString() + "\n" +
                  "CheckState: " + checkBox1.CheckState.ToString(); 
 }
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97