一个控件在两个容器上来回切换而不改变位置(视觉),想了有一阵子了,贴代码.
Public Function GetAbsTop(ByVal ctl As Control) As Integer If ctl.Parent Is Nothing Then GetAbsTop = 0 : Exit Function If Not ctl.Parent.Equals(Me) Then '递归找到相对于FORM的Top GetAbsTop = ctl.Top + GetAbsTop(ctl.Parent) Else GetAbsTop = ctl.Top End If End Function Public Function GetAbsLeft(ByVal ctl As Control) As Integer If ctl.Parent Is Nothing Then GetAbsLeft = 0 : Exit Function If Not ctl.Parent.Equals(Me) Then '递归找到相对于FORM的Left GetAbsLeft = ctl.Left + GetAbsLeft(ctl.Parent) Else GetAbsLeft = ctl.Left End If End Function Public Sub MoveCtl(ByVal TarObj As Control,ByVal New_Parent As Control) If TarObj Is Nothing Or New_Parent Is Nothing Then Exit Sub 'TarObj.SuspendLayout() TarObj.Left = GetAbsLeft(TarObj) - GetAbsLeft(New_Parent) TarObj.Top = GetAbsTop(TarObj) - GetAbsTop(New_Parent) TarObj.Parent = New_Parent 'TarObj.ResumeLayout() End Sub
仅限于在同一窗体内.