我怎样才能使用的ShowDialog来显示它时,控制对话框的位置? [英] How can I control the location of a dialog when using ShowDialog to display it?

查看:826
本文介绍了我怎样才能使用的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.

二是从在系统托盘中的NotifyIcon的上下文菜单,但在这里它出现在屏幕的左上角。理想情况下,我想它出现居中的主屏幕上,或者接近系统托盘中。

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.


  • 我试过设置位置,但是这似乎重写时 dialog.ShowDialog()被调用。

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

我已经使用 dialog.ShowDialog(IWin32Window)超载尝试过,但似乎并不喜欢我路过作为窗口句柄。

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

我试过使用 dialog.Show()代替,但(这是我可以去错了)的设置位置不出现给予一致的结果。

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.

我甚至试过设置在 dialog.Parent 属性 - 这当然引发异常

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.

感谢您的答案 - 我怀疑这是显而易见的,但像往常一样是我让自己陷进入向下看走错路线。在更可气的是,我用从设计师的这个属性。

推荐答案

您可以设置<一个href=\"https://msdn.microsoft.com/en-us/library/system.windows.forms.form.startposition.aspx\"><$c$c>Form.StartPosition属性 FormStartPosition.Manual ,然后设置 Form.Location 属性所需的位置。当你调用的ShowDialog 的形式应在需要的位置显示出来。

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天全站免登陆