Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase the size of checkbox in WinForms?

Tags:

.net

winforms

How do I increase the size of a checkbox in a .Net WinForm. I tried Height and Width but it does not increases the Box size.

like image 679
Amitabh Avatar asked Jul 02 '10 13:07

Amitabh


People also ask

How do I fix WinForm size?

Right click on your form and go to properties. Then go to Layout options,see there are a property named Size. Change it as your need as width and length wise.

Will WinForms be deprecated?

WinForms won't be deprecated until Win32 is ... which could be quite sometime! WPF on the other hand has few direct dependencies on Win32 so could potentially form the basis of a "fresh start" UI layer on a future version of windows.


3 Answers

The check box size is hardcoded inside Windows Forms, you cannot mess with it. One possible workaround is to draw a check box on top of the existing one. It is not a great solution since auto-sizing cannot work anymore as-is and text alignment is muddled, but it is serviceable.

Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. Adjust the size of the control so you get the desired box size and ensure it is wide enough to fit the text.

using System;
using System.Drawing;
using System.Windows.Forms;

class MyCheckBox : CheckBox {
    public MyCheckBox() {
        this.TextAlign = ContentAlignment.MiddleRight;
    }
    public override bool AutoSize {
        get { return base.AutoSize; }
        set { base.AutoSize = false; }
    }
    protected override void OnPaint(PaintEventArgs e) {
        base.OnPaint(e);
        int h = this.ClientSize.Height - 2;
        Rectangle rc = new Rectangle(new Point(0, 1), new Size(h, h));
        ControlPaint.DrawCheckBox(e.Graphics, rc,
            this.Checked ? ButtonState.Checked : ButtonState.Normal);
    }
}
like image 154
Hans Passant Avatar answered Oct 28 '22 11:10

Hans Passant


There’s an AutoSize option in the Properties windows; if you turn that off by changing it to False, you will be able to modify the size of your CheckBox.

like image 24
ASertacAkkaya Avatar answered Oct 28 '22 09:10

ASertacAkkaya


C# version, from a forum.codecall.net topic :

 class BigCheckBox : CheckBox
    {
        public BigCheckBox()
        {
            this.Text = "Approved";
            this.TextAlign = ContentAlignment.MiddleRight;              
        }

        public override bool AutoSize
        {
            set { base.AutoSize = false; }
            get { return base.AutoSize; }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            this.Height = 100;
            this.Width = 200;
            int squareSide = 80;

            Rectangle rect = new Rectangle(new Point(0, 1), new Size(squareSide, squareSide));

            ControlPaint.DrawCheckBox(e.Graphics, rect, this.Checked ? ButtonState.Checked : ButtonState.Normal);
        }
    }
like image 24
barbara.post Avatar answered Oct 28 '22 10:10

barbara.post