Positioning a Window

  • Thread starter Thread starter Matthew Ireland
  • Start date Start date
M

Matthew Ireland

I am creating a System.Windows.Forms.Form derived form in an OnClick()
method of a button. I would like to set the position of this new form on
the window, but I can't figure out what method to call or property to set to
override where the Operating System places the window. Any brillian ideas
our there?

Thanks
 
You mean like this?

This snippet should be placed in your new form. It will set the left and top
position of this new form 50 pixels
below and to the right of the parent form.

this.Left = this.Owner.Left + 50;
this.Top = this.Owner.Top + 50;

Hope you can use it.
 
For your own location:
using (Form f = new Form())
{
f.StartPosition = FormStartPosition.Manual;
f.Location = new Point(200, -20);
f.ShowDialog(); // or Show(), with or without an owner
}

To centre on the parent use StartPosition = CenterParent, lose the
Location line, and be sure to pass the parent form as the owner in
Show[Dialog](). In fact, do that anyway - it keeps things stacked
correctly ;-p

Marc
 
Exactly what was needed. I had found the Location property, but apparently
without StartPosition being set, it had no effect.

Many thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top