Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

controls resize with form resize 1

Status
Not open for further replies.

scorpion1974

Programmer
Mar 21, 2004
7
US
I am developing a Windows Control Library application in C#.I have several controls on my form and I want all the controls to resize when my form gets resize.Does anybody know how to do that?
Thank you.
 
A form is a container for the controls.
When the user resizes the form the controls are not moved or resized automatically, so you should determine the location and the size for each control programmatically.
Each control has the Location property to tell its coordinates of the upper-left corner of the control relative to the upper-left corner of its container.
Each control has also Left, Right and Size property which can be modified at run time.
For your custom controls there is Control.Layout event which is raised in response to a Resize event of this control, but also in response to other changes that affect the layout of the control.
Example:
When the user resizes the Form, the following handler will make it square always:
Code:
private void CMDForm_Resize(object sender, System.EventArgs e)
{
   Control control = (Control)sender;
        
   // Force the Form to square (Height = Width).
   if(control.Size.Height != control.Size.Width)
   {
      control.Size = new Size(control.Size.Width, control.Size.Width);
   }
}
The next Layout event handler keeps the window form centered on the screen when the user resizes it:
Code:
private void CMDForm_Layout(object sender, System.Windows.Forms.LayoutEventArgs e)
{
   // Force the Form in the center of the screen
this.SetBounds((Screen.GetBounds(this).Width/2) - (this.Width/2),(Screen.GetBounds(this).Height/2) - (this.Height/2), this.Width, this.Height, BoundsSpecified.Location);    // BoundsSpecified.All

}
-obislavu-
 
Or you can use the "Anchor" property of the control, which is much more efficient. Please consult the .NET framework documentation for details.

j
 
Wow, thanks for the pointer to the Anchor property. I'd never looked at it before. Have a star!

I was using a routine to calculate spacing between five buttons and make sure they were spread out properly, plus to resize a datagrid so that it fits within the resized form. Simply setting the anchor properties of each control does all of this for you; brilliant.

Some day there will be nothing for us programmers to do at all!

Regards

Nelviticus
 
I use the Scale method of the form, which inherits Control.

Scales the control and any child controls to the specified ratio.

[C#] public void Scale(float);

Scales the control and any child controls by the specified horizontal and vertical ratios.

[C#] public void Scale(float, float);


Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
JohnYingling - depending on how you're doing things you may not need to write any code at all.

If you have, for instance, a datagrid in the middle of your form and a button in the bottom right, then in the form designer you can set the Anchor property of the datagrid to 'Top, Bottom, Left, Right' and the Anchor property of the button to 'Bottom, Right'. When you re-size the form the datagrid will stretch or shrink both vertically and horizontally with the form and the button will stay the same size but always be in the bottom right corner.

It's really very clever and very simple and doesn't require any coding at all, just setting properties in the form designer.

Nelviticus
 
I know all about the Anchor property but it does not solve the problem of a user having a diffrent resolution than my design. In that case, I want all controls resized and re-positioned, not just the ones which are anchored on four sides.

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top