Home All Groups Group Topic Archive Search About

Changing Color of Textbox on Hover

Author
1 Dec 2006 4:03 PM
Henry Jones
I have a VS 2005 VB.NET project and would like to change the color of the
textbox when the user hovers over it.  In a Module I have the following
routines:

Public Sub Button_Hover(ByRef btnName As Button)

    btnName.BackColor = Color.BlanchedAlmond

End Sub

Public Sub Button_Leave(ByRef btnName As Button)

    btnName.BackColor = Color.Transparent

End Sub

----------------------------------------------



I called the routines from the following code:



Private Sub cmdTierCancel_MouseHover(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdTierCancel.MouseHover

    Button_Hover(cmdTierCancel)

End Sub

Private Sub cmdTierCancel_MouseLeave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdTierCancel.MouseLeave

    Button_Leave(cmdTierCancel)

End Sub

---------------------------------

SO FAR SO GOOD.  I thought it would be nice to use the Handles feature and
call the routine like this:

---------------------------------

Public Sub Handles_All_Buttons Handles(ByVal sender As Object, ByVal e As
System.EventArgs)  cmd1.MouseHover, cmd2.MouseHover, cmd3.MouseHover

    Button_Leave(sender)

End Sub



But this routine doesn't work.  Can anyone tell me what I am doing wrong?



Thanks

Author
1 Dec 2006 4:10 PM
Henry Jones
In the last routine, Button_Leave should be Button_Hover


Show quoteHide quote
"Henry Jones" <he***@yada.com> wrote in message
news:eboECJWFHHA.1248@TK2MSFTNGP02.phx.gbl...
>I have a VS 2005 VB.NET project and would like to change the color of the
>textbox when the user hovers over it.  In a Module I have the following
>routines:
>
> Public Sub Button_Hover(ByRef btnName As Button)
>
>    btnName.BackColor = Color.BlanchedAlmond
>
> End Sub
>
> Public Sub Button_Leave(ByRef btnName As Button)
>
>    btnName.BackColor = Color.Transparent
>
> End Sub
>
> ----------------------------------------------
>
>
>
> I called the routines from the following code:
>
>
>
> Private Sub cmdTierCancel_MouseHover(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles cmdTierCancel.MouseHover
>
>    Button_Hover(cmdTierCancel)
>
> End Sub
>
> Private Sub cmdTierCancel_MouseLeave(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles cmdTierCancel.MouseLeave
>
>    Button_Leave(cmdTierCancel)
>
> End Sub
>
> ---------------------------------
>
> SO FAR SO GOOD.  I thought it would be nice to use the Handles feature and
> call the routine like this:
>
> ---------------------------------
>
> Public Sub Handles_All_Buttons Handles(ByVal sender As Object, ByVal e As
> System.EventArgs)  cmd1.MouseHover, cmd2.MouseHover, cmd3.MouseHover
>
>    Button_Leave(sender)
>
> End Sub
>
>
>
> But this routine doesn't work.  Can anyone tell me what I am doing wrong?
>
>
>
> Thanks
>
>
>
>
>
>
>
>
Author
1 Dec 2006 5:53 PM
RobinS
In the last routine, shouldn't the Handles keyword come
after the "(ByVal sender as Object, e as EventArgs)"
and before cmd1.MouseHover, etc.?

Robin S.
------------------------------------------
Show quoteHide quote
"Henry Jones" <he***@yada.com> wrote in message
news:OTla$MWFHHA.536@TK2MSFTNGP02.phx.gbl...
> In the last routine, Button_Leave should be Button_Hover
>
>
> "Henry Jones" <he***@yada.com> wrote in message
> news:eboECJWFHHA.1248@TK2MSFTNGP02.phx.gbl...
>>I have a VS 2005 VB.NET project and would like to change the color of the
>>textbox when the user hovers over it.  In a Module I have the following
>>routines:
>>
>> Public Sub Button_Hover(ByRef btnName As Button)
>>
>>    btnName.BackColor = Color.BlanchedAlmond
>>
>> End Sub
>>
>> Public Sub Button_Leave(ByRef btnName As Button)
>>
>>    btnName.BackColor = Color.Transparent
>>
>> End Sub
>>
>> ----------------------------------------------
>>
>>
>>
>> I called the routines from the following code:
>>
>>
>>
>> Private Sub cmdTierCancel_MouseHover(ByVal sender As Object, ByVal e As
>> System.EventArgs) Handles cmdTierCancel.MouseHover
>>
>>    Button_Hover(cmdTierCancel)
>>
>> End Sub
>>
>> Private Sub cmdTierCancel_MouseLeave(ByVal sender As Object, ByVal e As
>> System.EventArgs) Handles cmdTierCancel.MouseLeave
>>
>>    Button_Leave(cmdTierCancel)
>>
>> End Sub
>>
>> ---------------------------------
>>
>> SO FAR SO GOOD.  I thought it would be nice to use the Handles feature
>> and call the routine like this:
>>
>> ---------------------------------
>>
>> Public Sub Handles_All_Buttons Handles(ByVal sender As Object, ByVal e As
>> System.EventArgs)  cmd1.MouseHover, cmd2.MouseHover, cmd3.MouseHover
>>
>>    Button_Leave(sender)
>>
>> End Sub
>>
>>
>>
>> But this routine doesn't work.  Can anyone tell me what I am doing wrong?
>>
>>
>>
>> Thanks
>>
>>
>>
>>
>>
>>
>>
>>
>
>
Author
1 Dec 2006 6:14 PM
Henry Jones
Yes, that was another "cut and paste" typo.  In my code, it is correct.
Oops.



Show quoteHide quote
"RobinS" <RobinS@NoSpam.yah.none> wrote in message
news:bcCdnXIhBvit8e3YnZ2dnUVZ_o6dnZ2d@comcast.com...
> In the last routine, shouldn't the Handles keyword come
> after the "(ByVal sender as Object, e as EventArgs)"
> and before cmd1.MouseHover, etc.?
>
> Robin S.
> ------------------------------------------
> "Henry Jones" <he***@yada.com> wrote in message
> news:OTla$MWFHHA.536@TK2MSFTNGP02.phx.gbl...
>> In the last routine, Button_Leave should be Button_Hover
>>
>>
>> "Henry Jones" <he***@yada.com> wrote in message
>> news:eboECJWFHHA.1248@TK2MSFTNGP02.phx.gbl...
>>>I have a VS 2005 VB.NET project and would like to change the color of the
>>>textbox when the user hovers over it.  In a Module I have the following
>>>routines:
>>>
>>> Public Sub Button_Hover(ByRef btnName As Button)
>>>
>>>    btnName.BackColor = Color.BlanchedAlmond
>>>
>>> End Sub
>>>
>>> Public Sub Button_Leave(ByRef btnName As Button)
>>>
>>>    btnName.BackColor = Color.Transparent
>>>
>>> End Sub
>>>
>>> ----------------------------------------------
>>>
>>>
>>>
>>> I called the routines from the following code:
>>>
>>>
>>>
>>> Private Sub cmdTierCancel_MouseHover(ByVal sender As Object, ByVal e As
>>> System.EventArgs) Handles cmdTierCancel.MouseHover
>>>
>>>    Button_Hover(cmdTierCancel)
>>>
>>> End Sub
>>>
>>> Private Sub cmdTierCancel_MouseLeave(ByVal sender As Object, ByVal e As
>>> System.EventArgs) Handles cmdTierCancel.MouseLeave
>>>
>>>    Button_Leave(cmdTierCancel)
>>>
>>> End Sub
>>>
>>> ---------------------------------
>>>
>>> SO FAR SO GOOD.  I thought it would be nice to use the Handles feature
>>> and call the routine like this:
>>>
>>> ---------------------------------
>>>
>>> Public Sub Handles_All_Buttons Handles(ByVal sender As Object, ByVal e
>>> As System.EventArgs)  cmd1.MouseHover, cmd2.MouseHover, cmd3.MouseHover
>>>
>>>    Button_Leave(sender)
>>>
>>> End Sub
>>>
>>>
>>>
>>> But this routine doesn't work.  Can anyone tell me what I am doing
>>> wrong?
>>>
>>>
>>>
>>> Thanks
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>
>>
>
>
Author
1 Dec 2006 4:16 PM
Tim Patrick
Did you intend to call Button_Hover from your final Handles-enabled routine,
or was that just a typo?

-----
Tim Patrick - www.timaki.com
Start-to-Finish Visual Basic 2005

Show quoteHide quote
> I have a VS 2005 VB.NET project and would like to change the color of
> the textbox when the user hovers over it.  In a Module I have the
> following routines:
>
> Public Sub Button_Hover(ByRef btnName As Button)
>
> btnName.BackColor = Color.BlanchedAlmond
>
> End Sub
>
> Public Sub Button_Leave(ByRef btnName As Button)
>
> btnName.BackColor = Color.Transparent
>
> End Sub
>
> ----------------------------------------------
>
> I called the routines from the following code:
>
> Private Sub cmdTierCancel_MouseHover(ByVal sender As Object, ByVal e
> As System.EventArgs) Handles cmdTierCancel.MouseHover
>
> Button_Hover(cmdTierCancel)
>
> End Sub
>
> Private Sub cmdTierCancel_MouseLeave(ByVal sender As Object, ByVal e
> As System.EventArgs) Handles cmdTierCancel.MouseLeave
>
> Button_Leave(cmdTierCancel)
>
> End Sub
>
> ---------------------------------
>
> SO FAR SO GOOD.  I thought it would be nice to use the Handles feature
> and call the routine like this:
>
> ---------------------------------
>
> Public Sub Handles_All_Buttons Handles(ByVal sender As Object, ByVal e
> As System.EventArgs)  cmd1.MouseHover, cmd2.MouseHover,
> cmd3.MouseHover
>
> Button_Leave(sender)
>
> End Sub
>
> But this routine doesn't work.  Can anyone tell me what I am doing
> wrong?
>
> Thanks
>
Author
1 Dec 2006 5:24 PM
Cor Ligthert [MVP]
Henry,

I am interested. What makes it that you think nicer to use handles instead
of .Net methods?

Cor

You want to show that you are a starting programmer or something like that?
Show quoteHide quote
"Henry Jones" <he***@yada.com> schreef in bericht
news:eboECJWFHHA.1248@TK2MSFTNGP02.phx.gbl...
>I have a VS 2005 VB.NET project and would like to change the color of the
>textbox when the user hovers over it.  In a Module I have the following
>routines:
>
> Public Sub Button_Hover(ByRef btnName As Button)
>
>    btnName.BackColor = Color.BlanchedAlmond
>
> End Sub
>
> Public Sub Button_Leave(ByRef btnName As Button)
>
>    btnName.BackColor = Color.Transparent
>
> End Sub
>
> ----------------------------------------------
>
>
>
> I called the routines from the following code:
>
>
>
> Private Sub cmdTierCancel_MouseHover(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles cmdTierCancel.MouseHover
>
>    Button_Hover(cmdTierCancel)
>
> End Sub
>
> Private Sub cmdTierCancel_MouseLeave(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles cmdTierCancel.MouseLeave
>
>    Button_Leave(cmdTierCancel)
>
> End Sub
>
> ---------------------------------
>
> SO FAR SO GOOD.  I thought it would be nice to use the Handles feature and
> call the routine like this:
>
> ---------------------------------
>
> Public Sub Handles_All_Buttons Handles(ByVal sender As Object, ByVal e As
> System.EventArgs)  cmd1.MouseHover, cmd2.MouseHover, cmd3.MouseHover
>
>    Button_Leave(sender)
>
> End Sub
>
>
>
> But this routine doesn't work.  Can anyone tell me what I am doing wrong?
>
>
>
> Thanks
>
>
>
>
>
>
>
>
Author
1 Dec 2006 5:42 PM
Henry Jones
I thought it be cleaner to use one routine that handles many buttons than to
create more code.  Better for maintenance later on.  Am I wrong?
But that isn't the point.  I would like to know how to do it, now that I
have thought of it.  I don't know if it's good programming practice, but
that is what these forums are all about.

Thanks,

Henry

Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:%23uPFY1WFHHA.3268@TK2MSFTNGP04.phx.gbl...
> Henry,
>
> I am interested. What makes it that you think nicer to use handles instead
> of .Net methods?
>
> Cor
>
> You want to show that you are a starting programmer or something like
> that?
> "Henry Jones" <he***@yada.com> schreef in bericht
> news:eboECJWFHHA.1248@TK2MSFTNGP02.phx.gbl...
>>I have a VS 2005 VB.NET project and would like to change the color of the
>>textbox when the user hovers over it.  In a Module I have the following
>>routines:
>>
>> Public Sub Button_Hover(ByRef btnName As Button)
>>
>>    btnName.BackColor = Color.BlanchedAlmond
>>
>> End Sub
>>
>> Public Sub Button_Leave(ByRef btnName As Button)
>>
>>    btnName.BackColor = Color.Transparent
>>
>> End Sub
>>
>> ----------------------------------------------
>>
>>
>>
>> I called the routines from the following code:
>>
>>
>>
>> Private Sub cmdTierCancel_MouseHover(ByVal sender As Object, ByVal e As
>> System.EventArgs) Handles cmdTierCancel.MouseHover
>>
>>    Button_Hover(cmdTierCancel)
>>
>> End Sub
>>
>> Private Sub cmdTierCancel_MouseLeave(ByVal sender As Object, ByVal e As
>> System.EventArgs) Handles cmdTierCancel.MouseLeave
>>
>>    Button_Leave(cmdTierCancel)
>>
>> End Sub
>>
>> ---------------------------------
>>
>> SO FAR SO GOOD.  I thought it would be nice to use the Handles feature
>> and call the routine like this:
>>
>> ---------------------------------
>>
>> Public Sub Handles_All_Buttons Handles(ByVal sender As Object, ByVal e As
>> System.EventArgs)  cmd1.MouseHover, cmd2.MouseHover, cmd3.MouseHover
>>
>>    Button_Leave(sender)
>>
>> End Sub
>>
>>
>>
>> But this routine doesn't work.  Can anyone tell me what I am doing wrong?
>>
>>
>>
>> Thanks
>>
>>
>>
>>
>>
>>
>>
>>
>
>
Author
1 Dec 2006 6:27 PM
RobinS
Henry,

I have a routine in my base form that adds event handlers for
mouseenter and mouseleave to textboxes. This changes the backcolor
when the user enters the textbox, and changes it back when he leaves.
I learned this from Deborah Kurata book, Doing Objects in VB2005
(just to give credit where credit is due).

To do this, I cycle through the controls on my forms and
use AddHandler to add the event handlers to each control.

If I add new controls of that type to the form,
they are automatically handled.

If I remove controls from the form, that is automatically
handled; I don't need to remove them from any Handles clauses.

Also, all controls of the same type are handled the same way.

I actually have this code in a base form, and all of my forms
inherit from it. That way, I have the same behavior on every
form in my application. (As one of my old teachers used to say,
"Right or wrong, be consistent.")

Here's the code. This is recursive to handle the cases where you
have controls within controls, like textboxes within panels. I
didn't try this out, but it's almost identical to my code that
does the textboxes.

In Form_Load, add this:  AddEventHandlers(me)

Private Sub AddEventHandlers(ByVal ctrlContainer As Control)
    For Each ctrl As Control In ctrlContainer.Controls
        If TypeOf ctrl Is Button Then
            'When the MouseHover event is raised for this button,
            '  run the Button_Hover routine.
            AddHandler ctrl.MouseHover, AddressOf Button_Hover
            'When the MouseLeave event is raised for this button,
            '  run the Button_Leave routine.
            AddHandler ctrl.MouseLeave, AddressOf Button_Leave
        End If
        'if control has children, call this function recursively
        If ctrl.HasChildren Then
            AddEventHandlers(ctrl)
        End If
    Next
End Sub

Public Sub Button_Hover(ByVal sender as Object, _
  ByVal e as System.EventArgs)
    DirectCast(sender, Control).BackColor = _
      Color.BlanchedAlmond
End Sub

Public Sub Button_Leave(ByVal sender as Object, _
  ByVal e as System.EventArgs)
    DirectCast(sender, Control).BackColor = _
      Color.Transparent
    'Note: you can also change this to one of
    ' the known colors, like the color of
    ' the buttonface:
    'DirectCast(sender, Control).BackColor = _
    '  Color.FromKnownColor(KnownColor.ButtonFace)
End Sub

What do you think?
Robin S.
------------------------------------------

Show quoteHide quote
"Henry Jones" <he***@yada.com> wrote in message
news:O7YEaAXFHHA.1252@TK2MSFTNGP02.phx.gbl...
>I thought it be cleaner to use one routine that handles many buttons than
>to create more code.  Better for maintenance later on.  Am I wrong?
> But that isn't the point.  I would like to know how to do it, now that I
> have thought of it.  I don't know if it's good programming practice, but
> that is what these forums are all about.
>
> Thanks,
>
> Henry
>
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
> news:%23uPFY1WFHHA.3268@TK2MSFTNGP04.phx.gbl...
>> Henry,
>>
>> I am interested. What makes it that you think nicer to use handles
>> instead of .Net methods?
>>
>> Cor
>>
>> You want to show that you are a starting programmer or something like
>> that?
>> "Henry Jones" <he***@yada.com> schreef in bericht
>> news:eboECJWFHHA.1248@TK2MSFTNGP02.phx.gbl...
>>>I have a VS 2005 VB.NET project and would like to change the color of the
>>>textbox when the user hovers over it.  In a Module I have the following
>>>routines:
>>>
>>> Public Sub Button_Hover(ByRef btnName As Button)
>>>
>>>    btnName.BackColor = Color.BlanchedAlmond
>>>
>>> End Sub
>>>
>>> Public Sub Button_Leave(ByRef btnName As Button)
>>>
>>>    btnName.BackColor = Color.Transparent
>>>
>>> End Sub
>>>
>>> ----------------------------------------------
>>>
>>>
>>>
>>> I called the routines from the following code:
>>>
>>>
>>>
>>> Private Sub cmdTierCancel_MouseHover(ByVal sender As Object, ByVal e As
>>> System.EventArgs) Handles cmdTierCancel.MouseHover
>>>
>>>    Button_Hover(cmdTierCancel)
>>>
>>> End Sub
>>>
>>> Private Sub cmdTierCancel_MouseLeave(ByVal sender As Object, ByVal e As
>>> System.EventArgs) Handles cmdTierCancel.MouseLeave
>>>
>>>    Button_Leave(cmdTierCancel)
>>>
>>> End Sub
>>>
>>> ---------------------------------
>>>
>>> SO FAR SO GOOD.  I thought it would be nice to use the Handles feature
>>> and call the routine like this:
>>>
>>> ---------------------------------
>>>
>>> Public Sub Handles_All_Buttons Handles(ByVal sender As Object, ByVal e
>>> As System.EventArgs)  cmd1.MouseHover, cmd2.MouseHover, cmd3.MouseHover
>>>
>>>    Button_Leave(sender)
>>>
>>> End Sub
>>>
>>>
>>>
>>> But this routine doesn't work.  Can anyone tell me what I am doing
>>> wrong?
>>>
>>>
>>>
>>> Thanks
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>
>>
>
>
Author
1 Dec 2006 6:43 PM
Henry Jones
Hi RobinS,

The code works like a charm.  Very cool.  If you were here in Sunny Los
Angeles, (maybe you are....dunno)  I'd buy you Starbucks Coffee.

Thanks,

Henry


Show quoteHide quote
"RobinS" <RobinS@NoSpam.yah.none> wrote in message
news:KaqdneO1Xsaa6e3YnZ2dnUVZ_tednZ2d@comcast.com...
> Henry,
>
> I have a routine in my base form that adds event handlers for
> mouseenter and mouseleave to textboxes. This changes the backcolor
> when the user enters the textbox, and changes it back when he leaves.
> I learned this from Deborah Kurata book, Doing Objects in VB2005
> (just to give credit where credit is due).
>
> To do this, I cycle through the controls on my forms and
> use AddHandler to add the event handlers to each control.
>
> If I add new controls of that type to the form,
> they are automatically handled.
>
> If I remove controls from the form, that is automatically
> handled; I don't need to remove them from any Handles clauses.
>
> Also, all controls of the same type are handled the same way.
>
> I actually have this code in a base form, and all of my forms
> inherit from it. That way, I have the same behavior on every
> form in my application. (As one of my old teachers used to say,
> "Right or wrong, be consistent.")
>
> Here's the code. This is recursive to handle the cases where you
> have controls within controls, like textboxes within panels. I
> didn't try this out, but it's almost identical to my code that
> does the textboxes.
>
> In Form_Load, add this:  AddEventHandlers(me)
>
> Private Sub AddEventHandlers(ByVal ctrlContainer As Control)
>    For Each ctrl As Control In ctrlContainer.Controls
>        If TypeOf ctrl Is Button Then
>            'When the MouseHover event is raised for this button,
>            '  run the Button_Hover routine.
>            AddHandler ctrl.MouseHover, AddressOf Button_Hover
>            'When the MouseLeave event is raised for this button,
>            '  run the Button_Leave routine.
>            AddHandler ctrl.MouseLeave, AddressOf Button_Leave
>        End If
>        'if control has children, call this function recursively
>        If ctrl.HasChildren Then
>            AddEventHandlers(ctrl)
>        End If
>    Next
> End Sub
>
> Public Sub Button_Hover(ByVal sender as Object, _
>  ByVal e as System.EventArgs)
>    DirectCast(sender, Control).BackColor = _
>      Color.BlanchedAlmond
> End Sub
>
> Public Sub Button_Leave(ByVal sender as Object, _
>  ByVal e as System.EventArgs)
>    DirectCast(sender, Control).BackColor = _
>      Color.Transparent
>    'Note: you can also change this to one of
>    ' the known colors, like the color of
>    ' the buttonface:
>    'DirectCast(sender, Control).BackColor = _
>    '  Color.FromKnownColor(KnownColor.ButtonFace)
> End Sub
>
> What do you think?
> Robin S.
> ------------------------------------------
>
> "Henry Jones" <he***@yada.com> wrote in message
> news:O7YEaAXFHHA.1252@TK2MSFTNGP02.phx.gbl...
>>I thought it be cleaner to use one routine that handles many buttons than
>>to create more code.  Better for maintenance later on.  Am I wrong?
>> But that isn't the point.  I would like to know how to do it, now that I
>> have thought of it.  I don't know if it's good programming practice, but
>> that is what these forums are all about.
>>
>> Thanks,
>>
>> Henry
>>
>> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
>> news:%23uPFY1WFHHA.3268@TK2MSFTNGP04.phx.gbl...
>>> Henry,
>>>
>>> I am interested. What makes it that you think nicer to use handles
>>> instead of .Net methods?
>>>
>>> Cor
>>>
>>> You want to show that you are a starting programmer or something like
>>> that?
>>> "Henry Jones" <he***@yada.com> schreef in bericht
>>> news:eboECJWFHHA.1248@TK2MSFTNGP02.phx.gbl...
>>>>I have a VS 2005 VB.NET project and would like to change the color of
>>>>the textbox when the user hovers over it.  In a Module I have the
>>>>following routines:
>>>>
>>>> Public Sub Button_Hover(ByRef btnName As Button)
>>>>
>>>>    btnName.BackColor = Color.BlanchedAlmond
>>>>
>>>> End Sub
>>>>
>>>> Public Sub Button_Leave(ByRef btnName As Button)
>>>>
>>>>    btnName.BackColor = Color.Transparent
>>>>
>>>> End Sub
>>>>
>>>> ----------------------------------------------
>>>>
>>>>
>>>>
>>>> I called the routines from the following code:
>>>>
>>>>
>>>>
>>>> Private Sub cmdTierCancel_MouseHover(ByVal sender As Object, ByVal e As
>>>> System.EventArgs) Handles cmdTierCancel.MouseHover
>>>>
>>>>    Button_Hover(cmdTierCancel)
>>>>
>>>> End Sub
>>>>
>>>> Private Sub cmdTierCancel_MouseLeave(ByVal sender As Object, ByVal e As
>>>> System.EventArgs) Handles cmdTierCancel.MouseLeave
>>>>
>>>>    Button_Leave(cmdTierCancel)
>>>>
>>>> End Sub
>>>>
>>>> ---------------------------------
>>>>
>>>> SO FAR SO GOOD.  I thought it would be nice to use the Handles feature
>>>> and call the routine like this:
>>>>
>>>> ---------------------------------
>>>>
>>>> Public Sub Handles_All_Buttons Handles(ByVal sender As Object, ByVal e
>>>> As System.EventArgs)  cmd1.MouseHover, cmd2.MouseHover, cmd3.MouseHover
>>>>
>>>>    Button_Leave(sender)
>>>>
>>>> End Sub
>>>>
>>>>
>>>>
>>>> But this routine doesn't work.  Can anyone tell me what I am doing
>>>> wrong?
>>>>
>>>>
>>>>
>>>> Thanks
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Author
1 Dec 2006 6:54 PM
RobinS
Good! I'm glad. I'm in the SF Bay Area, so it's a bit far
to drive for coffee!

Robin S.
-----------------------
Show quoteHide quote
"Henry Jones" <he***@yada.com> wrote in message
news:%23hAtaiXFHHA.928@TK2MSFTNGP05.phx.gbl...
> Hi RobinS,
>
> The code works like a charm.  Very cool.  If you were here in Sunny Los
> Angeles, (maybe you are....dunno)  I'd buy you Starbucks Coffee.
>
> Thanks,
>
> Henry
>
>
> "RobinS" <RobinS@NoSpam.yah.none> wrote in message
> news:KaqdneO1Xsaa6e3YnZ2dnUVZ_tednZ2d@comcast.com...
>> Henry,
>>
>> I have a routine in my base form that adds event handlers for
>> mouseenter and mouseleave to textboxes. This changes the backcolor
>> when the user enters the textbox, and changes it back when he leaves.
>> I learned this from Deborah Kurata book, Doing Objects in VB2005
>> (just to give credit where credit is due).
>>
>> To do this, I cycle through the controls on my forms and
>> use AddHandler to add the event handlers to each control.
>>
>> If I add new controls of that type to the form,
>> they are automatically handled.
>>
>> If I remove controls from the form, that is automatically
>> handled; I don't need to remove them from any Handles clauses.
>>
>> Also, all controls of the same type are handled the same way.
>>
>> I actually have this code in a base form, and all of my forms
>> inherit from it. That way, I have the same behavior on every
>> form in my application. (As one of my old teachers used to say,
>> "Right or wrong, be consistent.")
>>
>> Here's the code. This is recursive to handle the cases where you
>> have controls within controls, like textboxes within panels. I
>> didn't try this out, but it's almost identical to my code that
>> does the textboxes.
>>
>> In Form_Load, add this:  AddEventHandlers(me)
>>
>> Private Sub AddEventHandlers(ByVal ctrlContainer As Control)
>>    For Each ctrl As Control In ctrlContainer.Controls
>>        If TypeOf ctrl Is Button Then
>>            'When the MouseHover event is raised for this button,
>>            '  run the Button_Hover routine.
>>            AddHandler ctrl.MouseHover, AddressOf Button_Hover
>>            'When the MouseLeave event is raised for this button,
>>            '  run the Button_Leave routine.
>>            AddHandler ctrl.MouseLeave, AddressOf Button_Leave
>>        End If
>>        'if control has children, call this function recursively
>>        If ctrl.HasChildren Then
>>            AddEventHandlers(ctrl)
>>        End If
>>    Next
>> End Sub
>>
>> Public Sub Button_Hover(ByVal sender as Object, _
>>  ByVal e as System.EventArgs)
>>    DirectCast(sender, Control).BackColor = _
>>      Color.BlanchedAlmond
>> End Sub
>>
>> Public Sub Button_Leave(ByVal sender as Object, _
>>  ByVal e as System.EventArgs)
>>    DirectCast(sender, Control).BackColor = _
>>      Color.Transparent
>>    'Note: you can also change this to one of
>>    ' the known colors, like the color of
>>    ' the buttonface:
>>    'DirectCast(sender, Control).BackColor = _
>>    '  Color.FromKnownColor(KnownColor.ButtonFace)
>> End Sub
>>
>> What do you think?
>> Robin S.
>> ------------------------------------------
>>
>> "Henry Jones" <he***@yada.com> wrote in message
>> news:O7YEaAXFHHA.1252@TK2MSFTNGP02.phx.gbl...
>>>I thought it be cleaner to use one routine that handles many buttons than
>>>to create more code.  Better for maintenance later on.  Am I wrong?
>>> But that isn't the point.  I would like to know how to do it, now that I
>>> have thought of it.  I don't know if it's good programming practice, but
>>> that is what these forums are all about.
>>>
>>> Thanks,
>>>
>>> Henry
>>>
>>> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
>>> news:%23uPFY1WFHHA.3268@TK2MSFTNGP04.phx.gbl...
>>>> Henry,
>>>>
>>>> I am interested. What makes it that you think nicer to use handles
>>>> instead of .Net methods?
>>>>
>>>> Cor
>>>>
>>>> You want to show that you are a starting programmer or something like
>>>> that?
>>>> "Henry Jones" <he***@yada.com> schreef in bericht
>>>> news:eboECJWFHHA.1248@TK2MSFTNGP02.phx.gbl...
>>>>>I have a VS 2005 VB.NET project and would like to change the color of
>>>>>the textbox when the user hovers over it.  In a Module I have the
>>>>>following routines:
>>>>>
>>>>> Public Sub Button_Hover(ByRef btnName As Button)
>>>>>
>>>>>    btnName.BackColor = Color.BlanchedAlmond
>>>>>
>>>>> End Sub
>>>>>
>>>>> Public Sub Button_Leave(ByRef btnName As Button)
>>>>>
>>>>>    btnName.BackColor = Color.Transparent
>>>>>
>>>>> End Sub
>>>>>
>>>>> ----------------------------------------------
>>>>>
>>>>>
>>>>>
>>>>> I called the routines from the following code:
>>>>>
>>>>>
>>>>>
>>>>> Private Sub cmdTierCancel_MouseHover(ByVal sender As Object, ByVal e
>>>>> As System.EventArgs) Handles cmdTierCancel.MouseHover
>>>>>
>>>>>    Button_Hover(cmdTierCancel)
>>>>>
>>>>> End Sub
>>>>>
>>>>> Private Sub cmdTierCancel_MouseLeave(ByVal sender As Object, ByVal e
>>>>> As System.EventArgs) Handles cmdTierCancel.MouseLeave
>>>>>
>>>>>    Button_Leave(cmdTierCancel)
>>>>>
>>>>> End Sub
>>>>>
>>>>> ---------------------------------
>>>>>
>>>>> SO FAR SO GOOD.  I thought it would be nice to use the Handles feature
>>>>> and call the routine like this:
>>>>>
>>>>> ---------------------------------
>>>>>
>>>>> Public Sub Handles_All_Buttons Handles(ByVal sender As Object, ByVal e
>>>>> As System.EventArgs)  cmd1.MouseHover, cmd2.MouseHover,
>>>>> cmd3.MouseHover
>>>>>
>>>>>    Button_Leave(sender)
>>>>>
>>>>> End Sub
>>>>>
>>>>>
>>>>>
>>>>> But this routine doesn't work.  Can anyone tell me what I am doing
>>>>> wrong?
>>>>>
>>>>>
>>>>>
>>>>> Thanks
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Author
1 Dec 2006 8:08 PM
Herfried K. Wagner [MVP]
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb:
> I am interested. What makes it that you think nicer to use handles instead
> of .Net methods?

I feel sorry, but I do not understand what you are meaning...

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
1 Dec 2006 8:25 PM
Cor Ligthert [MVP]
Have a look what Robin wrote, than you understand it.
There is nothing more for me to add.

Cor

Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht
news:%23m4%237RYFHHA.536@TK2MSFTNGP02.phx.gbl...
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb:
>> I am interested. What makes it that you think nicer to use handles
>> instead of .Net methods?
>
> I feel sorry, but I do not understand what you are meaning...
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
1 Dec 2006 8:07 PM
Herfried K. Wagner [MVP]
"Henry Jones" <he***@yada.com> schrieb:
> Public Sub Button_Hover(ByRef btnName As Button)
>[...]
> Public Sub Button_Leave(ByRef btnName As Button)

'ByRef' => 'ByVal'.

>
>    btnName.BackColor = Color.Transparent
>
> End Sub
>[...]
> Public Sub Handles_All_Buttons Handles(ByVal sender As Object, ByVal e As
> System.EventArgs)  cmd1.MouseHover, cmd2.MouseHover, cmd3.MouseHover
>
>    Button_Leave(sender)
>
> But this routine doesn't work.  Can anyone tell me what I am doing wrong?

'Button_Leave(DirectCast(sender, Button))'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>