2010-07-09 36 views
0

我正在寫一個VB.Net WinForms應用程序,它在任何給定的表單上都有多個數據網格。在這種形式中,數據網格被加載到拆分容器中,拆分容器依次位於選項卡控件上。每個數據網格的加載方法都是線程化的,這樣就可以顯示一個動畫「加載」表單。我想將新的加載表單(小於網格)放置在正在加載的網格的頂部,並且最好位於網格的中心。WinForms:定位兩個控件的相對位置

在主窗體中查找網格位置的最簡單方法是什麼,以便我可以調整加載表單的位置?

回答

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) 

我希望這可以幫助別人!