Get label name into procedure. (1 Viewer)

Monardo

Registered User.
Local time
Tomorrow, 00:58
Joined
Mar 14, 2008
Messages
70
Hello

I have a form with about 100 labels and would like something to happen when I mouseover. So I have a following code:

Code:
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Label1.BackColor = vbBlue
End Sub

Private Sub Label2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Label2.BackColor = vbBlue
End Sub

...

Private Sub Label_n_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Label_n.BackColor = vbBlue
End Sub

etc...  100 times
What I would like is to do the following:

Code:
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Dim varLabelName as String 'or label, or control?
    varLabelName = CURRENT SUB Label Name 
    
    varLabelName.BackColor = vbBlue
End Sub
Which eventually could be made into function.

Is this possible, or maybe my approach is wrong?
 

AOB

Registered User.
Local time
Today, 22:58
Joined
Sep 26, 2012
Messages
615
Can you make your function thus :

Code:
Private Sub ChangeColour(lbl As Label)
    lbl.BackColor = vbBlue
End Sub

And then just call the function on the MouseMove event of each required label?

Code:
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Call ChangeColour(Me.Label1)
End Sub

Haven't tested; you may need to define the passed argument as a Control rather than a Label (I've run into problems using the Label object in the past)
 

pr2-eugin

Super Moderator
Local time
Today, 22:58
Joined
Nov 30, 2011
Messages
8,494
+ 1 for passing as Control rather than Label.
 

Monardo

Registered User.
Local time
Tomorrow, 00:58
Joined
Mar 14, 2008
Messages
70
AOB
pr2-eugin
Thank you for reply. Indeed what you propose is correct and that was my intended next step.

What I was hoping for is that instead of changing 100 labels, to make one and copy paste the same code something like:

Code:
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)    
    Dim varLabelName as String     
    varLabelName = CURRENT SUB's Label Name   

    Call ChangeColour(varLabelName) 
End Sub
I can't believe one cannot reference to current control name.
 

AOB

Registered User.
Local time
Today, 22:58
Joined
Sep 26, 2012
Messages
615
You mean a generic MouseMove event for all labels within a form, which somehow detects the label in question and uses that as the argument to pass to the generic function?

I don't believe that is possible - the MouseMove event is specific to each control therefore you have to call it independently from each control.

Unless you had a MouseMove event for the form in general, which used the X and Y variables to determine where within the form the mouse is currently located, in order to identify if/which label the mouse is currently over (most likely requiring a twips-to-cm conversion somewhere and looping through all label controls to get their properties) and then call the function if there is an overlap with a known label's location?

Even if that were possible (which I strongly doubt, as when the mouse moves over a label, that is a separate event to moving over the detail / header / footer sections of the form), I wouldn't think it a very good idea as you would have this MouseMove event being triggered almost constantly with a very elaborate and time-consuming loop each time.

In short - I think you need to have an independent trigger for each label. But open to correction on that...
 

AOB

Registered User.
Local time
Today, 22:58
Joined
Sep 26, 2012
Messages
615
Sorry, just re-read your last post - you want the same code to be used for each event without having to manually change the name of the calling control each time.

Try this thread - haven't tried it myself but may help...
 

Monardo

Registered User.
Local time
Tomorrow, 00:58
Joined
Mar 14, 2008
Messages
70
Sorry, just re-read your last post - you want the same code to be used for each event without having to manually change the name of the calling control each time.

Try this thread - haven't tried it myself but may help...

Thanks.

Actually that was very first thread I tried before posting here, but it works only when control can get and has focus.
 

Cronk

Registered User.
Local time
Tomorrow, 07:58
Joined
Jul 4, 2013
Messages
2,772
I had a similar but not the same situation in an application where approvals from various agencies had to be recorded by inserting their response date. I wanted the user to have the facility to double click to insert today's date in the particular one of the 18 possible agencies which could be asked to respond.

I avoid using macros (except Autoexec) but in this case it is easy to write a one line macro that runs a function and uses the ActiveControl property to do whatever. Multiple select all of the relevant controls and you only need type the macro name in once.

In my case, I used
Code:
 Function InsertDate()
   Screen.ActiveControl = Date
End Function

I use the Tag property, to treat subgroups differently.

Incidentally, my code had a bit more code, to test that the text box was blank, otherwise the entry was not overwritten.
 

Users who are viewing this thread

Top Bottom