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

查看:27
本文介绍了使用 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.

  • 我已经尝试设置 Location,但是当调用 dialog.ShowDialog() 时,这似乎被覆盖了.

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

我尝试过使用 dialog.ShowDialog(IWin32Window) 重载,但这似乎不像我将 null 作为窗口句柄传递.

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.

感谢您的回答 - 我怀疑这很明显,但像往常一样,我让自己陷入了寻找错误路线的困境.更烦人的是我也用过设计师的这个属性.

推荐答案

您可以设置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天全站免登陆