26

I have a non-modal child form which opens up from a parent form. I need to center the child form to its parent form. I have set property of child form to CenterParent and tried this:

Form2 f = new Form2();
f.Show(this);

but to no avail. This works with modal form, but not so with non-modal forms. Any simple solution, or need I go through all that mathematical calculation to fix its position to center?

JYelton
  • 35,664
  • 27
  • 132
  • 191
nawfal
  • 70,104
  • 56
  • 326
  • 368

4 Answers4

65

I'm afraid StartPosition.CenterParent is only good for modal dialogs (.ShowDialog).
You'll have to set the location manually as such:

Form f2 = new Form();
f2.StartPosition = FormStartPosition.Manual;
f2.Location = new Point(this.Location.X + (this.Width - f2.Width) / 2, this.Location.Y + (this.Height - f2.Height) / 2);
f2.Show(this);
Rotem
  • 21,452
  • 6
  • 62
  • 109
13

It seems really weird that Show(this) doesn't behave the same way as ShowDialog(this) w.r.t form centering. All I have to offer is Rotem's solution in a neat way to hide the hacky workaround.

Create an extension class:

public static class Extension
{
    public static Form CenterForm(this Form child, Form parent)
    {
        child.StartPosition = FormStartPosition.Manual;
        child.Location = new Point(parent.Location.X + (parent.Width - child.Width) / 2, parent.Location.Y + (parent.Height - child.Height) / 2);
        return child;
    }
}

Call it with minimal fuss:

var form = new Form();
form.CenterForm(this).Show();
Joe
  • 11,147
  • 7
  • 49
  • 60
4

For modeless form, this is the solution.

If you want to show a modeless dialog in the center of the parent form then you need to set child form's StartPosition to FormStartPosition.Manual.

form.StartPosition = FormStartPosition.Manual;

form.Location = new Point(parent.Location.X + (parent.Width - form.Width) / 2, parent.Location.Y + (parent.Height - form.Height) / 2);

form.Show(parent);

In .NET Framework 4.0 - If you set ControlBox property of child form to false and FormBorderStyle property to NotSizable like below:

form.ControlBox = false;
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;

then you will face the problem where part of child form doesn't show, if StartPosition is set to FormStartPosition.Manual.

To solve this you need to set child form's Localizable property to true.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
prmsmp
  • 41
  • 2
  • 2
    Thank you so much. Your comments about a form's Localizable property have fixed a problem which has been bugging me for ages, where on some customers' systems my pop-up forms would be slightly too small. It isn't very well documented by Microsoft. Do you understand why it works and what is happening when Localizable is false? or know any good links which explain it? Thanks again. – Erik Aug 11 '17 at 20:03
3
Form2 f = new Form2();
f.StartPosition = FormStartPosition.CenterParent;
f.Show(this);
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • do you know why does it work only this way? and not when setting the centerParent property from child form's constructor? – nawfal Dec 19 '11 at 19:50
  • 4
    Is this working for you? Not working for me. Where is this code being executed? – Rotem Dec 19 '11 at 19:55
  • @kol This does for me. But I suppose this works in .net 4 only.. I had faced the same trouble in .net 2.0 :) – nawfal Dec 19 '11 at 20:04
  • @Rotem This does for me. But I suppose this works in .net 4 only.. I had faced the same trouble in .net 2.0 :) – nawfal Dec 19 '11 at 20:05
  • @kol, yes strange.. for me, if i set the child form's property in the designer, it wont work. It works only if I set like the piece shown in the answer. You got to set the object's property from the parent form as shown! very strange – nawfal Dec 19 '11 at 20:10
  • I opened a separate question to clarify this: http://stackoverflow.com/questions/8567058/formstartposition-centerparent-does-not-work – kol Dec 19 '11 at 20:24
  • @kol strangely enough this code doesnt work for me anymore!! it worked, but not now!! how odd.. whats wrong! – nawfal Dec 19 '11 at 20:50
  • Hmm... have you changed something in the Property Editor? – kol Dec 19 '11 at 20:52
  • @kol i had changed. but i anyways reverted back.. it doesnt work anymore! strange.. – nawfal Dec 19 '11 at 21:10