简体   繁体   English

在C#.NET中设置最小窗口大小

[英]Set minimum window size in C# .NET

I am having trouble setting the minimum size of my window in a C# application I am working on. 我在我正在处理的C#应用​​程序中设置窗口的最小大小时遇到​​问题。 I have tried this code in the form's constructor: 我在表单的构造函数中尝试了这段代码:

this.MinimumSize.Width = 800;
this.MinimumSize.Height = 600;

But the compiler says: 但是编译器说:

Cannot modify the return value of 'System.Windows.Forms.Control.MinimumSize' because it is not a variable 无法修改'System.Windows.Forms.Control.MinimumSize'的返回值,因为它不是变量

Can anybody shed some light on this issue for me? 任何人都可以为我解释这个问题吗?

EDIT: 编辑:

Using: 使用:

this.MinimumSize = new Size(800,600);

Gives: 得到:

 error CS0118: 'System.Windows.Forms.Form.Size' is a 'property' but is used like a 'type'

Sorry I forgot to mention that I had already tried that. 对不起,我忘了提到我已经尝试过了。 Also forgot to mention that I am not using Visual Studio. 还忘了提到我没有使用Visual Studio。

Since Size is a struct, you can't do that. 由于Size是一个结构,你不能这样做。
Instead, you need to assign a new Size value to the property, like this: 相反,您需要为属性分配一个新的Size值,如下所示:

this.MinimumSize = new Size(800, 600);

EDIT Your compiler is wrong; 编辑您的编译器是错误的; it's confusing the Size class with the Control.Size property. 它使Size类与Control.Size属性混淆。

To work around this unjust error, you need to qualify the type with a namespace: 要解决此不公正的错误,您需要使用命名空间限定类型:

this.MinimumSize = new System.Drawing.Size(800, 600);

Or you just forgot using System.Drawing . 或者你只是忘了using System.Drawing

You should use something like this: 你应该使用这样的东西:

this.MinimumSize = new Size(100, 100);

Width and Height are used to get the existing values instead of setting them. WidthHeight用于获取现有值,而不是设置它们。

If you go to the definition of MinimumSize , you will see this: 如果你去定义MinimumSize ,你会看到:

public override Size MinimumSize { get; set; }

Once again confirming that even when you decide to set the value for it, you have to pass an actual Size instance. 再次确认即使您决定为其设置值,也必须传递实际的Size实例。 Width and Height are properties strictly tied to the Size instance. WidthHeight是与Size实例严格关联的属性。

You need to assign directly to the MinimumSize property: 您需要直接分配给MinimumSize属性:

this.MinimumSize = new Size(800, 600);

Basically, the return value of the MinimumSize property is always a new struct object; 基本上, MinimumSize属性的返回值始终是一个新的 struct对象; the compiler doesn't let you assign to this temporary value (as stated in the error, it's not a variable). 编译器不允许您分配给此临时值(如错误中所述,它不是变量)。

This MSDN Social thread is most enlightening about the subject. 这个MSDN社交主题对这个主题最具启发性。

This is the compiler error: 这是编译器错误:

http://msdn.microsoft.com/en-us/library/wydkhw2c(VS.71).aspx http://msdn.microsoft.com/en-us/library/wydkhw2c(VS.71).aspx

The basic problem is that the MinimumSize member property returns a struct - which is a value type - and so is copied into a local temporary variable - and this prevents you from writing a value back to the underlying property. 基本问题是MinimumSize成员属性返回一个结构 - 它是一个值类型 - 因此被复制到本地临时变量 - 这会阻止您将值写回底层属性。

To get around this, you need to assign to MinimumSize itself: 要解决这个问题,您需要分配给MinimumSize本身:

this.MinimumSize = new Size(800, 600);

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

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