WinForms:找到两个控件的相对位置

时间:2010-07-09 15:13:42

标签: vb.net winforms controls

我正在编写一个VB.Net WinForms应用程序,它在任何给定的表单上都有多个数据网格。在一个这样的表格上,数据网格被加载到分离容器中,而分离容器又位于标签控件上。每个数据网格的加载方法都是线程化的,因此可以显示动画“加载”表单。我想将新的加载形式(小于网格)放置在正在加载的网格的顶部,并且优选地在其中心。

在主窗体中查找网格位置的最简单方法是什么,以便我可以调整加载表单位置?

1 个答案:

答案 0 :(得分:2)

解决。评论?其他解决方案?

遍历父控件,直到找到主窗体。将每个位置的点添加到上一个位置。

Private Function Get_Control_Location(ByVal control As Control) As Point

    If control.Name = "MainForm" Then
        Return control.Location
    End If

    Return control.Location + Get_Control_Location(control.Parent)

End Function

然后计算新加载的大小,使其以网格为中心。

 Dim x As Integer = (GridControl.Width / 2) - (PleaseWait.Width / 2)
 Dim y As Integer = (GridControl.Height / 2) - (PleaseWait.Height / 2)
 PleaseWait.Location = Get_Control_Location(GridControl) + New Point(x, y)

我希望这有助于其他人!