How do I increase the size of a checkbox i开发者_如何学Gon a .Net WinForm. I tried Height and Width but it does not increases the Box size.
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);
}
}
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);
}
}
A simple workaround is to use the appearance property of the CheckBox. Use the following code to make the checkbox look like a button. It will function as a checkbox. Now the size can be easily changed along with the text size.
checkBox1.Appearance = Appearance.Button;
checkBox1.Font = new Font("Microsoft Sans Serif", 16);
checkBox1.AutoSize = false;
checkBox1.Size = new Size(100, 100);
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);
}
}
}
If anyone needs VB.NET code, I adapted this code to it. I didn't fool with AutoSize
in the class. The control should be added to the toolbox once the project is built. You can set AutoSize
to False
there the same as you would any other control.
If it matters, I just put the class code below the End Class
of the form I was using it in.
To clarify what AutoSize
does, the advantage of this new control is that the "box" portion of the checkbox can be made bigger. In the normal checkbox, you cannot change the box portion.
The only disadvantage of this new control that I see is that when you resize it the box portion overruns the text if left aligned; fix this with the TextAlign
property.
Public Class NewCheckBox
Inherits CheckBox
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
'Make the box you check 3/4 the height
Dim boxsize As Integer = Me.Height * 0.75
Dim rect As New Rectangle(
New Point(0, Me.Height / 2 - boxsize / 2),
New Size(boxsize, boxsize)
)
ControlPaint.DrawCheckBox(e.Graphics, rect, If(Me.Checked, ButtonState.Checked, ButtonState.Normal))
End Sub
End Class
Use a different control (e.g. label or button) and just program the onclick event to change its appearance in an acceptable way.
Here is a version that I use in Hi-Dpi scenarios (including per monitor), with TableLayoutPanels
and everything configured in "automatic resize" mode (tested with .NET Framework 4.8):
public class AutoSizeCheckBox : CheckBox
{
public AutoSizeCheckBox()
{
Anchor = AnchorStyles.None;
// uncomment for flat style
// FlatStyle = FlatStyle.Flat;
}
public override bool AutoSize { get => false; set => base.AutoSize = false; }
protected override void OnPaint(PaintEventArgs e)
{
OnPaintBackground(e); // we don't use base, so we must redraw focus ourselves
var h = ClientSize.Height;
var w = ClientSize.Width;
var state = Checked ? ButtonState.Checked : ButtonState.Normal;
if (FlatStyle == FlatStyle.Flat)
{
state |= ButtonState.Flat;
}
const int focusSize = 1;
h -= focusSize * 2;
var rc = new Rectangle(new Point((w - h) / 2, 0), new Size(h, h));
if (Focused)
{
ControlPaint.DrawFocusRectangle(e.Graphics, rc);
}
if (FlatStyle != FlatStyle.Flat)
{
rc.Inflate(-focusSize, -focusSize);
}
ControlPaint.DrawCheckBox(e.Graphics, rc, state);
}
}
精彩评论