Hi, I have a simple Problem. I want to center a dialog on a panel of
an application. I do the following:
public class MyDialog : System.Windows.Forms.Form
{
private Panel m_ParentPanel;
public MyDialog(Panel panel)
{
this.m_ParentPanel = panel;
// some controls
}
protected override void OnLayout(LayoutEventArgs levent)
{
int width = this.m_ParentPanel.ClientRectangle.Width - 40;
if (width > 300) width = 300;
int height = 100;
Point screenLocation =
this.m_ParentPanel.PointToScreen(this.m_ParentPane l.Location);
int x = screenLocation.X +
(this.m_ParentPanel.ClientRectangle.Width-width)/2;
int y = screenLocation.Y +
(this.m_ParentPanel.ClientRectangle.Height-height)/2;
this.Bounds = new Rectangle(x, y, width, height);
base.OnLayout (levent);
}
}
The result is nearly what I want, but the dialog box is always shifted
a bit (20px ?) too far right? What is wrong with this way to center
the dialog box? Or better what is the correct way to do it? ;)
Thanks for any help!