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.

Dai
  • 1,510
  • 1
  • 11
  • 12
  • Size is a `struct` and you have to create a new `struct`. –  Mar 15 '11 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. – Hans Passant Mar 15 '11 at 15:39
  • Interesting: with Mono I get a much more helpful [error CS1612](http://stackoverflow.com/questions/1747654/cannot-modify-the-return-value-error-c): Cannot modify a value type return value of ‘System.Windows.Forms.Form.MinimumSize’. Consider storing the value in a temporary variable – Josh Lee Mar 15 '11 at 15:58

4 Answers4

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    system.drawing... why the hell should I need that just to set a damn window size??? Thanks – Dai Mar 15 '11 at 16:10
  • 8
    @Dai: Because Windows Forms need to be drawn. http://msdn.microsoft.com/en-us/library/system.drawing.aspx –  Mar 15 '11 at 17:19
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.

Den
  • 16,686
  • 4
  • 47
  • 87
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);
Stuart
  • 66,722
  • 7
  • 114
  • 165
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.

Cameron
  • 96,106
  • 25
  • 196
  • 225