简体   繁体   English

调整C#Windows窗体的大小

[英]Resizing C# Windows Form

Not sure if this is the standard way of creating a form and opening, but the code the below does display the form properly. 不知道这是否是创建表单和打开表单的标准方法,但是下面的代码确实正确显示了表单。 My problem is that I can't programatically change it's size. 我的问题是我无法以编程方式更改其大小。 I'm guessing it has to do with the scope, "main" creates the form object, but I'd like to be resize it in the scope where it actually gets initialized (instead of in the [Design] tab of MS Studio) but I can't find the object/handle to it! 我猜想它与范围有关,“ main”创建了表单对象,但是我想在它实际初始化的范围内(而不是在MS Studio的[Design]选项卡中)调整它的大小。但是我找不到它的对象/句柄!

main.cs:
class MainProgram
{
    static void Main(string[] args)
    {
        // Create Form Object and Open Gui for user
        MainForm newFrm = new MainForm();   // Mainform is type Systems.Windows.Forms.Form
        Application.Run(newFrm); 
    }
}

formFile.cs:
public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
        //TODO: How to Resize Form from here?
        //this.Size.Height = 100; // ERROR!
    }
}

You could change the Size with this code 您可以使用此代码更改大小

  this.Size = new System.Drawing.Size(this.Size.Width, 100); 

Size is a Struct not a Class. 大小是结构而不是类。 This means that it is a Value type. 这意味着它是一个值类型。 When you try to change one of its properties the rules of the language force the compiler to create a copy of the original structure and you change the property of the copy not of the original structure. 当您尝试更改其属性之一时,语言规则会强制编译器创建原始结构的副本,并且您更改副本的属性而不是原始结构。

More details at MSDN 在MSDN上的更多详细信息

Choosing between Class and Struct 在类和结构之间选择

The way you're changing form size it's wrong. 您更改表单大小的方式是错误的。 You should use System.Drawing.Size 您应该使用System.Drawing.Size

using System.Drawing;

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
        this.Size = new Size(this.Size.Width, 100); 
    }
} 

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

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