56

It's been a mess to show a DialogBox at the center of its parent form. Here is a method to show a dialog.

I am positioning its parent to center but not able to center the DialogBox

private void OpenForm(Object point, Object height, Object width)
{
    FormLoading frm = new FormLoading();
    Point temp = (Point)point;
    Point location = new Point(temp.X + (int)((int)width) / 2, 
                               temp.Y + (int)((int)height) / 2);
    frm.Location = location;
    frm.ShowDialog();
}

private void btnView_Click(object sender, EventArgs e)
{
    try
    {                    
        ThreadStart starter= delegate { OpenForm(currentScreenLocation, 
                                                 this.Height, this.Width); };
        Thread t = new Thread(starter);
        t.Start();
        ////// Some functionality here...
        t.Abort();
    }
    catch (Exception)
    {
    }
}
Ghasem
  • 14,455
  • 21
  • 138
  • 171
Tausif Khan
  • 2,228
  • 9
  • 40
  • 51
  • 2
    I would also be careful with multithreading. Control's properties (at least those that deal with the User interface) can only be changed from the thread on which they are created. I don't see a bigger picture here so I don't know why you create each form in it's own thread, but keep that in mind. – Kornelije Petak Jul 07 '11 at 07:07

5 Answers5

119

You might want to check the Form.StartPosition property.

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.startposition.aspx

something along the lines of:

private void OpenForm(Form parent)
{
    FormLoading frm = new FormLoading();
    frm.Parent = parent;
    frm.StartPosition = FormStartPosition.CenterParent;
    frm.ShowDialog();
}

This of course requires setting the form's parent.

Kornelije Petak
  • 9,412
  • 15
  • 68
  • 96
  • frm.StartPosition = FormStartPosition.Manual; frm.Location = location; I used it and worked for me :) – Tausif Khan Jul 07 '11 at 09:12
  • Setting it to FormStartPosition.CenterParent does the math for you. But I guess you would want to retain your code if you plan on doing anything more complex than centering. I'm glad you solved your problem. – Kornelije Petak Jul 07 '11 at 09:15
  • FormStartPosition.CenterParent did not work for me. I just want to center it. But your provided link was a key for me. Thanks for that – Tausif Khan Jul 07 '11 at 09:22
  • 23
    `frm.Parent = parent;` doesn't work, but removing that line of code still works properly.. the parent thing gives a Top-Level error which couldn't be fixed. `Top-level control cannot be added to a control` – SSpoke Jul 02 '13 at 23:53
  • rather than `frm.Parent = parent`, I use `frm.Parent = this` – Jim Lahman Feb 06 '14 at 14:26
  • 6
    If you get the 'Top-Level control cannot be added...' exception try setting the Owner instead: `frm.Owner = parent;` – Nils Lande Jun 03 '15 at 07:55
  • You can also pass the parent window to ShowDialog – Patrick Feb 07 '18 at 05:21
  • @Nils +1 for `frm.Owner` as this works within the child form class. – Mooseman Oct 24 '18 at 17:44
14
form1.StartPosition = FormStartPosition.CenterScreen;

See http://msdn.microsoft.com/en-us/library/system.windows.forms.form.startposition(v=vs.110).aspx

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
user2070102
  • 157
  • 1
  • 2
6

if you are making a custom MessageBox,you can simply put this:

CenterToParent();

in your custom MessageBox formload() method.

Mohsen K
  • 257
  • 4
  • 10
4

In addition, if you want to set up arbitrary location you can use this

FormLoading frm = new FormLoading();
Point location = new Point(300, 400);
frm.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
frm.Location = location;
frm.ShowDialog();
DuyLuc
  • 1,664
  • 2
  • 11
  • 9
3
NewForm.Show();

NewForm.Top = (this.Top + (this.Height / 2)) - NewForm.Height / 2;
NewForm.Left = (this.Left + (this.Width / 2)) - NewForm.Width / 2;
LarsTech
  • 80,625
  • 14
  • 153
  • 225
Billy Xd
  • 79
  • 5