How to increase the size of checkbox in WinForms?
It support Flat
using System.Drawing;
using System.Windows.Forms;
public class TodoCheckBox : CheckBox
{
public override bool AutoSize
{
get => base.AutoSize;
set => base.AutoSize = false;
}
public TodoCheckBox()
{
this.TextAlign = ContentAlignment.MiddleRight;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
int h = this.ClientSize.Height - 2;
var rc = new Rectangle(new Point(-1, this.Height / 2 - h / 2), new Size(h, h));
if (this.FlatStyle == FlatStyle.Flat)
{
ControlPaint.DrawCheckBox(e.Graphics, rc, this.Checked ? ButtonState.Flat | ButtonState.Checked : ButtonState.Flat | ButtonState.Normal);
}
else
{
ControlPaint.DrawCheckBox(e.Graphics, rc, this.Checked ? ButtonState.Checked : ButtonState.Normal);
}
}
}
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
.
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);
}
}
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);
}
}