使用ShowDialog显示时,如何控制对话框的位置? [英] How can I control the location of a dialog when using ShowDialog to display it?

查看:122
本文介绍了使用ShowDialog显示时,如何控制对话框的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常微不足道的问题,但我似乎找不到解决方法。这是令我烦恼的,因为我觉得我应该知道这个答案,但是我正在寻找错误的术语,或者查看错误的方法和属性。



我有一个从两个地方调用的配置对话框。



第一个是从正常工作的表单上的按钮,正如您所期望的那样。



第二个是系统托盘中的notifyIcon上的一个上下文菜单,但是它出现在屏幕的左上角。理想情况下,我希望它以主屏幕为中心,或者可能靠近系统托盘。




  • 尝试设置位置,但是当 dialog.ShowDialog()被调用时,这似乎被覆盖。


  • 我尝试使用 dialog.ShowDialog(IWin32Window)重载,但似乎并不喜欢我通过 null 作为窗口句柄。


  • 我尝试使用对话框。(显示()而不是(这就是我可能会出错的地方)设置的位置似乎不能给出一致的结果。


  • 我甚至尝试设置 dialog.Parent 属性 - 这当然引起了一个例外。




我只是知道,当我(希望)看到一些答案时,我会意识到答案是显而易见的,但目前我完全陷入困境。感谢您的回答 - 就像我怀疑的那样,p>

这很明显,但像往常一样,我会陷入困惑的路线。更令人讨厌的事情是,我也从设计师那里使用了这个属性。

解决方案

你可以设置 Form.StartPosition 属性到 FormStartPosition.Manual 然后设置 Form.Location 属性到您所需的位置。当您拨打 ShowDialog 时,表单应显示在所需位置。

  MyForm frm = new MyForm(); 
frm.StartPosition = FormStartPosition.Manual;
frm.Location = new Point(10,10);
frm.ShowDialog();


This is a very trivial problem but I can't seem to find a way of solving it. It's annoying me because I feel I should know the answer to this, but I'm either searching for the wrong terms or looking at the wrong methods and properties.

I have a configuration dialog that's called from two places.

The first is from the button on the form which is working correctly - as you'd expect.

The second is from a context menu on the notifyIcon in the system tray, but here it appears at the top left of the screen. Ideally I'd like it to appear centered on the primary screen, or perhaps close to the system tray.

  • I've tried setting the Location, but this appears to be overridden when dialog.ShowDialog() is called.

  • I've tried using the dialog.ShowDialog(IWin32Window) overload, but that didn't seem to like me passing null as the window handle.

  • I've tried using dialog.Show() instead, but (and this is where I could be going wrong) setting the location doesn't appear to give consistent results.

  • I've even tried setting the dialog.Parent property - which of course raised an exception.

I just know that I'm going to realise that the answer is obvious when I (hopefully) see some answers, but at the moment I'm completely stuck.

Thanks for the answers - as I suspected it was obvious, but as usual I'd got myself stuck into looking down the wrong route. The even more annoying thing is that I've used this property from the designer too.

解决方案

You can set the Form.StartPosition property to FormStartPosition.Manual and then set the Form.Location property to your desired location. When you call ShowDialog the form should show up in the desired location.

MyForm frm = new MyForm();
frm.StartPosition = FormStartPosition.Manual;
frm.Location = new Point(10, 10);
frm.ShowDialog();

这篇关于使用ShowDialog显示时,如何控制对话框的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆