如果您将要移动的控件放入集合中(例如,通过使用.Tag或.Nameproperty),只需遍历该集合(您的组)并将每个控件移动相同的值。
在标准模块中:
Public Sub MoveGroup(ByVal MovingGroup As Collection, _
ByVal AddLeft As Long, _
ByVal AddTop As Long)
Dim CtlToMove As Control
For Each CtlToMove In MovingGroup
CtlToMove.Move CtlToMove.Left + AddLeft, CtlToMove.Top + AddTop
Next
End Sub
Public Sub MoveByTagAsolute(ByRef CtlAbsolute As Control, _
ByVal MovingGroupTag As String, _
ByVal NewLeft As Long, _
ByVal NewTop As Long)
Dim CtlGroup As Collection
Set CtlGroup = New Collection
Dim CtlToMove As Control
For Each CtlToMove In CtlAbsolute.Parent.Controls
If CtlToMove.Tag Like MovingGroupTag Then
CtlGroup.Add CtlToMove
End If
Next
MoveGroup CtlGroup, NewLeft - CtlAbsolute.Left, NewTop - CtlAbsolute.Top
End Sub
Public Sub MoveByTagRelative(ByRef Frm As Form, _
ByVal MovingGroupTag As String, _
ByVal AddLeft As Long, _
ByVal AddTop As Long)
Dim CtlGroup As Collection
Set CtlGroup = New Collection
Dim CtlToMove As Control
For Each CtlToMove In Frm.Controls
If CtlToMove.Tag Like MovingGroupTag Then
CtlGroup.Add CtlToMove
End If
Next
MoveGroup CtlGroup, AddLeft, AddTop
End Sub
在表单模块中:
Private Sub CommandMoveByTagAsolute_Click()
MoveByTagAsolute Me.Controls("ControlAbsolutePos"), "move*", 2000, 3000
End Sub
Private Sub CommandMoveByTagRelative_Click()
MoveByTagRelative Me, "*", 1000, 2000
End Sub
ButtonCommandMoveByTagAsoluteclick-event 移动ControlAbsolutePos到新坐标,其余组相对于它移动。
ButtonCommandMoveByTagRelativeclick-event 相对移动整个组(“*”选择表单上的所有控件)。