How to move a Winform dialog
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 me开发者_如何学Cthod?
Thanks
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();
}
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
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.
精彩评论