开发者

How to move a Winform dialog

开发者 https://www.devze.com 2022-12-23 19:41 出处:网络
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 ri

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消