More efficiant code for mousemove text highlighting.

Greyowlsl

Mlak Mlak
Local time
Today, 23:30
Joined
Oct 4, 2006
Messages
204
Hi,

I have a number of buttons and they each have a mousemove cmd so when i move my mouse over the button the text goes blue and when i take it off it goes black again. I was wondering if there is a better way to code it. Below is the code for the page: (this is only a small section, the whole code it 13000+ characters so i couldent paste it all on here :P)

Code:
Private Sub cmd1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.cmd6.ForeColor = vbBlack
Me.cmd1.ForeColor = vbBlue
Me.cmd2.ForeColor = vbBlack
Me.cmd3.ForeColor = vbBlack
Me.cmd4.ForeColor = vbBlack
Me.cmd5.ForeColor = vbBlack
Me.cmd7.ForeColor = vbBlack
Me.cmd8.ForeColor = vbBlack
Me.cmd9.ForeColor = vbBlack
Me.cmd10.ForeColor = vbBlack
Me.cmd11.ForeColor = vbBlack
End Sub

Private Sub Box31_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.cmd16.SetFocus
Me.cmd1.ForeColor = vbBlack
Me.cmd2.ForeColor = vbBlack
Me.cmd3.ForeColor = vbBlack
Me.cmd4.ForeColor = vbBlack
Me.cmd5.ForeColor = vbBlack
Me.cmd6.ForeColor = vbBlack
Me.cmd7.ForeColor = vbBlack
Me.cmd8.ForeColor = vbBlack
Me.cmd9.ForeColor = vbBlack
Me.cmd10.ForeColor = vbBlack
Me.cmd11.ForeColor = vbBlack
Me.cmd16.SetFocus
End Sub

Private Sub Form_GotFocus()
Me.cmd16.SetFocus
End Sub

Private Sub Form_Open(Cancel As Integer)
Me.cmd16.SetFocus
End Sub

Thanks,

Leon
 
How about:

Code:
Private Function ButtonSetter(CtlName As String)
  Dim ctl                As Control
  For Each ctl In Me.Controls
    If ctl.ControlType = acCommandButton Then
      If ctl.Name = CtlName Then
        ctl.ForeColor = vbBlue
      Else
        ctl.ForeColor = vbBlack
      End If
    End If
  Next ctl
End Function

Called from each button. You might find a naming convention helpful. Down the road, nobody will know what "cmd7" or "box31" is for. "cmdSave" or "txtFirstName" on the other hand, tell you exactly what they are.
 
did pbaldy's idea work

you can have a function event handler on each control affect by the mousemove - needs brackets

myhandler(mycontrolname)
(not sure if it should be =myhandler(mycontrolname) - there is a difference

then a global or form function

function myhandler(ctrl as control) as anytype
ctrl.backcolor = etc
end if

but i am not sure how you would unset the control after the mouse moves off elsewhere.
 
Of course it works; I tested it. I should be insulted by the question! :p

Unsetting the control is easy, when you stop and think about it. ;)
 
Hey again,

Thanks for the code, but I cant seem to get it to work; What do i actualy have to change to fit it to my form?
 
In the function itself, nothing. In the mousemove event of each button, instead of all the stuff you have:

ButtonSetter ("ButtonNameHere")
 
The buttons only unset when i move over another button, but I understand the way the code works and i placed a transperant box around the buttons so it takes the ctrl away from the buttons and makes them unset

Code:
Private Sub Box50_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
ButtonSetter ("Box50")
End Sub

Also i will be changing the names of the buttons later but this database it only at testing stages.

Thanks for your help,

Leon
 
sorry to impugn you abilities pbaldy,

i meant did your solution solve the problem for the poster. i was trying to see if you could achieve this with using an event handler for one control only, rather than iterating all the controls, but i couldnt see how to change it back after the mouse moves off. since yo u say its obvious i'll have another look at the events myself
 
i've tried my method now, and it didn't work, but i've not got time to look further now. lol
 
me?

I always did, I just thought one might be able to set the current control over which the mouse was hovering without having to iterate every control on the form, but I can't get the syntax right.
 

Users who are viewing this thread

Back
Top Bottom