简体   繁体   English

在运行时更改表单大小 C#

[英]Change form size at runtime in C#

How can I change window form size at runtime?如何在运行时更改 window 表单大小?

I saw examples, but every one requires Form.Size property.我看到了示例,但每个示例都需要 Form.Size 属性。 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:这个属性可以在这里设置: http://msdn.microsoft.com/en-us/library/25w4thew.aspx#Y456 ,但我已经在可视化工具中创建了我的应用程序表单,并且表单是这样创建的:

static void Main()
{
    Application.Run(new Form());
}

How do I set that Size property now and then change it by the Form.Height and Form.Width methods?我现在如何设置Size属性,然后通过Form.HeightForm.Width方法更改它?

You cannot change the Width and Height properties of the Form as they are readonly.您不能更改 Form 的 Width 和 Height 属性,因为它们是只读的。 You can change the form's size like this:您可以像这样更改表单的大小:

button1_Click(object sender, EventArgs e)
{
    // This will change the Form's Width and Height, respectively.
    this.Size = new Size(420, 200);
}

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 out you can't set the Width and Height directly but have to set the Size property.尽管正如Arrow 指出的那样,您不能直接设置WidthHeight ,而必须设置Size属性。

In order to call this you will have to store a reference to your form and pass the reference to the run method.为了调用它,您必须存储对表单的引用并将引用传递给 run 方法。 Then you can call this in an actionhandler.然后你可以在 actionhandler 中调用它。

public partial class Form1 : Form
{
    public void ChangeSize(int width, int height)
    {
        this.Size = new Size(width, height);
    }
}

Try this.ClientSize instead of this.Size:尝试使用 this.ClientSize 而不是 this.Size:

private void Form1_Load(object sender, EventArgs e)
{
   this.ClientSize = new Size(mywidth, myheight);
}

Worked for me.为我工作。

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;

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 mainForm from another Form , you can simply use Property to set the size.另一个有用的一点是,如果您想从另一个Form更改mainForm的大小,您可以简单地使用 Property 来设置大小。

As a complement to the answers given above;作为上述答案的补充; do not forget about Form MinimumSize Property, in case you require to create smaller Forms.如果您需要创建较小的表单,请不要忘记 Form MinimumSize 属性。

Example Bellow:示例波纹管:

private void SetDefaultWindowSize()
{
   int sizeW, sizeH;
   sizeW = 180;
   sizeH = 100;

   var size = new Size(sizeW, sizeH);

   Size = size;
   MinimumSize = size;
}

private void SetNewSize()
{
   Size = new Size(Width, 10);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM