Attribute VB_Name = "mMove" ' Moving Controls ' - capability to drag any object ' code by Saul Greenberg ' THINGS TO NOTE: ' --------------- ' be sure that there is a timer on the form ' the timer must have the following code: 'Form: ' Private Sub movingTimer_Timer() ' mMove.ToNewPosition ' End Sub 'MouseDown Code: ' If Button = 1 Then mMove.Begin Me.ActiveControl, X, Y 'MouseUp Code: ' If Button = 1 Then mMove.EndIt 'MouseMove Code: ' If Button = 1 Then mMove.InProgress X, Y 'Form Load: ' mMove.Initialize movingTimer ' where MovingTimer is the name of the timer on the form Option Explicit 'the Current X and Y position Public curX As Single Public curY As Single 'the last recorded X and Y position Private lastX As Single Private lastY As Single 'the control being moved Private control As control 'Whether the control is in the middle of being moved Private Moving As Boolean Private timer As timer ' Being the move Public Sub Begin(control As control, X As Single, Y As Single) mMove.Moving = True Set mMove.control = control mMove.lastX = X mMove.lastY = Y End Sub Public Sub InProgress(X As Single, Y As Single) If Not mMove.Moving Then Exit Sub timer.Enabled = True mMove.curX = X - mMove.lastX mMove.curY = Y - mMove.lastY End Sub Public Sub EndIt() mMove.Moving = False timer.Enabled = False End Sub Public Sub ToNewPosition() Dim X As Single Dim Y As Single 'Set it to the new position. X = mMove.control.Left + mMove.curX Y = mMove.control.Top + mMove.curY 'Make sure we don't drag it off the screen... If X < 0 Then X = 0 ElseIf X > frmMain.ScaleWidth - mMove.control.Width Then X = frmMain.ScaleWidth - mMove.control.Width End If If mMove.control.Top + mMove.curY < 0 Then Y = 0 ElseIf Y > frmMain.ScaleHeight - mMove.control.Height Then Y = frmMain.ScaleHeight - mMove.control.Height End If mMove.control.Move X, Y End Sub Public Sub Initialize(tmr As timer) tmr.Enabled = False tmr.Interval = 30 Set timer = tmr End Sub