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

http://imgur.com/a/SkHW9

This is what the "Big" checkbox should look like

ΦXocę 웃 Пepeúpa ツ
  • 45,713
  • 17
  • 64
  • 91
  • You will probably have to owner-draw it. Can you show an image of the 3 states you want? – TaW Dec 08 '16 at 16:27
  • ok, I'll upload to imgur – user7115764 Dec 08 '16 at 16:28
  • And what should the 3rd state look like? By default it is a black (or when focused blue) square. – TaW Dec 08 '16 at 16:49
  • You *already* get a square when the three-state checkbox is used. Make sure that it is set to `CheckState.Indeterminate`. – Cody Gray Dec 08 '16 at 17:32
  • You cab customize shape and panting of and [CheckBox](http://stackoverflow.com/a/38432140/3110834) and [RadioButton](http://stackoverflow.com/a/38390028/3110834). – Reza Aghaei Dec 09 '16 at 08:20
  • Have you resolved your problems? – TaW Jan 08 '17 at 13:42
  • afraid not, but since it's simply an aesthetic change I've moved on. I'll try to attempt to implement the different results here a second time eventually though, since the answers are very interesting. – user7115764 Jan 09 '17 at 18:26

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
  • 50,936
  • 8
  • 63
  • 99
  • 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 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,783
  • 5
  • 34
  • 51
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 ツ
  • 45,713
  • 17
  • 64
  • 91