Label Formatting Code - Repetitive

K

Kirstie Adam

Hi All,

I have a switchboard which is beginning to get a bit ugly with buttons and
labels everywhere, so i am re-organising it.

For each of my labels (currently font blue and underlined to look like a
hyperlink) on the MouseDown event i want the text color to change to Red and
the FontUnderline to switch off, and then on MouseUp i want the text colour
to change again and the underline back on.

I have got this working, however am having to type the same code in over and
over for each individual label. is there a way i can save the code in a
module or something and then attach it to each label i want to do this?

I have never worked with modules before, so if this is the case, please
could you explain from the basics?!

Just so you know, the code i have at the moment, against each label is:

Private Sub lblSiteInfo_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single)
Me.lblSiteInfo.ForeColor = vbRed
Me.lblSiteInfo.FontUnderline = False
End Sub

Private Sub lblSiteInfo_MouseUp(Button As Integer, Shift As Integer, X As
Single, Y As Single)
Me.lblSiteInfo.ForeColor = 10040115
Me.lblSiteInfo.FontUnderline = True
End Sub


And if that's not enough, i'd quite like the background of a label to change
as the mouse moves over it and back again when the mouse moves off it, even
if not clicked, is this possible??

Any suggestions much appreciated!

Kirstie
 
G

Guest

You can use a function or sub to do this. Where you put it depends on how
you want it scoped. That is, if you put it in a form's module, then it is
(normally) visible only to the form. (It is possible to reference a public
function in another form's module, but not a good practice). If you put it
in a standard module and make it public, it is visible to the application.
The trick is to pass the control to the function:

Public Sub ChangeColor(ctl As Control, lngForeColor, blnUnderline)

ctl.ForeColor = lngForeColor
ctl.FontUnderline = blnUnderline

End Sub

Private Sub lblSiteInfo_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single)
Call ChangeColor(Me.lblSiteInfo, vbRed, False)
End Sub

Private Sub lblSiteInfo_MouseUp(Button As Integer, Shift As Integer, X As
Single, Y As Single)
Call ChangeColor(Me.lblSiteInfo, 10040115, True)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top