views:

542

answers:

5

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?

+4  A: 

Try with TableLayoutPanel or flowlayoutpanel control.
http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.aspx
http://visualbasic.about.com/od/usingvbnet/a/tloflopnl.htm

Brij
An interesting idea, but i have verything layed out as i want it right now...
RCIX
A: 

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.

w0rd-driven
I'm going to downvote this answer because I think this method will cause more problems than it will solve. First, OnPaint is called many many times: how do you know which version to save? Is it just the min(previous, now)? What happens when the user explicitly resizes the form to be larger? Shouldn't the form respect that? Secondly, the program knows when it's setting the strings: just save the values then.
popester
I use IsFirstPaint to make sure the code is only called once when I hook OnPaint for control-related behavior. The point was really to introduce OnInitializeAfterPaint() in essence to get known good location and size values after things like DPI are applied. I incorrectly assumed I set all that up properly before charging through the rest of it. Thanks for pointing that out so I could clarify. It's still a crappy answer but I did want to put *strong* emphasis on the event lifecycle and why I chose OnPaint.
w0rd-driven
+7  A: 

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.

Jason D
I think i can rig something up with this suggestion, thanks!
RCIX
Awesome. I'm always glad to help.
Jason D
+4  A: 

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 i.e. 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.

popester
not to mention the effects of DPI scaling.
Jeremy Seghi
Just to be curious, what effects are you speaking of?
popester
FTR, i'm the only user, so i can take certain allowances that i might normally not take. I've turned off user-sizing of the form as well, since it will be acting as a sort of dialog.
RCIX
To avoid the mentioned bug, you just have to add some logic to check the window's boundaries against the screen rectangle and move the window to ensure an appropriate area of the window is still visible on the screen, such as part of its title bar.
Triynko
A: 

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

Behrooz
This doesn't answer the question.
popester