"Govert J. Knopper" <gk*@nodots.d.e.r.e.u.m.a.u.x.comwrote in message
news:45**********************@news.kpnplanet.nl...
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