33

I am having trouble setting the minimum size of my window in a C# application I am working on. 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

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.

3
  • Size is a struct and you have to create a new struct. Commented Mar 15, 2011 at 15:36
  • 1
    Consult your favorite C# language book about the difference between value and reference types. The Size type is a struct, a value type. Commented Mar 15, 2011 at 15:39
  • Interesting: with Mono I get a much more helpful error CS1612: Cannot modify a value type return value of ‘System.Windows.Forms.Form.MinimumSize’. Consider storing the value in a temporary variable Commented Mar 15, 2011 at 15:58

4 Answers 4

63

Since Size is a struct, you can't do that.
Instead, you need to assign a new Size value to the property, like this:

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

EDIT Your compiler is wrong; it's confusing the Size class with the Control.Size property.

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.

Sign up to request clarification or add additional context in comments.

2 Comments

system.drawing... why the hell should I need that just to set a damn window size??? Thanks
@Dai: Because Windows Forms need to be drawn. msdn.microsoft.com/en-us/library/system.drawing.aspx
6

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.

If you go to the definition of MinimumSize, you will see this:

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. Width and Height are properties strictly tied to the Size instance.

2 Comments

That's not actually true; the properties are writable.
Properties are writable for the Size instance (when you are passing it to MinimumSize).
4

This is the compiler error:

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.

To get around this, you need to assign to MinimumSize itself:

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

Comments

3

You need to assign directly to the MinimumSize property:

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

Basically, the return value of the MinimumSize property is always a new struct object; 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.