Reputation: 21
I have a userform that creates 20-30 labels upon initalization. At the same time a listbox is filled with the name of all these labels.
I want the user to select the label from the listbox which will 'activate' that label by defining it as ActiveTB. Then I will be able to run the following code to move that label around on the userform.
If i only had 3 labels: Label1, label2, label3 - I could just copy and paste this code below and have ActiveTB replaced with their names. However this is rediculous for 30 labels, and i also don't know what the names of these labels will be (there is a rename function in there).
I'm sure this is a simple fix but i can't find a way to call an object via a commonly activated name.
Private Sub ActiveTB_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = 1 Then
m_sngLeftPos = X
m_sngTopPos = Y
End If
End Sub
Private Sub ActiveTB_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim sngLeft As Single
Dim sngTop As Single
If Button = 1 Then
With ActiveTB
sngLeft = .Left + X - m_sngLeftPos
If sngLeft < SeriesImage.Left Then sngLeft = SeriesImage.Left
If (sngLeft + .Width) > (SeriesImage.Left + SeriesImage.Width) Then
sngLeft = SeriesImage.Left + SeriesImage.Width - .Width
End If
sngTop = .Top + Y - m_sngTopPos
If sngTop < SeriesImage.Top Then sngTop = SeriesImage.Top
If (sngTop + .Height) > (SeriesImage.Top + SeriesImage.Height) Then
sngTop = SeriesImage.Top + SeriesImage.Height - .Height
End If
.Move sngLeft, sngTop
End With
End If
End Sub
Upvotes: 0
Views: 120
Reputation: 166306
You can use a global variable declared using WithEvents
and set that to the label corresponding to the user's listbox selection.
Here's a simple example with just two labels - each one is "set" by a separate button:
Private WithEvents ActiveLabel As MSForms.Label '<< ######
Dim m_sngLeftPos, m_sngTopPos
'activate label1
Private Sub CommandButton1_Click()
Set ActiveLabel = Me.Label1
End Sub
'activate label2
Private Sub CommandButton2_Click()
Set ActiveLabel = Me.Label2
End Sub
Private Sub ActiveLabel_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = 1 Then
m_sngLeftPos = X
m_sngTopPos = Y
End If
End Sub
Private Sub ActiveLabel_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim sngLeft As Single
Dim sngTop As Single
If Button = 1 Then
With ActiveLabel
sngLeft = .Left + X - m_sngLeftPos
If sngLeft < SeriesImage.Left Then sngLeft = SeriesImage.Left
If (sngLeft + .Width) > (SeriesImage.Left + SeriesImage.Width) Then
sngLeft = SeriesImage.Left + SeriesImage.Width - .Width
End If
sngTop = .Top + Y - m_sngTopPos
If sngTop < SeriesImage.Top Then sngTop = SeriesImage.Top
If (sngTop + .Height) > (SeriesImage.Top + SeriesImage.Height) Then
sngTop = SeriesImage.Top + SeriesImage.Height - .Height
End If
.Move sngLeft, sngTop
End With
End If
End Sub
Upvotes: 1