How to make a panel center of form, even if the size of the form is changed. Using c# windows application
+9
A:
Position the panel in the center of the form using the designer, and then clear the Anchor
property, so it is not anchored to any edge. This will keep it centered when the form resizes, without resizing the panel itself.
If, for some reason, you will need to position the panel in code (depending on things that happens during form load for instance) you can do something like this:
// code for initializing the panel and setting
// its size goes here
_thePanel.Location = new Point(
this.ClientSize.Width / 2 - _thePanel.Size.Width / 2,
this.ClientSize.Height / 2 - _thePanel.Size.Height / 2);
_thePanel.Anchor = AnchorStyles.None;
That should take care of most scenarios, I imagine.
Fredrik Mörk
2010-05-18 08:05:20
@Fredrik Mörk, My panel has lot of controls in it. This application will run in many systems where the resolution varies. So, in that case the position of panel is different for different systems.
Anuya
2010-05-18 08:18:22
@karthink: see updated answer.
Fredrik Mörk
2010-05-18 08:26:49