user7115764
user7115764

Reputation: 63

How do I change the shape of a checkbox?

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.sstatic.net/XzbPe.jpg

This is what the "Big" checkbox should look like

Upvotes: 0

Views: 4352

Answers (3)

TaW
TaW

Reputation: 54453

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..

Upvotes: 4

Yuriy Zaletskyy
Yuriy Zaletskyy

Reputation: 5151

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

Upvotes: 1

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(); 
 }

Upvotes: 0

Related Questions