PDA

View Full Version : Userform mouseover text color change



oxicottin
12-14-2012, 02:47 PM
Hello, I would like to create a Sub TextColorChange () Module that I could call to change the text of a several labels on my userform (Form_Switchboard) when I mouse over them and back to original color when I'm not moused over them. Right now the text color is (&H00A76C42&). Would it be possible to use the labels tag to accomplish this, How can I do this?


Thanks!

p45cal
12-14-2012, 05:32 PM
Your Sub TextColorChange in a standard code-module:Sub TextColorChange(ctrl As Control)
ctrl.ForeColor = &H8000000D
End Sub

Something like this in the user form's code-module:Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
TextColorChange Label1
End Sub
Private Sub Label2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
TextColorChange Label2
End Sub
Private Sub Label3_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
TextColorChange Label3
End Sub
Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
For Each ctrl In Controls
If TypeName(ctrl) = "Label" Then ctrl.ForeColor = &H80000012
Next ctrl
End Sub


The userform had 3 labels on.
It worked here.

oxicottin
12-14-2012, 06:25 PM
It worked for a sec then I got varable not defined at (ctrl)

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
For Each ctrl In Controls
If TypeName(ctrl) = "Label" Then ctrl.ForeColor = &H80000012
Next ctrl
End Sub


I added (Dim ctrl As Control) in the above and it works but only if they are sitting on the userform itself. I have a few labels sitting on an image and i can mouse over each one and they all change color and stay the changed color until the mouse touches the userform itself.

Any thoughts?

p45cal
12-14-2012, 07:55 PM
add this to the standard code-module:Sub RestoreColours(UsForm As UserForm)
Dim ctrl As Control
For Each ctrl In UsForm.Controls
If TypeName(ctrl) = "Label" Then ctrl.ForeColor = &H80000012
Next ctrl
End Sub
change the Userform MouseMove to:Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
RestoreColours Me
End Sub
and add this (adjusted for your image) to the userform code-module:

Private Sub Image1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
RestoreColours Me
End Sub

oxicottin
12-14-2012, 08:05 PM
Briliant... That worked great, Thanks a million! One last question, how do I obtain the colors? What I mean is how did you come up with the color code for my color, Is there a online picker I can use? Thanks again!

:thumb

oxicottin
12-15-2012, 03:42 AM
Im sorry I found an issue. The problem is the code is changing the color of every label in the userform and I only wanted it for a few. How can I fix this? Thanks!

p45cal
12-15-2012, 04:52 AM
The problem is the code is changing the color of every label in the userform and I only wanted it for a few. How can I fix this? Thanks!Change to:Sub RestoreColours(UsForm As UserForm)
Dim ctrl As Control
Dim LabelNames, Ln
LabelNames = Array("Label1", "Label3") 'only include the names of the labels you want to restore.
For Each ctrl In UsForm.Controls
If TypeName(ctrl) = "Label" Then
For Each Ln In LabelNames
If ctrl.Name = Ln Then ctrl.ForeColor = &H80000012
Next Ln
End If
Next ctrl
End Sub
If there are many labels you want to restore, and only a few you want left as is, change it so that the list contains those you want left as is and change:If ctrl.Name = Ln Then ctrl.ForeColor = &H80000012
to:If ctrl.Name <> Ln Then ctrl.ForeColor = &H80000012

p45cal
12-15-2012, 05:12 AM
One last question, how do I obtain the colors? What I mean is how did you come up with the color code for my color, Is there a online picker I can use?From Help:"Settings
You can use any integer that represents a valid color. You can also specify a color by using the RGB function with red, green, and blue color components. The value of each color component is an integer that ranges from zero to 255. For example, you can specify teal blue as the integer value 4966415 or as red, green, and blue color components 15, 200, 75."

So you can use the likes of:
ctrl.ForeColor = RGB(167, 170, 28)
To find the colours, you can use the likes of MS Paint, go into Colors|Edit Colors, then click the button Define Custom Colors, you then get a cursor that you can position in a colour continuum, once you've got the colour you want, make a note of the Red, Green and Blue values in the bottom right hand corner.

oxicottin
12-15-2012, 05:40 AM
P45cal... I will check this afternoon when I get home... lol im whitetaile hunting in my blind right now :thumb

Thanks!

snb
12-15-2012, 07:18 AM
another approach using a class module: cfr. the attachment

oxicottin
12-15-2012, 11:28 AM
p45cal, That did it :thumb Worked like a charm... If I wanted to add possibly a bold or name like Tacoma I would just add something like .name = "Tacoma" and .Bold = 9 right? Thanks a million again!!!

On a second note, I hope this thread helps others because I searched for days before I posted this question and couldn't find anything on the subject...

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


another approach using a class module: cfr. the attachment

SNB, I checked out your example and it flickered like crazy for me? I am using one of your examples I had found somewhere (I cant remember where) and it works beautifully and I want to thank you if it was yours! its a border less userform (__userform without captionbar) and its named similar to your example and the userform is the same sorta. If it is yours how did you get/setup the image as the background for the sheet? Tricks and Tips or examples mean a lot to us beginners or users that just don't use it a lot and are greatly appreciated Thanks!

snb
12-15-2012, 01:59 PM
I used the same event (mousemove) p45cal used.

If you move very diligently along the border of the labels you can use it to turn the color on/of.
But perhaps it would be better to use the mousedown event.

Dive into the code and you can learn:
- to produce a collection.
- to populate that collection with instances of a class module
- to assign a userform control to a class variable
- to test the type of userformcontrols
- to include/exclude certain userformcontrols
- to enter eventcode in a classmodule
- to link eventcode in a classmodule to a collection of userformcontrols.

oxicottin
12-28-2012, 12:32 PM
NOTE: Attached example doesn't pertain to p45cal question, its
just a second way of trying to achieve the same look with less code but flickers like crazy and doesn't always work on MouseMove.


p45cal,

I had another question about getting the labels to change when MouseMove. I originally just needed a few labels to change but then I wanted/needed a few image borders to change as well so I got that working but now I need one last thing and cant figure it out. I have a label I want to be able to mouse over and have it change one of my images border color and back to normal when moused off of just like it already does for everything else.
There are two problems though;

[PROBLEMS] The label I want to be able to mouse over is called [lblAscDesc] and when MouseMove it needs to highlight on of two images [imgSortDescend] OR [imgSortAscend] depending on which one is Visible = true BUT in one instance neither might be visible because I have a image covering both if there isn't anything to sort because these images are used form sorting. I am already using both Images in code below to change the border for which ever one is visible. How could I achieve this? Also, I been messing with the example snb posted and I got it to work using the Tag property. How could I change your code to use the tag instead? And do you think it would be easier or beneficial to add a Tag = "?" or something instead of having a long list of labels names or image names ect in an array.

The label I want to MouseMove is [lblAscDesc] and I want it to change the border color of the Image [imgSortDescend] OR [imgSortAscend].

Here is what I have in my workbook that pertains to the original post.



*******ChangeControlLabel [module]



Option Explicit

Sub TextColorChange(ctrl As Control)
'Color,Font and size
ctrl.ForeColor = 26367 'Orange
ctrl.Font.Size = 9 'Font size
ctrl.Font.Bold = False
End Sub

Sub RestoreColours(UsForm As UserForm)
Dim ctrl As Control
Dim LabelNames, Ln
LabelNames = Array("btnHowToRetrieve", "btnFindImport", "btnClearSheet", "btnCopyToClipboard") 'only include the names of the labels you want to restore.
For Each ctrl In UsForm.Controls
If TypeName(ctrl) = "Label" Then
For Each Ln In LabelNames
If ctrl.Name = Ln Then
ctrl.ForeColor = &HA76C42 'Blue OR USE .ForeColor = RGB(0, 0, 255)
ctrl.Font.Size = 8
ctrl.Font.Bold = False
End If
Next Ln
End If
Next ctrl
End Sub

Sub BorderColorChange(ctrl As Control)
ctrl.BorderColor = 26367 'Orange
End Sub

Sub RestoreBorders(UsForm As UserForm)
Dim ctrl As Control
Dim ImageNames, Im
ImageNames = Array("imgSortAscend", "imgSortDescend")
For Each ctrl In UsForm.Controls
If TypeName(ctrl) = "Image" Then
For Each Im In ImageNames
If ctrl.Name = Im Then
ctrl.BorderColor = &HF5EFEA 'Blue
End If
Next Im
End If
Next ctrl
End Sub



*******FORMS MODULE



Private Sub btnHowToRetrieve_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'Changes text colors, font, bold, size on mouse over
TextColorChange btnHowToRetrieve
End Sub
Private Sub btnFindImport_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'Changes text colors, font, bold, size on mouse over
TextColorChange btnFindImport
End Sub
Private Sub btnClearSheet_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'Changes text colors, font, bold, size on mouse over
TextColorChange btnClearSheet
End Sub
Private Sub btnCopyToClipboard_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'Changes text colors, font, bold, size on mouse over
TextColorChange btnCopyToClipboard
End Sub
Private Sub imgSortAscend_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
BorderColorChange imgSortAscend
End Sub
Private Sub imgSortDescend_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
BorderColorChange imgSortDescend
End Sub
Private Sub imgMenuBackground_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'Restores colors back to normal
RestoreColours Me
RestoreBorders Me
End Sub
Private Sub lbxOracleProduct_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'Restores colors back to normal
RestoreColours Me
RestoreBorders Me
End Sub
Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'Restores colors back to normal
RestoreColours Me
RestoreBorders Me
End Sub


/////////////////////////////////////////////////////////////////////////////////////////////////////////////

snb,

I have been messing with your example and it still flickers but p45cals code does not it works perfectly and I wanted to know why it doesn't work. I got it working using the controls tag instead of if the label and "_" because
its easier to just add a TAG "?" and have it work. In your example I could only get it to work (60%) of the time and it almost never changes back to its original state after I mouse onto the form or off the control, even the Image does the same thing, flickers and doesn't always change back when moused off of control.

SEE ATTACHED :think:

oxicottin
12-29-2012, 03:37 AM
Ok, after messing with for awhile I got the module ti work using the the .Tag property and it works great. The label boxes I want to change I use a "?" in its tag and it doesn't matter what the name is and the Image I use a "*" in its .Tag property for the border to work. I haven't figured out the if I MouseMove on a label I can have a Images border change but I will keep messing.

Here is a question, tight now in my forms module I'm using a lot of MouseMoves for controls, can I create a Class Module that will be used for labels and Images and a Userform Mousemove for restore? I tried messing with but I'm not knowing where to start.

New Module working using tag

Option Explicit

Sub ControlColorChange(ctrl As Control)
'Use the [?] in the Labels controls [Tag] property for it to change colors
If TypeName(ctrl) = "Label" And ctrl.Tag = "?" Then
ctrl.ForeColor = &H80FF& 'Orange Forecolor = &H000080FF&
ctrl.Font.Size = 9 'Font Size
ctrl.Font.Bold = True 'Is Bold
End If
'Use the in the image controls [Tag] property for it to change colors
If TypeName(ctrl) = "Image" And ctrl.Tag = "*" Then
ctrl.BorderColor = &H80FF& 'Orange Forecolor = &H000080FF&
End If
End Sub

Sub RestoreControlColours(UsForm As UserForm)
Dim ctrl As Control
For Each ctrl In UsForm.Controls
'Use the [?] in the Labels controls [Tag] property for it to change colors
If TypeName(ctrl) = "Label" And ctrl.Tag = "?" Then
ctrl.ForeColor = &HA76C42 'Blue Forcolor = &H00A76C42&
ctrl.Font.Size = 8 'Font Size
ctrl.Font.Bold = False 'Isnt Bold
End If
'Use the in the image controls [Tag] property for it to change colors
If TypeName(ctrl) = "Image" And ctrl.Tag = "*" Then
ctrl.BorderColor = &HF5EFEA 'Blue Border = &H00F5EFEA&
End If
Next ctrl
End Sub

New Class Module Not working

Option Explicit
'\\Color's used: &H8000000F& = Blue[Background Color], &H000080FF& = Orange, &HA76C42 = Blue[Txt Color]

'Class Module for Changing Color Properties
Public WithEvents ALabel As MSForms.label
Public WithEvents BImage As MSForms.Image

Private Sub ALabel_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
ControlColorChange ALabel
End Sub

Private Sub BImage_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
ControlColorChange BImage
End Sub

Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
RestoreControlColours Me
End Sub

p45cal
12-29-2012, 11:45 AM
Try this as the ClassModule code called ColorControlsPublic WithEvents ALabel As MSForms.Label
Public WithEvents BImage As MSForms.Image
Public WithEvents UForm As MSForms.UserForm

Private Sub ALabel_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
ALabel.ForeColor = &H80FF&
ALabel.Font.Bold = True
ALabel.Font.Size = 9
End Sub

Private Sub BImage_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
BImage.BorderColor = &H80FF&
End Sub

Private Sub UForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim ctrl As Control
For Each ctrl In UForm.Controls
If TypeName(ctrl) = "Label" And ctrl.Tag = "?" Then
ctrl.ForeColor = &HA76C42 'Blue Forcolor = &H00A76C42&
ctrl.Font.Size = 8 'Font Size
ctrl.Font.Bold = False 'Isnt Bold
End If
If TypeName(ctrl) = "Image" And ctrl.Tag = "?" Then
ctrl.BorderColor = &HF5EFEA
End If
Next ctrl
End Sub
This as the userform code in its entirety:Option Explicit
Public ManipulatedProp As New Collection

Private Sub UserForm_Initialize()
Dim ctrl As Control
For Each ctrl In Controls
If TypeName(ctrl) = "Label" And ctrl.Tag = "?" Then
ManipulatedProp.Add New ColorControls, ctrl.Name
Set ManipulatedProp(ctrl.Name).ALabel = ctrl
End If
If TypeName(ctrl) = "Image" And ctrl.Tag = "?" Then
ManipulatedProp.Add New ColorControls, ctrl.Name
Set ManipulatedProp(ctrl.Name).BImage = ctrl
End If
Next
ManipulatedProp.Add New ColorControls, Me.Name
Set ManipulatedProp(Me.Name).UForm = Me
End Sub

The reason you had lots of flickering was because of lines like:
IIf(ALabel.Font.Bold = False, True, False)
which flips the Bold on-to-off and off-to-on many times as the mouse moves over the control triggering the event many times.

See attachment too.

oxicottin
12-29-2012, 11:59 AM
Cool, Works like a charm..... One question though. If I had a label and wanted when moused over trigger one of the images to show its border, how would I do that?

Thanks!

p45cal
12-29-2012, 12:23 PM
Cool, Works like a charm..... One question though. If I had a label and wanted when moused over trigger one of the images to show its border, how would I do that?Easy enough, but where to put the code? If there are lots of label/image pairs then in the class module perhaps, with some way of linking each pair, but you'll lose a lot of the generic-ness of the class module. If only one or two label/image pairs then in the userform's own code-module:Private Sub Label2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Image2.BorderColor = &H80FF&
End Sub
will successfully do what you want , and because the image is already tagged removing the border colour is automatic. If you do something similar with Image1, which isn't tagged, you'll have to revert to the Userform's own mouseover event to remove the Image1 border.

oxicottin
12-29-2012, 12:36 PM
Thanks again... I only have two insistence's were if I moused on lblHowToAscDesc it would highlight (Depending if the image was Visible = true) imgSortAscend OR imgSortDescend.

p45cal
12-29-2012, 12:53 PM
You don't have to worry about it being visible or not, you can still flip the border colour, visible image or not.

oxicottin
12-30-2012, 04:00 AM
P45Cal, I tried the mouseMove as suggested but it just highlights the image and wont return the color when moused off of also, some of the labels are sitting on a background image[imgMenuBackground] and wont change until I mouse off of image.

I was also wondering if highlighting or text coloring would work for a userform
multiselect listbox? I know when highlighted it blue in color just wondering if that can be changed.... Thanks Again!

Private Sub lblHowToAscDesc_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
imgSortAscend.BorderColor = &H80FF&
imgSortDescend.BorderColor = &H80FF&
End Sub

p45cal
12-30-2012, 04:24 AM
P45Cal, I tried the mouseMove as suggested but it just highlights the image and wont return the color when moused off of.
Private Sub lblHowToAscDesc_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
imgSortAscend.BorderColor = &H80FF&
imgSortDescend.BorderColor = &H80FF&
End SubYou haven't shared the workbook with the likes of imgSortAscend so I don't know if it's tagged appropriately - remember:
If you do something similar with Image1, which isn't tagged, you'll have to revert to the Userform's own mouseover event to remove the Image1 border.




I was also wondering if highlighting or text coloring would work for a userform
multiselect listbox? I know when highlighted it blue in color just wondering if that can be changed.... Thanks Again!I don't know, but I doubt it.

oxicottin
12-30-2012, 05:32 AM
The imgSortAscend and imgSortDescend are the only two labels that are highlighted using border. Thier .Tag is "*"

As for the listbox, just curious no bigie! Thanks again!!!

p45cal
12-30-2012, 06:07 AM
I'd have to look at the actual file. Private message me for my email address if you don't want it to be public (and you trust me!).

oxicottin
12-30-2012, 06:36 AM
Pm has been sent.... :thumb

oxicottin
12-30-2012, 01:57 PM
p45cal, I'm sorry the code does work. I forgot I changed the Image .Tag from "?" to "*" that's why the mousemove for the image didn't work. As for the background image blocking the uform MouseMove to change everything back to normal still isn't working.

'CHANGED .TAG
If TypeName(ctrl) = "Image" And ctrl.Tag = "*" Then


'*********************************** Control MouseMove Properties Start
Private Sub lblHowToAscDesc_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
imgSortAscend.BorderColor = &H80FF&
imgSortDescend.BorderColor = &H80FF&
End Sub
'*********************************** Control MouseMove Properties End

walden
11-03-2016, 11:24 PM
Thank you so much !