tags:

views:

226

answers:

4

I asked this question previously and thought I had it figured out but it still doesn't work. http://stackoverflow.com/questions/1214014/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?

A: 

Have you tried showing the form, and then adjusting the Location?

Edit: Or, have you tried adjusting the Left and Top properties?

Joe
Yes I have tried this. And its the BringToFront()-ing that is the problem and not the Show()-ing. But I have also tried bringing to front and then adjusting the location...still nothing.
novacara
Yes same problem. The problem seems to be that first of all Show() does not allow StartPosition to be set like it is supposed to so no re-locating. Secondly, when maximizing the child it moves it down 1px and right 1px although I don't understand why because it is maximized initially yet located where its supposed to be. If I don't maximize then the mdi parent gives scrollbars and I definitely don't want that. If I edit the form to be 1px left and up where it is now it shows incorrectly initially but correctly when I bringtofront(). All I want is a workaround!
novacara
A: 

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.

Lck
+1  A: 

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

Joe
Works perfectly!! Thanks so much!!
novacara
Glad it worked!
Joe
As a side question...Does anyone know how using Windows API calls works with Visual Studio? Do the dlls get included with the finished product or can the program only be used on a Windows operating system?
novacara
Should probably be asked as a separate question, but if you use p/invoke, it will only work on Windows.
Joe
+1  A: 

Have you tried:

this.Location

or

Form.ActiveForm.Location ?

baeltazor