Solved Class Module to Drag/Drop Multiple Labels on Form (1 Viewer)

G37Sam

Registered User.
Local time
Tomorrow, 01:13
Joined
Apr 23, 2008
Messages
455
Hello All,

I have a form wherein I'd like to drag and drop multiple labels, I was able to do this for one label and figured there must be some sort of technique wherein I wouldn't have to re-write the code for every single label. A little research pointed me towards class modules and I ended up patching together the below code.

Now how do I "attach" the below class module to labels on my form to trigger the below events automatically? Am I making sense?

Code:
Option Explicit

Public WithEvents DragLabel As Form.Label
Dim MoveMe As Boolean
Dim InitX As Long
Dim InitY As Long
Dim DragX As Long
Dim DragY As Long

Private Sub DragLabel_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = acLeftButton And Shift = acCtrlMask Then
    MoveMe = True
    InitX = X
    InitY = Y
Else
    MoveMe = False
End If
End Sub

Private Sub DragLabel_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If MoveMe = True Then
    DragX = X
    DragY = Y
End If
End Sub
   
Private Sub DragLabel_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If MoveMe = True Then
    With DragLabel
        .Left = .Left + DragX - InitX
        .Top = .Top + DragY - InitY
    End With
    MoveMe = False
End If
End Sub
 

conception_native_0123

Well-known member
Local time
Today, 16:13
Joined
Mar 13, 2021
Messages
1,244
actually if you don't mind me asking, why write code to do this if it can be done without it? is patternization part of what you are trying to do? obviously the code doesn't show that, but it wouldn't have to.
 

G37Sam

Registered User.
Local time
Tomorrow, 01:13
Joined
Apr 23, 2008
Messages
455
The user wants to be able to move labels on the form whose positions are linked to a report
 

conception_native_0123

Well-known member
Local time
Today, 16:13
Joined
Mar 13, 2021
Messages
1,244
i see. well this is probably out of my wheelhouse because I have never gotten a request like that, but are you asking how to attach code to an object's event? class modules, unless I'm wrong, are not generally meant to be attached to objects that are already classes in themselves. the purpose of a coding class is to create objects that are not already created by the program by default. but it seems that your code is trying to relate the x-y grid and place movements to the labels that are being targeted. correct? so you want to capture a click event, right?

this might be out of my wheelhouse, but I replied because I've seen it attempted before. it seems like you need more code to get this done, if it is possible. withEvents looks like it is being used correctly. but does this thread give you an idea of how to complete the task?

 

theDBguy

I’m here to help
Staff member
Local time
Today, 14:13
Joined
Oct 29, 2018
Messages
16,610
The user wants to be able to move labels on the form whose positions are linked to a report
The way I've done this in the past was to store the label locations in a table and simply read them to set the locations when the report opens.

Obviously, it's not as sophisticated as what you're trying to do, but just thought I'd mention it.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 22:13
Joined
Jul 9, 2003
Messages
13,433
I don't think I've seen him on the forum today, but I'm pretty sure that the member here "MajP" might well be able to help you, he is very experienced with Class Modules.

I have very limited experience with class modules, but I do dabble with them from time to time. I did a series of videos on various methods of locking and unlocking controls on my website here:- https://www.niftyaccess.com/lock-unlock-controls/ ... And I think in the last video I explored the possibility of using a class module. This particular part of the video that might be relevant to your question, see YouTube link here:-

Lock/Unlock Controls 7 - Nifty Access​

 

NauticalGent

CopyPaster of the First Order
Local time
Today, 17:13
Joined
Apr 27, 2015
Messages
4,180
...he is very experienced with Class Modules.
That he is, I was waiting for him to weigh in!

Here is a link to a Class Module MarkK uploaded a few years back. Not sure it is exactly what you are looking for, but it might give you some ideas:
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:13
Joined
May 21, 2018
Messages
5,501
Code:
Option Compare Database
Option Explicit

Private WithEvents mDragLabel As Access.Label
Dim MoveMe As Boolean
Dim InitX As Long
Dim InitY As Long
Dim DragX As Long
Dim DragY As Long

Private Sub mDragLabel_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = acLeftButton And Shift = acCtrlMask Then
        MoveMe = True
        InitX = X
        InitY = Y
    Else
        MoveMe = False
    End If
End Sub


Private Sub mDragLabel_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If MoveMe = True Then
    DragX = X
    DragY = Y
End If
End Sub
  
Private Sub mDragLabel_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If MoveMe = True Then
    With DragLabel
        .Left = .Left + DragX - InitX
        .Top = .Top + DragY - InitY
    End With
    MoveMe = False
End If
End Sub
'--------------------------------------------------------------------- To add
Public Sub Initialize(TheLabel As Access.Label)
  TheLabel.OnMouseDown = "[Event Procedure]"
  TheLabel.OnMouseMove = "[Event Procedure]"
  TheLabel.OnMouseUp = "[Event Procedure]"
  Set Me.DragLabel = TheLabel
 
End Sub
Public Property Get DragLabel() As Access.Label
    Set DragLabel = mDragLabel
End Property

Public Property Set DragLabel(ByVal objNewValue As Access.Label)
    Set mDragLabel = objNewValue
End Property

Form code
Code:
Private dl1 As New DragLabel
Private dl2 As New DragLabel
Private dl3 As New DragLabel
Private dl4 As New DragLabel

Private Sub Form_Load()
  dl1.Initialize Me.Label2
  dl2.Initialize Me.Label3
  dl3.Initialize Me.Label4
  dl4.Initialize Me.Label5
End Sub
 

Attachments

  • DragLabel.accdb
    1 MB · Views: 131

conception_native_0123

Well-known member
Local time
Today, 16:13
Joined
Mar 13, 2021
Messages
1,244
The way I've done this in the past was to store the label locations in a table and simply read them to set the locations when the report opens.

you mean this request has been given more than once? =) obviously I had no clue what the user wanted to do. =( sounds pretty unique to me.
 

isladogs

CID VIP
Local time
Today, 22:13
Joined
Jan 14, 2017
Messages
15,417
@MajP
Just tried your example. Nothing at all happens when I tried to drag & drop labels.
Any idea what I might be doing wrong?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:13
Joined
May 21, 2018
Messages
5,501
did you hold down the shift key?
 

isladogs

CID VIP
Local time
Today, 22:13
Joined
Jan 14, 2017
Messages
15,417
No but that doesn't work anyway.
However, it does work when I hold down the Ctrl key (y)
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:13
Joined
May 21, 2018
Messages
5,501
Yes, the control key.
  • acShiftMask The bit mask for the Shift key.
  • acCtrlMask The bit mask for the Ctrl key.
  • acAltMask The bit mask for the Alt key.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 14:13
Joined
Aug 30, 2003
Messages
35,406
I had an app that made extensive use of this type of thing. The labels represented cabs, and the form had a map of the city as a background. I got started with the code from ChrisO's drag and drop:


Having said that, @MajP 's code looks simpler. Wish I'd seen it 15 years ago when I was writing the app!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:13
Joined
May 7, 2009
Messages
14,157
it was done many years ago.
 

Attachments

  • Splitter.accdb
    744 KB · Views: 146

G37Sam

Registered User.
Local time
Tomorrow, 01:13
Joined
Apr 23, 2008
Messages
455
Code:
Option Compare Database
Option Explicit

Private WithEvents mDragLabel As Access.Label
Dim MoveMe As Boolean
Dim InitX As Long
Dim InitY As Long
Dim DragX As Long
Dim DragY As Long

Private Sub mDragLabel_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = acLeftButton And Shift = acCtrlMask Then
        MoveMe = True
        InitX = X
        InitY = Y
    Else
        MoveMe = False
    End If
End Sub


Private Sub mDragLabel_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If MoveMe = True Then
    DragX = X
    DragY = Y
End If
End Sub

Private Sub mDragLabel_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If MoveMe = True Then
    With DragLabel
        .Left = .Left + DragX - InitX
        .Top = .Top + DragY - InitY
    End With
    MoveMe = False
End If
End Sub
'--------------------------------------------------------------------- To add
Public Sub Initialize(TheLabel As Access.Label)
  TheLabel.OnMouseDown = "[Event Procedure]"
  TheLabel.OnMouseMove = "[Event Procedure]"
  TheLabel.OnMouseUp = "[Event Procedure]"
  Set Me.DragLabel = TheLabel

End Sub
Public Property Get DragLabel() As Access.Label
    Set DragLabel = mDragLabel
End Property

Public Property Set DragLabel(ByVal objNewValue As Access.Label)
    Set mDragLabel = objNewValue
End Property

Form code
Code:
Private dl1 As New DragLabel
Private dl2 As New DragLabel
Private dl3 As New DragLabel
Private dl4 As New DragLabel

Private Sub Form_Load()
  dl1.Initialize Me.Label2
  dl2.Initialize Me.Label3
  dl3.Initialize Me.Label4
  dl4.Initialize Me.Label5
End Sub
This is exactly what I was missing - the initialize bit. Thanks a ton MajP.

I actually always wanted to practice Class Modules and figured this would be a good time to do so.

Follow up question if I may: triggering these 3 mouse events now calls the code in your class module bypassing any code in the form itself, correct? How does this impact other events (on_click etc..)? Must they all fall under the class module now or can they be coded in the main form?
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:13
Joined
May 7, 2009
Messages
14,157
you can always use the Original events of your Labels in the Form.
the Events on the Class are just "additional" to what is in your form.

the label's event on the form is called first (if you have any), then the event on the Class.
 

G37Sam

Registered User.
Local time
Tomorrow, 01:13
Joined
Apr 23, 2008
Messages
455
you mean this request has been given more than once? =) obviously I had no clue what the user wanted to do. =( sounds pretty unique to me.

I'm incorporating a Cheque Printer into my application. The user wanted a more use friendly interface to move fields (Beneficiary, Date, Amount, Amount in Words) depending on the cheque formatting from that bank.

My initial solution involved a traditional text box at first wherein the user just keyed in the distances from the top and left borders but the user wanted to drag/drop with their mouse and then use up down left right buttons to fine tune where everything sits.
 

conception_native_0123

Well-known member
Local time
Today, 16:13
Joined
Mar 13, 2021
Messages
1,244
My initial solution involved a traditional text box at first wherein the user just keyed in the distances from the top and left borders
yeah, that would not be ideal for putting together something user-friendly. that is not user-friendly. ha ha. =)

I'm glad a solution was posted for you. good luck with it.
 

Users who are viewing this thread

Top Bottom