I asked this question previously and thought I had it figured out but it still doesn't work. Form.Show() moves form position slightly
So I have a parent form that opens a bunch of children with show() and then when one is needed I use bringToFront() to display it. The problem is when show() is called the child form is aligned perfectly but when I use bringToFront it moves left and down 1 px which screws with my borders. I have set all the child forms startPosition properties to Manual before show()-ing them. I have set frm.location = new Point(x,y) when bringing to front. I've also tried explicity setting frm.location when show()-ing also. It still moves it left and down 1 px when I bringToFront(). Is there something with bringToFront() that doesn't let me change the location property of the form? Here is my code:
if (myNewForm != null)
{
myNewForm.MdiParent = this;
bool isFormOpen = false;
foreach (Form frm in Application.OpenForms)
{
if (frm.GetType() == myNewForm.GetType())
{
frm.WindowState = FormWindowState.Maximized;
frm.BringToFront();
frm.Location = new Point(-4, -30);
isFormOpen = true;
break;
}
}
if (!isFormOpen)
{
myNewForm.StartPosition = FormStartPosition.Manual;
myNewForm.Show();
}
}
EDIT: Ok so apparently Microsoft has a bug that lets StartPosition only work for ShowDialog() and not Show() but refuses to fix it: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=107589
But my application needs to keep all the different forms open and just bring them to the front when needed...so ShowDialog() could not be appropriately used in this instance correct? So what options do I have? Any?
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.
If you wish to set the form location, you have to set the form's WindowState
to Normal
(on any other setting, the location is set for you according to the rules of that setting, so your value is ignored), and its StartPosition
to Manual
.
frm.WindowState = FormWindowState.Normal;
frm.StartPosition = FormStartPosition.Manual;
frm.BringToFront();
frm.Location = new Point(-4, -30);
Have you tried:
this.Location
or
Form.ActiveForm.Location
?
I suppose the forms are moved by the code that handles the Show() (and BringToFront) requests, so you really cannot set the form's location - neither before nor after calling the method - because the form location will be updated after the code in your main window has executed (and left control back to the window message pump, in Win32 terms, basically).
I would use a subclass of Form for each of your forms and then add an explicit Point property that indicates the fixed position where that particular form is expected to be. Inside this class, override the OnShown virtual method (or perhaps the OnActivated method too) and simply update this.Location with the correct location.
That should force the forms to the correct position, even if some code inside windows forms changes it at some time.
What about using a p/Invoke to MoveWindow? The link provided includes a C# example.
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