我在MSDN上看过Control.PointToClient
或Control.PointToScreen
。
但是,如果我有这个控件层次结构,当TextBox
点击时,如何将另一个表单显示为与Button
相同的位置?
+- Form
+--- Panel
+---- Panel
+------ TextBox
+------ Button
答案 0 :(得分:1)
将PointToScreen
与空点(0,0)一起使用以获取控件相对于屏幕的位置,然后只显示该表单(确保StartPosition
为Manual
):
Point controlPosition = myTextBox.PointToScreen(Point.Empty);
MyForm newForm = new MyForm();
newForm.Location = controlPosition;
newForm.Show(this);
答案 1 :(得分:-1)
如果将新表单显示为showdialog,则需要使用:
newForm.StartPosition = FormStartPosition.Manual;