By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,894 Members | 2,002 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,894 IT Pros & Developers. It's quick & easy.

position of modal form

P: n/a
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
Oct 2 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"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
Oct 2 '06 #2

P: n/a
Thanks!

Govert
Oct 3 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.