Click to See Complete Forum and Search --> : Highliting label


Maarten Versteeg
January 21st, 2000, 08:31 AM
Hi,
When we want cross over a label with caption, we want to highlight or color the caption. If we leave the label we want to change the color back to the normal color.

Please help us
Thank you

Johnny101
January 21st, 2000, 11:03 AM
Try this:

private Sub Form_MouseMove(Button as Integer, Shift as Integer, X as Single, Y as Single)
If Label1.ForeColor <> vbBlack then
Label1.ForeColor = vbBlack
End If
End Sub

private Sub Label1_MouseMove(Button as Integer, Shift as Integer, X as Single, Y as Single)
If Label1.ForeColor <> vbRed then
Label1.ForeColor = vbRed
End If
End Sub




When the mouse is over the label, the text in the label will turn red. When the mouse is over the form it turns the text back to black. The if statments keep the code from re-assigning the color every time the mouse moves, to improve performance.

Hope this helps,
John


John Pirkey
MCSD
www.ShallowWaterSystems.com

Rippin
January 21st, 2000, 04:01 PM
That will work, but if there are any other controls on the form besides the one label, then it probably won't work well. I don't know of a goot way to perform this action using a label, but if you use a picturebox (with AutoRedraw set to True) you can use the SetCapture and ReleaseCapture API calls. Try this:

option Explicit
'
'API Declarations
private Declare Function SetCapture Lib "user32" _
(byval hWnd as Long) as Long
private Declare Function ReleaseCapture _
Lib "user32" () as Long
'
private Sub Form_Load()
'Make sure the picturebox's AutoRedraw property
'is true.
Picture1.AutoRedraw = true
'Place the caption for the "make-shift" label
'in the Tag property so we don't have to
'remember it.
Picture1.Tag = "My Label"
'print the caption
Picture1.print Picture1.Tag
End Sub
'
private Sub Picture1_MouseMove (Button as Integer, Shift as Integer, X as Single, Y as Single)
'Check to see if the mouse is outside of the
'picturebox's borders
If (X < 0 Or X > Picture1.Width) Or (Y < 0 Or Y > Picture1.Height) then
'Mouse is not over the picturebox.
ReleaseCapture
'set the forecolor to black
Picture1.ForeColor = vbBlack
else
'Mouse is within the borders
SetCapture Picture1.hWnd
'set the forecolor to the highlight color
Picture1.ForeColor = vbBlue
End If
'
'Clear the picturebox
Picture1.Cls
'Redraw the caption
Picture1.print Picture1.Tag
End Sub




It's a little wierd, but it works. There may be a better (easier way) to do this, but I don't know what it is.

Hope this helps,
Rippin

Johnny101
January 21st, 2000, 04:48 PM
I know this isn't the best code to do this, but i don't see why more controls on the form would make a difference in the functionality of it. I can see having to do it for 20 or 30 labels and all of them have to have this "highlight" function - that would be bad because the code would have to do 20 or 30 if checks to find the one label that needs changing, if the labels weren't in a control array. If they were, then setting the color would still be just the one liner above, but setting it back would still go through the array. Where is the OnMouseOut function that's in VBScript when you need it!?
:)

John

John Pirkey
MCSD
www.ShallowWaterSystems.com

Rippin
January 21st, 2000, 07:02 PM
The reason I say that it won't work well with multiple controls is because the Form_MouseMove might not fire if you move over, say, a commandbutton. Especially if the controls are positioned closely together. In order for it to work, you would have to add the code to every control's MouseMove event. Also you would need to loop though all other controls to make sure their color is not stuck in the highlight color. Like I said, it will work, I'm not disputing that, I'm just offering another possible solution. Please don't think I was trying to discredit your code.

Rippin

Eric Gravert
January 21st, 2000, 08:29 PM
There is a great example of this here: http://www.mvps.org/vb/

Go to the samples section and check out HyperLabel.Zip.
It will do exactly what you want and probably more.

Tina
January 21st, 2000, 11:10 PM
Nice ! tried it out......works smooth.......