473,549 Members | 3,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simulating rollover effects

I have constructed the following code that simulates the common
rollover effect when moving the mouse over a label (this example makes
the label bold.)
I'm wondering if anyone has come up with a better solution.
lq

'start code:
Private RolloverBoldFla g As Boolean
Function RolloverBold(my Frm As String, myLabel As Integer)
' Change the selected "Z" label font to bold OnMouseMove
' and un-bolds all other "Z" labels.

' Examples:
' for a label named "Z1" : OnMouseMove = RolloverBold([FormName],1)
' for a label named "Z2" : OnMouseMove = RolloverBold([FormName],2)
' etc.
' for the form detail: OnMouseMove = RolloverBold([FormName],0)
Dim ctl As Control

'determine the selected label:

If myLabel > 0 Then 'it is a label:

If Forms(myFrm)("Z " & CStr((myLabel)) ).FontBold = False Then

'reset all label buttons colors:
For Each ctl In Forms(myFrm).Co ntrols
If ctl.ControlType = acLabel And _
Left(ctl.Name, 1) = "Z" _
Then ctl.FontBold = False
Next ctl

'set selected label to bold:
Forms(myFrm)("Z " & CStr((myLabel)) ).FontBold = True

RolloverBoldFla g = True
End If

Else 'it is not a label, it is a command to reset all labels:

If RolloverBoldFla g = True Then

'reset all label buttons colors:
For Each ctl In Forms(myFrm).Co ntrols
If ctl.ControlType = acLabel And _
Left(ctl.Name, 1) = "Z" _
Then ctl.FontBold = False
Next ctl

RolloverBoldFla g = False

End If

End If

End Function

'end code

Nov 13 '05 #1
47 7570
rkc
Lauren Quantrell wrote:
I have constructed the following code that simulates the common
rollover effect when moving the mouse over a label (this example makes
the label bold.)
I'm wondering if anyone has come up with a better solution.


I don't know about better. If your code works then it can't get
better.

I ripped this idea off from a book I have on VB design patterns.
It works fairly well but it is possible to race the mouse over
the label and leave it bold on exit.

Another way is to put a large label with it's background color
set to the form's bg color behind the smaller label and use
that label's mouseover event to reset the smaller label.

<clsRollOverLab el>
Private WithEvents lbl As Access.Label

Public Sub Init(l As Access.Label)
Set lbl = l
lbl.OnMouseMove = "[Event Procedure]"
End Sub

Private Sub lbl_MouseMove(B utton As Integer, Shift As Integer, X As
Single, Y As Single)
Dim h As Integer
Dim w As Integer

h = lbl.Height
w = lbl.Width

If X < 32 Or Y < 32 Or X > w - 64 Or Y > h - 64 Then
lbl.FontBold = False
Else
lbl.FontBold = True
End If
End Sub
</clsRollOverLabe l>

<form module>
Dim ro As clsRollOverLabe l

Private Sub Form_Load()
Set ro = New clsRollOverLabe l
ro.Init Me.Label2
End Sub

Private Sub Form_Unload(Can cel As Integer)
Set ro = Nothing
End Sub
</form module>
Nov 13 '05 #2
The problem with that approach is that if the user rolls the mouse very
quickly, the OnMouseMove of the underlying label will not be fired and
the label will remain bold. I have tried this approach and instead
favor the approach of using the form's detail section to fire the undo
bold command using OnMouseMov and using: For Each ctl In
Forms(myFrm).Co ntrols...
Just my preference.

I'm hoping to find a more elegant want to do this

Nov 13 '05 #3
DFS
Lauren Quantrell wrote:
The problem with that approach is that if the user rolls the mouse
very quickly, the OnMouseMove of the underlying label will not be
fired and the label will remain bold. I have tried this approach and
instead favor the approach of using the form's detail section to fire
the undo bold command using OnMouseMov and using: For Each ctl In
Forms(myFrm).Co ntrols...
Just my preference.

I'm hoping to find a more elegant want to do this


Lauren,

Here's your original

Function RolloverBold(my Frm As String, myLabel As Integer)

Dim ctl As Control

If myLabel > 0 Then 'it is a label:
If Forms(myFrm)("Z " & CStr((myLabel)) ).FontBold = False Then
For Each ctl In Forms(myFrm).Co ntrols
If ctl.ControlType = acLabel And Left(ctl.Name, 1) = "Z"
then
ctl.FontBold = False
endif
Next ctl
Forms(myFrm)("Z " & CStr((myLabel)) ).FontBold = True
RolloverBoldFla g = True
End If
Else 'it is not a label, it is a command to reset all labels:
If RolloverBoldFla g = True Then
For Each ctl In Forms(myFrm).Co ntrols
If ctl.ControlType = acLabel And Left(ctl.Name, 1) = "Z"
then
ctl.FontBold = False
endif
Next ctl
RolloverBoldFla g = False
End If
End If

End Function
I don't see where you're passing or using the rolloverbold flag, and it
seems like you would only call the function from a label OnMouseMove anyway,
and you're resetting the font in both conditions, so you may be able to
shorten to (haven't tested it)

Dim ctl As Control
For Each ctl In Forms(myFrm).Co ntrols
If ctl.ControlType = acLabel And Left(ctl.name, 1) = "Z" Then
ctl.FontBold = False
End If
Next ctl

set ctl = Forms(myFrm)("Z " & CStr(myLabel))
If ctl.FontBold = False Then ctl.FontBold = True
=============== =============== =============== =============== ========
How about a less elegant, but potentially faster, way?

In the past, when the nbr of labels is small, I've hard coded the labels
that get changed (bold, or sunken/raised) OnMouseMove.

For my set of 3 labels, each gets OnMouseMove code like this:

If Me.lblDocAttach .SpecialEffect = 0 Then
Me.lblDocSearch .SpecialEffect = 0
Me.lblDocRetrie ve.SpecialEffec t = 0
Me.lblDocAttach .SpecialEffect = 1
End If

So for each label I have 5 lines, and one condition statement that gets
evaluated. It's a bear to maintain the more labels you have, of course, or
the more settings you apply. But it runs blazingly fast.

In the past I've also iterated the form controls as you show, but it seems
inefficient to call a function to evaluate every control on the form, check
if it's a label, check the caption or the name, then do something. My gut
feeling is the hard-coding is quicker than walking the controls.

Maybe I'm wrong. If someone can prove it's not the best way (excepting
maintenance nightmares), that it's slower or uses more system resources,
I'll try the iterative method.
Nov 13 '05 #4

Lauren Quantrell wrote:
I'm wondering if anyone has come up with a better solution.


This is a different solution. It's quite old (1999). I've quickly
modified it to bring it in line with AC2000, so it may be buggy.

http://ffdba.com/downloads/Hover Demo 2000.dat

rename to "Hover Demo 2000.mdb"

Nov 13 '05 #5
rkc
Lauren Quantrell wrote:
The problem with that approach is that if the user rolls the mouse very
quickly, the OnMouseMove of the underlying label will not be fired and
the label will remain bold. I have tried this approach and instead
favor the approach of using the form's detail section to fire the undo
bold command using OnMouseMov and using: For Each ctl In
Forms(myFrm).Co ntrols...
Just my preference.

I'm hoping to find a more elegant want to do this


Well, I guess you can't beat Lyle Fairfield's offering for elegant.

The solution to the background label approach is to use just one
label for the background and make it cover the entire area. The
drawback is that you have to use the class approach as in my first
example.

I have to tell you though, the one time I implemented this in a
real project the client emailed me an hour latter and asked me
to "stop making it do that."



Nov 13 '05 #6
http://ffdba.com/downloads/Hover.dat
(rename to "Hover.mdb" )

is a classless rendition created in Access 2003 but I think there is
nothing in it that would require anything more than Access 97.

Nov 13 '05 #7
rkc
Lauren Quantrell wrote:
The problem with that approach is that if the user rolls the mouse very
quickly, the OnMouseMove of the underlying label will not be fired and
the label will remain bold. I have tried this approach and instead
favor the approach of using the form's detail section to fire the undo
bold command using OnMouseMov and using: For Each ctl In
Forms(myFrm).Co ntrols...
Just my preference.

I'm hoping to find a more elegant want to do this


O.K. After a cup of coffee and a couple of chocolate pop tarts
I figured out how to watch the form's detail section for a
mouseover event. This seems to solve the rapid mouse move problem.
<clsRollOverLab el>
Private WithEvents mLabel As Label
Private WithEvents mBackGround As Access.Section

Public Function SetTextLabel(lb l As Label)

Set mLabel = lbl
mLabel.OnMouseM ove = "[Event Procedure]"
Set mBackGround = mLabel.parent.D etail
mBackGround.OnM ouseMove = "[Event Procedure]"

End Function

Private Sub mBackGround_Mou seMove(Button As Integer, Shift As Integer, X
As Single, Y As Single)
mLabel.FontBold = False
End Sub

Private Sub mlabel_MouseMov e(Button As Integer, Shift As Integer, X As
Single, Y As Single)
mLabel.FontBold = True
End Sub
</clsRollOverLabe l>

Nov 13 '05 #8
Does this work when there is no visible detail section?

Nov 13 '05 #9
rkc
lylefair wrote:
Does this work when there is no visible detail section?


Good question. Probably not. But, then the op was using the
Detail mouseover to trigger their function so I figured...

You will have to explain to me a situation where there would be
no visible Detail section, but the Detail section would still
contain controls.

Can you make your solution work without a Timer event?
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.