Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
code to resize controls such as buttons etc on different screen resolutions
Posted

You should look into using Dock and anchor properties of the controls, which will automatically resize controls whne the window is resized.Manage WinForms controls using Dock and Anchor properties[^] is a good place to start.

Good Luck
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Feb-12 4:13am    
This is a right thing to do, my 5.
--SA
If you are using straight winforms, then you can, but it is never very effective, and the more complex your form, the worse the effect gets. The problem is that while it is possible to make the controls themselves grow and shrink in response to changes in the form size just by playing with the Anchor property, this does not affect the text size. So if you enlarge a button, you end up with a button the size of your hand, with text the size of your fingernail.

You can get round this by handling the Resize event for each control, and modifying teh font appropriately, but it's a heck of a lot of hassle.

The anchor can work very well, provided you are careful about which controls you anchor, and to where - lookl at VS - it resizes nicely, without stretching controls at all.

The only other alternative is WPF, I'm afraid.
 
Share this answer
 
Your best bet to give a nice and correct form layout which does not need special code for resizing. To add to the answers by Wayne and Griff, I would say, that Dock is good and very important, but it also needs Padding, and Anchor is best avoided. First, Anchor is tends to flicker, but it also requires manual positioning (like centering) or hard-coded left/right (top/bottom) spaces, which is not so nice.

In a good design, you use many addition Panels in hierarchy and never absolute positioning and any controls at all. Also some "spacer" panels are useful in some situations. It's also very important to limit minimum size of the form.

Well designed layout resize itself correctly without processing of resize event.

Please see also my past answers to related questions:
Zom Out malfunctions when Screen resolution changes[^],
GUI Apperance - C#.Net[^].

In this one I've shown some simplest code sample: how to dock button so that it can adjust with the form[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900