Create a new placemark in a custom shape that can be dragged / removed
I am using the below code to allow the user to drag the shortcut.
The code works fine - but I'm looking for a way to 1) simplify the code when working with multiple labels and 2) give the user the ability to create a new label that has the same properties as drag / drop.
In a way, the code is specific to specific labels ie Label1 etc., I have to copy the code over and over again to reference all the labels I want (50+)
So, is there a way to make my code automatically work for all shortcuts, both existing and newly created?
Private x_offset%, y_offset%
Private Sub Label1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
If Button = XlMouseButton.xlPrimaryButton Then
x_offset = X
y_offset = Y
End If
End Sub
Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
If Button = XlMouseButton.xlPrimaryButton Then
Label1.Left = Label1.Left + X - x_offset
Label1.Top = Label1.Top + Y - y_offset
End If
End Sub
thank
source to share
A bit late in response, but here's how it's done. The idea is that you need to create a class module that can handle events for Label
. Once you have a class to handle an event, you need to hook up a new / existing Labels
one to go through the class. This is usually done by creating Collection
one that contains all of your class objects. Other than that, you just need to create a class object for each label (new or existing). The following snippets are required:
- UserForm1 with its own code
- LabelHolder class class
The LabelHolder class contains the code for a perfect "Label Holder". This is a simple class that contains a reference to MSForms.Label
and handles each event. Note that I called the object Label1
so that I can lazily copy the code. It has Label1
nothing to do with Label1
on UserForm
; they have different areas and are independent.
'class module code
Public WithEvents Label1 As MSForms.Label
Private x_offset%, y_offset%
Private Sub Label1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
If Button = XlMouseButton.xlPrimaryButton Then
x_offset = X
y_offset = Y
End If
End Sub
Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
If Button = XlMouseButton.xlPrimaryButton Then
Label1.Left = Label1.Left + X - x_offset
Label1.Top = Label1.Top + Y - y_offset
End If
End Sub
The UserForm1 code behind shows an event for the button, which creates a new one Label
and adds it to Collection
. It also stores Collection
, which ensures that class objects are globally scoped and not garbage collected sooner. There is also an event Initialize
that shows how to add an existing one Label
to a fold.
'UserForm1 code behind
Dim labels As Collection
Private Sub CommandButton1_Click()
If labels Is Nothing Then
Set labels = New Collection
End If
Dim lbl As MSForms.Label
Set lbl = Frame1.Controls.Add("Forms.Label.1")
lbl.Caption = "testing"
Dim holder As New LabelHolder
Set holder.Label1 = lbl
labels.Add holder
End Sub
Private Sub UserForm_Initialize()
If labels Is Nothing Then
Set labels = New Collection
End If
Dim holder As New LabelHolder
Set holder.Label1 = Label1
labels.Add holder
End Sub
Finally, here is an image UserForm1
that has default names for all controls.
The same form after clicking the button and dragging:
All this code shows how to hook up a class module to dynamically created and original components in a custom form. He doesn't cover how to create a new one Label
using drag and drop, but it is possible. You put this code in a class module and make sure you have enough userform references to access the properties you need there.
source to share
I was not able to solve this problem - instead, I decided that several shortcuts are set visible = false and therefore hidden. Then I have a button that returns them and the mounsdown code is already set up. Seems much easier! However, if someone sees this and can answer it, I would be very interested!
source to share
check out this question: Use string variable to set object variable in VBA? (Excel 2013)
maybe you can skip all the button names and assign them to an object and then move the object.
Let me know if it helped you. If you edit your question and provide a screenshot of your labels, please.
considers
source to share