Search code examples
c#winforms

Form.Location doesn't work


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?


Solution

  • What about using a p/Invoke to MoveWindow? The link provided includes a C# example.