Tuesday, June 3, 2008

CenterParent for a non-Modal Form in C#

I'm using a non-modal form to display a progress/busy/working dialog while a form is busy doing something. Unfortunately, the StartPosition=CenterParent, which works fine via Form.ShowDialog(), doesn't seem to work when a form is display via Form.Show(). I get around this by centering the form in the form_load method via:


private void frmBusy_Load(object sender, EventArgs e)
{
// centerparent not workin for a ".Show" form,
// so set position manually.
this.Location = new System.Drawing.Point(
this.Owner.Location.X +
(this.Owner.Width - this.Width) / 2,
this.Owner.Location.Y +
(this.Owner.Height - this.Height) / 2);
}

1 comment:

Sam said...

You could just use the Form.CenterToParent() method