18

I'm trying to set the location of a form when calling it by .Show(). The problem is that because I'm using .Show instead of .ShowDialog the StartPosition value does not work. I can't use the .Showdialog since I want the program to do work in the background while showing the form.

When I'm creating the form I set it's location to a fixed value:

using (ConnectingForm CF = new ConnectingForm())
{
    CF.Show();
    CF.Location = new Point(this.ClientSize.Width / 2, this.ClientSize.Height / 2);
}

But when I run the Code the form places itself on different positions each time I start it.

Any solutions? (The location is never set anywhere else by my code)

Robin
  • 1,927
  • 3
  • 18
  • 27
  • I hope below link has a fix. http://stackoverflow.com/questions/21171764/how-to-position-the-opening-form-at-the-top-left-corner-in-c-sharp-windows-forms – user2852008 Sep 18 '16 at 16:49

2 Answers2

29

StartPosition should work fine with Form.Show. Try:

ConnectingForm CF = new ConnectingForm();
CF.StartPosition = FormStartPosition.CenterParent;
CF.Show(this);

If you want to manually place the form, as you've shown, that can be done as well, but still requires setting the StartPosition property to Manual:

ConnectingForm CF = new ConnectingForm();
CF.StartPosition = FormStartPosition.Manual;
CF.Location = new Point(this.ClientSize.Width / 2, this.ClientSize.Height / 2);
CF.Show();

On a side note, you shouldn't use a using statement with Form.Show. using will call Dispose on the form, which isn't desired, since the form's lifetime is longer than this block of code.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • The following code does not work (the new form is located in the top left corner): `using (ConnectingForm CF = new ConnectingForm()) { CF.StartPosition = FormStartPosition.CenterParent; CF.Show(this); }` And `this` is indeed the handle for the right parent. I use the `using` statement to be sure that the form is disposed correctly. There is more code inside the using block that I didn't post since it's not relevant to the form's position. – Robin Jul 01 '13 at 08:28
  • `using (ConnectingForm CF = new ConnectingForm()) { CF.StartPosition = FormStartPosition.Manual; CF.Show(this); CF.Location = new Point(this.ClientSize.Width / 2, this.ClientSize.Height / 2);` Does not work either. The forms location varies each time I start the application. – Robin Jul 01 '13 at 08:33
  • When I look at the form closer it shows that it is actually not the new form that changes position. It's the main form that changes startup position each time. However this is very odd since I've set the new forms location relative to the main ones. – Robin Jul 01 '13 at 08:41
8

With some help from other threads I found a working solution:

    using (ConnectingForm CF = new ConnectingForm())
    {
        CF.StartPosition = FormStartPosition.Manual;
        CF.Show(this);
        ......
    }

On the new form's load event:

    private void ConnectingForm_Load(object sender, EventArgs e)
    {
        this.Location = this.Owner.Location;
        this.Left += this.Owner.ClientSize.Width / 2 - this.Width / 2;
        this.Top += this.Owner.ClientSize.Height / 2 - this.Height / 2;
    }

(I'm no expert so please correct me if I'm wrong) Here is how I interpret the problem and the solution: The problem from the beginning was that the first form's (MainForm) Startup Position was set to Windows Default Location which varies when you start up the form. When I then called the new form (Connecting form), it's location was not relative to it's parent's location, but the location (0, 0) (top lef corner of the screen). So what I was seeing was the MainForms position changing, which made it look like the Connecting Form's position was moving. So the solution to this problem was basically to first set the new form's location to the Main Form's location. After that I was able to set the location to be the center of the MainForm.

TL;DR the new form's location was not relative to the parent form's location, but to a fixed position that I'm guessing is (0, 0)

I changed the MainForm's Startup Position to a fixed one for my own convenience. I also added an event to make sure that the new forms position always was the center of the MainForm.

    private void Location_Changed(object sender, EventArgs e)
    {
        this.Location = this.Owner.Location;
        this.Left += this.Owner.ClientSize.Width / 2 - this.Width / 2;
        this.Top += this.Owner.ClientSize.Height / 2 - this.Height / 2;
    }

    private void ConnectingForm_Load(object sender, EventArgs e)
    {
        this.Owner.LocationChanged += new EventHandler(this.Location_Changed);
        this.Location = this.Owner.Location;
        this.Left += this.Owner.ClientSize.Width / 2 - this.Width / 2;
        this.Top += this.Owner.ClientSize.Height / 2 - this.Height / 2;
    }

Hopefully this will help others with the same problem!

Robin
  • 1,927
  • 3
  • 18
  • 27