views:

817

answers:

2

How can I open a second window directly over the first, and not slightly diagonally down-right or back at the default position? I'm just trying to make a few screens to be clicked through. Is there another way I should be doing this?

why doesnt CenterParent do it? What does CenterParent do then?

+1  A: 

Try setting the location of the new form to be equal to the first, existing form. Make sure that the second form's StartPosition property is set to 'Manual'. This assumes your forms are all the same sizes.

Example constructor of the 'floating' form:

// reference to the form underneath, as it might
// change location between creating the FloatingWindow, and showing
// FloatingWindow!
Form BeneathWindow;

public FloatingWindow(Form BeneathWindow)
{
   InitializeComponent();

   // save this for when we show the form
   this.BeneathWindow = BeneathWindow;
   StartPosition = FormStartPosition.Manual;

}

// OnLoad event handler
private void FloatingWindowLoad(object sender, EventArgs e)
{
   Location = BeneathWindow.Location;
}

If your forms are not the same sizes, then you'll likely want to center them. You can use CenterParent as others have suggested, or you can manually center them yourself, as I sometimes like to do:

Location = new Point((BeneathWindow.Width - Width)/2 , (BeneathWindow.Height - Height)/2 );

Either should work!

Charlie Salts
Right Code, wrong place. This belongs (only) in the Load event.
Henk Holterman
@ Henk: Right you are! It's possible that the window underneath might change location after the above window is constructed. Post edited to reflect this.
Charlie Salts
A: 

See properties:

  1. StartPosition, try set it to CenterParent

  2. Owner, try set it to ParentForm. Or Open Yours window using methods:

    //
    // Summary:
    //     Shows the form with the specified owner to the user.
    //
    // Parameters:
    //   owner:
    //     Any object that implements System.Windows.Forms.IWin32Window and represents
    //     the top-level window that will own this form.
    //
    // Exceptions:
    //   System.ArgumentException:
    //     The form specified in the owner parameter is the same as the form being shown.
    public void Show(IWin32Window owner);
    

Or

    //
    // Summary:
    //     Shows the form as a modal dialog box with the specified owner.
    //
    // Parameters:
    //   owner:
    //     Any object that implements System.Windows.Forms.IWin32Window that represents
    //     the top-level window that will own the modal dialog box.
    //
    // Returns:
    //     One of the System.Windows.Forms.DialogResult values.
    //
    // Exceptions:
    //   System.ArgumentException:
    //     The form specified in the owner parameter is the same as the form being shown.
    //
    //   System.InvalidOperationException:
    //     The form being shown is already visible.-or- The form being shown is disabled.-or-
    //     The form being shown is not a top-level window.-or- The form being shown
    //     as a dialog box is already a modal form.-or-The current process is not running
    //     in user interactive mode (for more information, see System.Windows.Forms.SystemInformation.UserInteractive).
    public DialogResult ShowDialog(IWin32Window owner);

Or You can do it programmatically:

public partial class ChildForm : Form
{
    public ChildForm(Form owner)
    {
        InitializeComponent();
        this.StartPosition = FormStartPosition.Manual;
        int x = owner.Location.X + owner.Width / 2 - this.Width / 2;
        int y = owner.Location.Y + owner.Height / 2 - this.Height / 2;
        this.DesktopLocation = new Point(x, y);
    }
}

Parent form:

public partial class ParentForm : Form
{
    public ParentForm()
    {
        InitializeComponent();
    }

    private void ButtonOpenClick(object sender, EventArgs e)
    {
        ChildForm form = new ChildForm(this);
        form.Show();
    }
}
mykhaylo