Moving a label on a form at run time.

  • Thread starter Thread starter Tony
  • Start date Start date
T

Tony

Is there a way to allow a user to move (drag and drop) a text box or a label
on an open form? I want the user to be able to place a "note" on the form,
but once the box opens, I need them to be able to move it around on the page
similar to the way one can move a Post It note. I am running Excel 2003.

Tony
 
Hi Tony

Here on a Userform, dragging Label1 around:

Option Explicit ' *********** top pof module ************

Dim x0 As Long, y0 As Long

Private Sub Label1_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
x0 = X
y0 = Y
End Sub

Private Sub Label1_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
If Button = 1 Then
Label1.Top = Label1.Top + Y - y0
Label1.Left = Label1.Left + X - x0
End If
End Sub

HTH. Best wishes Harald
 
Harald -

Pretty cool, as always. I made some refinements to keep the label within the
confines of the userform:

'''''''''''''''''''''''''''
Option Explicit

Dim x0 As Long, y0 As Long

Private Sub Label1_MouseDown(ByVal Button As Integer, _

ByVal Shift As Integer, _

ByVal X As Single, ByVal Y As Single)


x0 = X

y0 = Y


End Sub

Private Sub Label1_MouseMove(ByVal Button As Integer, _

ByVal Shift As Integer, _

ByVal X As Single, ByVal Y As Single)


Dim x1 As Long, y1 As Long


If Button = 1 Then

x1 = Label1.Left + X - x0

If x1 < 0 Then x1 = 0

If x1 > Me.InsideWidth - Me.Label1.Width Then

x1 = Me.InsideWidth - Me.Label1.Width

End If


y1 = Label1.Top + Y - y0

If y1 < 0 Then y1 = 0

If y1 > Me.InsideHeight - Me.Label1.Height Then

y1 = Me.InsideHeight - Me.Label1.Height

End If


Label1.Left = x1

Label1.Top = y1

End If


End Sub

'''''''''''''''''''''''''''

- Jon
 
Back
Top