VB.NET - how to center the panel in forms?

Asked By Prabhakaran on 16-Nov-11 11:46 PM
hi friends,


            how to center the panel control in forms when we maximize and restore maximize in different monitor?
i use the normal 
x = screen.width - form.width / 2
y = screen.height - form.height / 2 

panel.location = new point(x,y)

it some time works fine, some time not? is any other solution?
Jitendra Faye replied to Prabhakaran on 16-Nov-11 11:55 PM
Try this-]

Panel1.Location = New Point((Me.Width / 2) - (Panel1.Width / 2), (Me.Height / 2) - (Panel1.Height / 2))
aneesa replied to Prabhakaran on 17-Nov-11 12:04 AM
Set the Dock property of the panel as Fill
Code:

Panel1. Dock = System.Windows.Forms.DockStyle.Fill
Riley K replied to Prabhakaran on 17-Nov-11 12:12 AM

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.


// 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;

Try and let me know

Regards
Suchit shah replied to Prabhakaran on 17-Nov-11 12:27 AM

You could achieve this with the use of anchors. Or more precisely the non use of them.

Controls are anchored by default to the top left of the form which means when the form size will be changed, their distance from the top left side of the form will remain constant. If you change the control anchor to bottom left, then the control will keep the same distance from the bottom and left sides of the form when the form if resized.

Turning off the anchor in a direction will keep the control centred in that direction when resizing.

myControl.Left = (this.ClientSize.Width - myControl.Width) / 2 ;
myControl
.Top = (this.ClientSize.Height - myControl.Height) / 2;
Prabhakaran replied to Suchit shah on 17-Nov-11 07:20 AM
thnx its working fine.. 
Suchit shah replied to Prabhakaran on 17-Nov-11 07:37 AM
You are most welcome :)