简体   繁体   English

如何在父窗口(开启器)中相对于控件的位置显示模态窗体

[英]How to display a Modal form in a position relative to the a control in the parent window (opener)

Well, I have a form that I open using: 好吧,我有一个打开的表单:

ShowDialog(this);

I try to change the position of the form using its Location property, but I don't understand what exactly is this position relative to? 我尝试使用其Location属性更改表单的Location ,但我不明白这个位置到底是什么? I want to open this form below a certain button. 我想在某个按钮下方打开此表单。 So how can this be done? 那么怎么做呢?

Thanks. 谢谢。

A Form will expect the co-ordinates relative to the screen's top-left corner. 表格将指向相对于屏幕左上角的坐标。 However, the location of a Control within a Form is relative to the top-left corner of the form . 然而,一个表格的控制的位置是相对于所述形式的左上角。

Use the Control's Location property to find its location, and then call PointToScreen on the Form object to turn it into screen co-ordinates. 使用Control的Location属性查找其位置,然后在Form对象上调用PointToScreen将其转换为屏幕坐标。 Then you can position the new form relative to that. 然后,您可以相对于该位置定位新表单。

For example: 例如:

var locationInForm = myControl.Location;
var locationOnScreen = mainForm.PointToScreen(locationInForm);

using (var model = new ModelForm())
{
    model.Location = new Point(locationOnScreen.X, locationOnScreen.Y + myControl.Height + 3);
    model.ShowDialog();
}

Actually the top-left corner of the client area of the form. 实际上是表单客户区的左上角。

我更喜欢这个:

myModalForm.Location = New Point(myControl.PointToScreen(Point.Empty).X + myControl.Width, myControl.PointToScreen(Point.Empty).Y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM