tags:

views:

58

answers:

3

My main form launches another as a modal dialog, using .ShowDialog. I want to position this based on the mouse-position, but my attempts to call SetDesktopLocation are having no effect. Is this the right method?

Thanks

+1  A: 

You can try this in the onLoad() method of your new form:

this.Location = new Point(paramX, paramY);

where paramX and paramY are representing the mouse-position.

Amokrane
+3  A: 

Also, don't forget that modal dialogues halt execution on other forms until they are closed - so you'll need to do the positioning with the dialogue form itself, not on subsequent lines in the opener - as they won't be called until after the dialogue closes.

Also, check that the StartPosition of the form is set to Manual

Basiclife
+3  A: 

In order to set the position of a form programatically before it's visible, you need to set the StartPosition property to Manual, then set the Location property to the desired location.

using(Form toShow = new YourForm())
{
    toShow.StartPosition = FormStartPosition.Manual;
    toShow.Location = MousePosition;

    toShow.ShowDialog();
}
Adam Robinson
My form has no Position property, only Location. But the StartPosition was the main problem so I'll give you the green tick
John
@John: Sorry, wrote the answer in haste. `Location` is the correct property.
Adam Robinson