C#-如何使用CheckBox调整表单大小 [英] C# - How to Resize my form size with CheckBox
问题描述
我有问题
我想增加表格尺寸
所以我创建了一个复选框来决定增大/减小大小
这是代码
I have a problem
I want to increase my form size
so i created a Checkbox to decide Increase/Decrease the size
here is the code
private void ExtandedColorBox(object sender, EventArgs e)
{
CheckBox CheckBox1 = new CheckBox(); //--> CheckBox1=name of the form
CheckBox1.Checked = true;
if (CheckBox1.Checked == true)
{
this.MaximumSize = new Size(300, 303); //--> this=Form
this.Size = new Size(300, 303);
}
if (CheckBox1.Checked == false)
{
this.MaximumSize = new Size(300, 280);
this.Size = new Size(300, 280);
}
}
当我检查时,表单会增加,但是当我再次检查时,表单并不会减少
[edit]已添加代码块-OriginalGriff [/edit]
因为Checkbox的状态永远不会更改为False
请帮忙:-)
when i check then the form getting increased but when i check again it''s does''nt getting decreased
[edit]Code block added - OriginalGriff[/edit]
because the status of the Checkbox never getting changed to False
Please help :-)
推荐答案
那是因为您继续创建新的复选框-对您没有任何帮助.
每次进入该方法时,您都会创建一个新复选框,并将其"Checked"属性设置为True.因此,当您测试它以查看应使用的表格大小时,总是会得到已检查"等于True,因此您总是将大小设置为300 x303.
而是删除
That''s because you keep on creating a new checkbox - which does nothing useful for you.
Every time into the method, you create a new checkbox, and give set it''s "Checked" property to True. So when you test it to see what you should be doing with teh form size, you always get "Checked" equals True, so you always set the size to 300 by 303.
Instead, delete the
CheckBox CheckBox1 = new CheckBox();
CheckBox1.Checked = true;
行,并将CheckBox1
替换为表单上实际CheckBox的名称(可能是checkBox1
,但您可能已对其进行了更改):
lines, and replace CheckBox1
with the name of the actual CheckBox on your form (probably checkBox1
, but you may have changed it):
private void ExtandedColorBox(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
this.MaximumSize = new Size(300, 303); //--> this=Form
this.Size = new Size(300, 303);
}
else
{
this.MaximumSize = new Size(300, 280);
this.Size = new Size(300, 280);
}
}
此外,您不需要显式检查是否为false-如果不是true
,则为false:我已更改您的代码以使其更整洁!
顺便说一句:您也不需要继续键入this.
-所有非静态类方法中都暗含了它:
In addition, you don''t need to check for false explicitly - if it isn''t true
then it is false: I have altered your code to be neater!
BTW: you don''t need to keep typing this.
either - it is implied in all non-static class methods:
private void ExtandedColorBox(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
MaximumSize = new Size(300, 303);
Size = new Size(300, 303);
}
else
{
MaximumSize = new Size(300, 280);
Size = new Size(300, 280);
}
}
这篇关于C#-如何使用CheckBox调整表单大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!