Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move a Winform dialog

Tags:

c#

winforms

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

like image 484
Mr. Boy Avatar asked Mar 20 '10 16:03

Mr. Boy


1 Answers

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();
}
like image 146
Adam Robinson Avatar answered Sep 28 '22 06:09

Adam Robinson