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)
Remarks. This property enables you to set the starting position of the form when it is displayed at run time. The form's position can be specified manually by setting the Location property or use the default location specified by Windows.
Add the control by drawingSelect the control by clicking on it. In your form, drag-select a region. The control will be placed to fit the size of the region you selected.
Click the form to display its properties in the Properties window. Set the StartPosition property to CenterScreen. Changing the StartPosition property to CenterScreen directs Visual Basic to display the form in the center of the Windows desktop when you run the program.
Remarks. This enumeration is used by the StartPosition property of the Form class. It represents the different start positions of the form. The default start position is WindowsDefaultLocation .
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With