3

I am creating a simple Windows form application using Visual C#.

The form is can re-sized by the user (by click and drag). But, I don't want the user to decrease the dimensions of the form beyond a certain limit. How can I set the limits?

Paul
  • 121
  • 1
  • 3
  • 11

3 Answers3

3

Go to your form in VS and press F4 to get to the properties of the form and look for the MinimumSize property:

enter image description here

To set the size from code use:

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

Note that this does not prevent minimising/maximising the form! If you want to prevent this, then set the MaximizeBox property to false.

keenthinker
  • 7,645
  • 2
  • 35
  • 45
  • In MSVS 2015, the default minimum width is too small, but when I put in a bigger number it reverts to the previous value as soon as I hit enter or tab to another property. Why is that? – Kevin S. Miller May 02 '17 at 18:57
  • @KevinMiller Are you changing only the width field, or the combined **width; height** field (if you put only one number into the combined field, it is not accepted)? What number are you putting in? Is your code under source control? – keenthinker May 02 '17 at 19:52
  • I expanded the Minimum Size property and changed the width value from 0 to 1229, but when I tab out it changes to 1159. I can make it smaller than 1159, but larger values revert. I am using GIT, but it changes immediately, not after a pull if I understand your point. The form has a TableLayoutPanel which contains a TabControl and a StatusStrip and rows and columns. I think the problem may be the interaction of the sizes of these elements inside elements but I haven't been able to figure it out. The form was developed by someone else several years ago. – Kevin S. Miller May 03 '17 at 19:00
  • 1
    @KevinMiller it is because of the TableLayoutPanel - it has a fixed (or max) size set and/or AutoSize property set to true and thus all children controls are truncated to this value. I would suggest to inspect the TableLayoutPanel and check all (layout)control hierarchies to identify the problem. – keenthinker May 04 '17 at 20:02
  • 1
    .@pasty - That was it. There was an empty element with a fixed size used to simulate align right on a label. I removed the element and set the container to alignRight. – Kevin S. Miller May 09 '17 at 20:08
0

In the WinForm properties there is minimum size property see here http://msdn.microsoft.com/en-us/library/system.windows.forms.form.minimumsize(v=vs.110).aspx

Shachaf.Gortler
  • 5,655
  • 14
  • 43
  • 71
0

To change it on run-time:

int width, height;
this.MinimumSize = new Size(width, height);
etaiso
  • 2,736
  • 3
  • 26
  • 38