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.
如何增加。net WinForm中的复选框的大小。我尝试了高度和宽度,但是没有增加盒子的尺寸。
6 个解决方案
#1
30
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.
复选框的大小是硬编码在Windows窗体中,你不能搞砸它。一种可能的解决方法是在现有的复选框之上绘制一个复选框。这并不是一个很好的解决方案,因为自动调整大小已经不能正常工作了,而且文本对齐也很混乱,但是它是可用的。
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);
}
}
#2
15
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
.
在属性窗口中有一个自动大小选项;如果您通过将它更改为False来关闭它,您将能够修改复选框的大小。
#3
5
C# version, from a forum.codecall.net topic :
c#版本,来自forum.codecall.net主题:
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);
}
}
#4
1
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.
如果有人需要VB。NET代码,我对它进行了修改。我在课堂上不傻。在构建项目之后,应该将控件添加到工具箱中。您可以将AutoSize设置为False,就像您可以设置任何其他控件一样。
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.
为了阐明AutoSize的功能,这个新控件的优点是可以将复选框的“box”部分放大。在正常的复选框中,不能更改框部分。
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.
我看到的这个新控件的唯一缺点是,当你调整它的大小时,如果保持对齐的话,盒子部分会超过文本;用TextAlign属性修复这个问题。
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
#5
0
Use a different control (e.g. label or button) and just program the onclick event to change its appearance in an acceptable way.
使用不同的控件(例如,标签或按钮),并只对onclick事件进行编程,以以可接受的方式改变其外观。
#6
-2
To be able to resize a checkbox you must set the resize property to false. I have tried:
要能够调整复选框的大小,必须将resize属性设置为false。我有尝试:
Public Sub New()
Me.Text = "Approved"
Me.TextAlign = ContentAlignment.MiddleLeft
Me.FlatStyle = Windows.Forms.FlatStyle.Flat
End Sub
and:
和:
Me.Height = 24
Me.Width = 200
Dim squareSide As Integer = 20
Dim rect As New Rectangle(New Point(0, 1), New Size(squareSide, squareSide))
ControlPaint.DrawCheckBox(
e.Graphics, rect, If(Me.Checked, ButtonState.Checked, ButtonState.Normal))
Me.FlatStyle = FlatStyle.Flat
End Sub
Not worked,
不工作,
also i tried to override the flat style, but its not override-able...any ideas?
我也试着重写平面样式,但它不是过度的。什么好主意吗?
#1
30
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.
复选框的大小是硬编码在Windows窗体中,你不能搞砸它。一种可能的解决方法是在现有的复选框之上绘制一个复选框。这并不是一个很好的解决方案,因为自动调整大小已经不能正常工作了,而且文本对齐也很混乱,但是它是可用的。
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);
}
}
#2
15
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
.
在属性窗口中有一个自动大小选项;如果您通过将它更改为False来关闭它,您将能够修改复选框的大小。
#3
5
C# version, from a forum.codecall.net topic :
c#版本,来自forum.codecall.net主题:
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);
}
}
#4
1
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.
如果有人需要VB。NET代码,我对它进行了修改。我在课堂上不傻。在构建项目之后,应该将控件添加到工具箱中。您可以将AutoSize设置为False,就像您可以设置任何其他控件一样。
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.
为了阐明AutoSize的功能,这个新控件的优点是可以将复选框的“box”部分放大。在正常的复选框中,不能更改框部分。
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.
我看到的这个新控件的唯一缺点是,当你调整它的大小时,如果保持对齐的话,盒子部分会超过文本;用TextAlign属性修复这个问题。
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
#5
0
Use a different control (e.g. label or button) and just program the onclick event to change its appearance in an acceptable way.
使用不同的控件(例如,标签或按钮),并只对onclick事件进行编程,以以可接受的方式改变其外观。
#6
-2
To be able to resize a checkbox you must set the resize property to false. I have tried:
要能够调整复选框的大小,必须将resize属性设置为false。我有尝试:
Public Sub New()
Me.Text = "Approved"
Me.TextAlign = ContentAlignment.MiddleLeft
Me.FlatStyle = Windows.Forms.FlatStyle.Flat
End Sub
and:
和:
Me.Height = 24
Me.Width = 200
Dim squareSide As Integer = 20
Dim rect As New Rectangle(New Point(0, 1), New Size(squareSide, squareSide))
ControlPaint.DrawCheckBox(
e.Graphics, rect, If(Me.Checked, ButtonState.Checked, ButtonState.Normal))
Me.FlatStyle = FlatStyle.Flat
End Sub
Not worked,
不工作,
also i tried to override the flat style, but its not override-able...any ideas?
我也试着重写平面样式,但它不是过度的。什么好主意吗?