在 C# 中在运行时更改表单大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13064350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change form size at runtime in C#
提问by bLAZ
How can I change window form size at runtime?
如何在运行时更改窗口窗体大小?
I saw examples, but every one requires Form.Size property. This property can be set like here: http://msdn.microsoft.com/en-us/library/25w4thew.aspx#Y456, but I've created my application form in a visual tool and the form is created like this:
我看到了示例,但每个示例都需要 Form.Size 属性。这个属性可以像这里一样设置:http: //msdn.microsoft.com/en-us/library/25w4thew.aspx#Y456,但我已经在一个可视化工具中创建了我的应用程序表单,表单是这样创建的:
static void Main()
{
Application.Run(new Form());
}
How do I set that Sizeproperty now and then change it by the Form.Heightand Form.Widthmethods?
我Size现在如何设置该属性,然后通过Form.Height和Form.Width方法更改它?
采纳答案by ChrisF
If you want to manipulate the form programmatically the simplest solution is to keep a reference to it:
如果要以编程方式操作表单,最简单的解决方案是保留对它的引用:
static Form myForm;
static void Main()
{
myForm = new Form();
Application.Run(myForm);
}
You can then use that to change the size (or what ever else you want to do) at run time. Though as Arrow points outyou can't set the Widthand Heightdirectly but have to set the Sizeproperty.
然后,您可以使用它在运行时更改大小(或您想做的任何其他事情)。尽管正如Arrow 指出的那样,您不能直接设置Width和Height,但必须设置Size属性。
回答by MrFox
In order to call this you will have to store a reference to your form and pass the reference to the run method. Then you can call this in an actionhandler.
为了调用它,您必须存储对表单的引用并将引用传递给 run 方法。然后你可以在 actionhandler 中调用它。
public partial class Form1 : Form
{
public void ChangeSize(int width, int height)
{
this.Size = new Size(width, height);
}
}
回答by Arrow
You cannot change the Width and Height properties of the Form as they are readonly. You can change the form's size like this:
您不能更改 Form 的 Width 和 Height 属性,因为它们是只读的。您可以像这样更改表单的大小:
button1_Click(object sender, EventArgs e)
{
// This will change the Form's Width and Height, respectively.
this.Size = new Size(420, 200);
}
回答by Penge
You can change the height of a form by doing the following where you want to change the size (substitute '10' for your size):
您可以通过执行以下要更改大小的操作来更改表单的高度(将“10”替换为您的大小):
this.Height = 10;
This can be done with the width as well:
这也可以通过宽度来完成:
this.Width = 10;
回答by GntS
Something like this works fine for me:
像这样的东西对我来说很好用:
public partial class Form1 : Form
{
Form mainFormHandler;
...
}
private void Form1_Load(object sender, EventArgs e){
mainFormHandler = Application.OpenForms[0];
//or instead use this one:
//mainFormHandler = Application.OpenForms["Form1"];
}
Then you can change the size as below:
然后你可以改变大小如下:
mainFormHandler.Width = 600;
mainFormHandler.Height= 400;
or
或者
mainFormHandler.Size = new Size(600, 400);
Another useful point is that if you want to change the size of mainFormfrom another Form, you can simply use Property to set the size.
另一个有用的一点是,如果您想mainForm从另一个更改 的大小Form,您可以简单地使用 Property 来设置大小。

