We help IT Professionals succeed at work.

c# center modal forms

pdering
pdering asked
on
my form launches 3 modal forms.  i'm trying to center the forms based on the parent form.  The first modal form centers based n the screen - no matter which form i launch first it's always the first one that centers based on screen and not on parent.  I have a workaround which is to first launch a blank form and then hide it, but does anyone know why this would occur?
Comment
Watch Question

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
How are you opening up the dialog?

Something like this should work:
    public partial class Form1 : Form
    {

        private void button1_Click(object sender, EventArgs e)
        {
            frmDialog dialog = new frmDialog();
            dialog.StartPosition = FormStartPosition.CenterParent;
            dialog.ShowDialog(this);
        }
    }

Open in new window

AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
You can have a look at the form properties - there should be one for the location of the form.  Select the option Center Parent and that should do what you want.

Author

Commented:
Thanks!  I was leaving off the "this".
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
>>Thanks!  I was leaving off the "this".

Odd.  My suggestion works with just
            frmDialog dialog = new frmDialog();
            dialog.ShowDialog();

and I have checked that from idle_mind (with a minor change)
            frmDialog dialog = new frmDialog();
            dialog.StartPosition = FormStartPosition.CenterParent;
            dialog.ShowDialog();   //the 'this' is removed

and that also centers to parent

Explore More ContentExplore courses, solutions, and other research materials related to this topic.