简体   繁体   中英

How do I set a “preferred size” for a form?

I have some controls on a form, and I have them anchored to the left and right of the form to ensure they grow and shrink with the form. My form is also set to automatically grow and shrink in order to accommodate some labels that may get long strings. When those labels get their long strings removed, I want my form to go back to the width it was at before. Said controls which are anchored to the left and right on my form appear to be preventing this. Can I make it work as I intend?

Here's a way that doesn't require you to write any sizing specific code... but it assumes all your buttons are at the bottom.

Step 1. Set a minimum size for the dialog.

Step 2. Create a panel on the form.

Step 3. Move your buttons into the panel.

Step 4. Dock the panel to the bottom of the form.

Step 5. Ensure that the dialogs AutoSizeMode is set to ShrinkAndGrow

You can dock panels to any of the left,right,top and bottom borders with this technique. and have the dialog resize appropriately.

Can I suggest you not do this? I know you're trying to be helpful for your users, but having a window shrinking randomly (it will likely seem random to the user) is going to be confusing and perhaps frustrating. This is probably even more true with the new Windows 7 UI feature Aero Snap ie you dock the window to the left and then it resizes on you.

Consider the window size a user preference. The user has already told you how big they want the thing to be, respect it!

Edit: I should also add that there is a bug if you do go down this route. Consider the form being 1000 pixes wide and the user drags it to (-500, 100) to show it half on their monitor. If they hit a button that's visible (or worse, something happens in the background!) and the form resizes down to 450 pixels the window has just disappeared! The user sees it in the taskbar, but can't get to it unless they know some fancy alt+space shortcuts.

One suggestion would be to hook the OnPaint event. Your controls are built by this point and will have their initial location values set on the form. Capture everything you want into an object set private at the form level.

During an OnStringsRemoved event, read from your object and set everything back the way it was. Modifying a control that's anchored should trigger the form resize but it may be that you have to set everything manually. It may also be that modifying the form will shrink the controls and not the other way around.

An alternative suggestion would be to just hardcode your initial values and go back to them every time. I personally would opt for OnPaint. It'll help you handle things like Large Fonts bumping up control size or anything the framework does before the user sees it.

use System.Drawing.Graphics.MeasureString

    public SizeF MeasureString(string text, Font font, int width);

text=yourtext; font=fontofthelabel; width=maxwidthoflabel;

MeasureString in MSDN

you can google for more

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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