4

Every time I re-open a form inside an MdiParent Container, the child form moves a small amount (~20 pixels) down and towards the right.

Is there a way to stop the form from doing this on form load, without stopping it from being moved freely within the parent?

Here is an example:

enter image description here

and then reload the child form:

enter image description here

1 Answer 1

3

Change the StartPosition property on the child form:

StartPosition = FormStartPosition.Manual

The default value is WindowsDefaultLocation, which causes each instance of the child form to drift down and to the right each time.


The following block of code from the Form class is called when the form is an MdiChild form, and the form is initially being displayed. (I trimmed out some unrelated parts.)

For all other StartPosition values beside Manual, there's some calculation going on regarding the location of the form, but it does nothing for StartPosition.Manual.

 // Adjusts the CreateParams to reflect the window bounds and start position.
 private void FillInCreateParamsStartPosition(CreateParams cp) { 
    switch ((FormStartPosition)formState[FormStateStartPos]) {
        case FormStartPosition.WindowsDefaultBounds: 
            cp.Width = NativeMethods.CW_USEDEFAULT;
            cp.Height = NativeMethods.CW_USEDEFAULT; 
            ...
        case FormStartPosition.WindowsDefaultLocation: 
        case FormStartPosition.CenterParent:
            ...
            cp.X = NativeMethods.CW_USEDEFAULT;
            cp.Y = NativeMethods.CW_USEDEFAULT;
            break;
        case FormStartPosition.CenterScreen: 
            if (IsMdiChild) {
                ...
                cp.X = Math.Max(clientRect.X,clientRect.X + (clientRect.Width - cp.Width)/2); 
                cp.Y = Math.Max(clientRect.Y,clientRect.Y + (clientRect.Height - cp.Height)/2);
            }
            else {
                ...
                if (WindowState != FormWindowState.Maximized) { 
                    cp.X = Math.Max(screenRect.X,screenRect.X + (screenRect.Width - cp.Width)/2); 
                    cp.Y = Math.Max(screenRect.Y,screenRect.Y + (screenRect.Height - cp.Height)/2);
                } 
            }
            break;
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.