Sie sind auf Seite 1von 3

Setting form's location when calling

Form.Show()
up I'm trying to set the location of a form when calling it by .Show(). The problem is
vote8do that because I'm using .Show instead of .ShowDialog the StartPosition value does
wn vote favorite
not work. I can't use the .Showdialog since I want the program to do work in the
1 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)

c# .net windows visual-studio-2012

shareimprove this question asked Jun 28 '13 at 16:12

Robin
5842622
I hope below link has a fix. stackoverflow.com/questions/21171764/ user2852008 Sep 18 at 16:49
add a comment
2 Answers
activeoldest votes

up StartPosition should work fine with Form.Show. Try:


vote15do ConnectingForm CF = new ConnectingForm();
wn vote 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.
shareimprove this answer answered Jun 28 '13 at 16:14
Reed Copsey
402k398181136
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 1 '13 at 8: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 1 '13 at 8:33

Nor does switching place on the .location and .show Robin Jul 1 '13 at 8:34

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 th
main ones. Robin Jul 1 '13 at 8:41
add a comment

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


vote3down
vote using (ConnectingForm CF = new ConnectingForm())
{
accept ed

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;
}

Das könnte Ihnen auch gefallen