Would like to know if there is a better way to do what I did

yankarinRG

New member
Local time
Today, 16:57
Joined
Nov 6, 2016
Messages
5
Hello,
I've got a form which works as a menu with a pair of buttons with a black picture and caption. When the mouse is over the button, the picture and the caption turn white.
That's how I did it:
-Added the black and white images to the database gallery;
-Set the CommandButton.PictureType Property to shared;
-Set the CommandButton.Picture Property to "black";

Now in VBA:
Code:
Private Sub btnTest_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Me.btnTest.Picture = "white" Then
        Exit Sub
    Else
        Me.btnTest.Picture = "white"
    End If
End Sub

Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Me.btnTest.Picture = "black" Then
        Exit Sub
    Else
        Me.btnTest.Picture = "black"
    End If
End Sub
This is to revert to black if the cursor leaves the button.

However I think this event handling can become pretty slow, since the CPU use on my PC (i7-3770K) raises from 1% to 6%.

Is there a better/more elegant solution to do this?

Thanks!
 
Mousemove fires repeatedly while the mouse moves. You could comment out your event handler and see if the CPU consumption changes, but it might be the same with or without your code.
Also, IMO your code would be a bit clearer if you do this...
Code:
Private Sub btnTest_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
[COLOR="Green"]'   changes the property to white, but only if it isn't already white[/COLOR]
    If Me.btnTest.Picture <> "white" Then Me.btnTest.Picture = "white"
End Sub
1) You don't need the whole if..else...end if block, and
2) it is not recommended that you embed an arbitrary "Exit Sub" in your code. Yes, the language allows it, but, as in the code you posted, it generally means that you've been sloppy with your program flow. If you are tempted to have an "Exit Sub" before the end of the routine, try to imagine another way to structure your code so that all paths exit the routine normally and at the end.
Cheers,
 
Thanks MarkK for the guidance.
I will totally change the logic to be similar to yours.
Anyway when I said it was a little bit resource consuming I meant if there was another way to check if the cursor left the button, like a MouseLeft event, instead of the MouseMove event on the detail section of the form.
P.S. What if i set a variable called isWhite as boolean and check if it's true or false instead of checking the string: will the evaluation be faster?
P.S.S. Could you give me more insight on the Exit Sub use? Are you referring to Singe Entry, Single Exit notion?
Cheers!
 
Logically there's no reason for a test at all:

Code:
    If Me.btnTest.Picture = "white" Then
        Exit Sub
    Else
        Me.btnTest.Picture = "white"
    End If

In every case, after the function runs Me.btnTest.Picture will equal "white". Right? So why test? Just set it:

Code:
Private Sub btnTest_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Me.btnTest.Picture = "white"
End Sub

Same thing for the other function
 
Code:
Private Sub btnTest_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Me.btnTest.Picture = "white"
End Sub
When you set this property, the underlying implementation assumes the value is changing and repaints the area, which causes flicker. If you do the test, and only change the value if it requires changing, you create a smoother appearance. And MouseMove fires a lot.
 

Users who are viewing this thread

Back
Top Bottom