position of modal form

  • Thread starter Govert J. Knopper
  • Start date
G

Govert J. Knopper

Here's my C# newbie question:

How do I set the position of a modal form?

This (correctly) positions top left of the form at the position of the
main form:
....
Form frmAbout = new About();
frmAbout.Show();
frmAbout.Left = this.Left;
frmAbout.Top = this.Top;
....

But this doesn't work
....
Form frmAbout = new About();
frmAbout.ShowDialog();
frmAbout.Left = this.Left;
frmAbout.Top = this.Top;
....

thanks, Govert
 
P

Peter Duniho

Govert J. Knopper said:
Here's my C# newbie question:

How do I set the position of a modal form?

This (correctly) positions top left of the form at the position of the
main form:
...
Form frmAbout = new About();
frmAbout.Show();
frmAbout.Left = this.Left;
frmAbout.Top = this.Top;
...

But this doesn't work
...
Form frmAbout = new About();
frmAbout.ShowDialog();
frmAbout.Left = this.Left;
frmAbout.Top = this.Top;

The latter fails to do what you want because the last two lines aren't
executed until you dismiss the modal form.

If you want to set the position of the modal form, you need to do it in code
that will execute while the form is being displayed. For example, in the
form's Load event handler. One solution might be as follows:

* In your modal form (dialog) class, add a private Point field:
private Point _ptPosition;

* Change the constructor to take a Point as a parameter and set the
private field:
public About(Point ptPosition)
{
// do other stuff (like InitializeComponent())
_ptPosition = ptPosition;
}

* Use the position in the Load event handler:
private void About_Load(object sender, EventArgs e)
{
Location = _ptPosition;
}

* And finally, in your caller take advantage of the changed constructor:
About frmAbout = new About(Location)

Pete
 

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

Top