如何创建具有相同位置的其他控件的弹出窗体

时间:2012-02-19 02:58:03

标签: c# winforms

我在MSDN上看过Control.PointToClientControl.PointToScreen

但是,如果我有这个控件层次结构,当TextBox点击时,如何将另一个表单显示为与Button相同的位置?

+- Form
+--- Panel
+---- Panel
+------ TextBox
+------ Button

2 个答案:

答案 0 :(得分:1)

PointToScreen与空点(0,0)一起使用以获取控件相对于屏幕的位置,然后只显示该表单(确保StartPositionManual ):

Point controlPosition = myTextBox.PointToScreen(Point.Empty);
MyForm newForm = new MyForm();
newForm.Location = controlPosition;
newForm.Show(this);

答案 1 :(得分:-1)

如果将新表单显示为showdialog,则需要使用:

newForm.StartPosition = FormStartPosition.Manual;