PDA

View Full Version : ">Dynamic Resizing of Form Elements Relative to Form Shape in C#


complete
04-19-2010, 07:27 AM
What is the magic that makes components cling to the edges of a form?

I had thought that one must use the resize event of the form and them force each element in the form to resize.

But then I saw some sample code which, even when I am editing the form, the elements seem to adhere to a percentage of the space they take up in the form rather than a set diminsion. In other words, when I am editing the form and resizing it, the panels and the parts inside the form bend their shape such that the edges remain a few pixels from the edges.

But in my own program I have not been able to find where I can duplicate this feature. When I run my program, this (normal looking form/window (http://i67.photobucket.com/albums/h292/Athono/microsoft/001.jpg)) goes to this (expanded window/form with the components staying put when I want them to resize (http://i67.photobucket.com/albums/h292/Athono/microsoft/002.jpg)) when I click on the bottom right corner of the window and drag the mouse to resize.

BrickInTheWall
04-19-2010, 07:42 AM
If I'm understanding your request correctly, it's the Anchor/Dock-property of a control that you are looking to work with. You can set the control's position with the Bottom, Top, Left, Right properties, and the set the Anchor/Dock property to specify how you want the control to behave when your window is resized.

Here is an excerpt from http://www.codeproject.com/KB/books/1861004982.aspx:

Anchor and Dock Properties

These two properties are especially useful when you are designing your form. Ensuring that a window doesn't become a mess to look at if the user decides to resize the window is far from trivial, and numerous lines of code have been written to achieve this. Many programs solve the problem by simply disallowing the window from being resized, which is clearly the easiest way around the problem, but not the best. The Anchor and Dock properties that have been introduced with .NET lets you solve this problem without writing a single line of code.

The Anchor property is used to to specify how the control behaves when a user resizes the window. You can specify if the control should resize itself, anchoring itself in proportion to its own edges, or stay the same size, anchoring its position relative to the window's edges.

The Dock property is related to the Anchor property. You can use it to specify that a control should dock to an edge of its container. If a user resizes the window, the control will continue to be docked to the edge of the window. If, for instance, you specify that a control should dock with the bottom of its container, the control will resize itself to always occupy the bottom part of the screen, no matter how the window is resized. The control will not be resized in the process; it simply stays docked to the edge of the window.

See the text box example later in this chapter for the exact use of the Anchor property.