PC Review


Reply
Thread Tools Rate Thread

Changing Color of Textbox on Hover

 
 
Henry Jones
Guest
Posts: n/a
 
      1st Dec 2006
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








 
Reply With Quote
 
 
 
 
Henry Jones
Guest
Posts: n/a
 
      1st Dec 2006
In the last routine, Button_Leave should be Button_Hover


"Henry Jones" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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
>
>
>
>
>
>
>
>



 
Reply With Quote
 
 
 
 
Tim Patrick
Guest
Posts: n/a
 
      1st Dec 2006
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

> 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
>



 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      1st Dec 2006
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" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...
>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
>
>
>
>
>
>
>
>



 
Reply With Quote
 
Henry Jones
Guest
Posts: n/a
 
      1st Dec 2006
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]" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> schreef in bericht
> news:(E-Mail Removed)...
>>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
>>
>>
>>
>>
>>
>>
>>
>>

>
>



 
Reply With Quote
 
RobinS
Guest
Posts: n/a
 
      1st Dec 2006
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" <(E-Mail Removed)> wrote in message
news:OTla$(E-Mail Removed)...
> In the last routine, Button_Leave should be Button_Hover
>
>
> "Henry Jones" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>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
>>
>>
>>
>>
>>
>>
>>
>>

>
>



 
Reply With Quote
 
Henry Jones
Guest
Posts: n/a
 
      1st Dec 2006
Yes, that was another "cut and paste" typo. In my code, it is correct.
Oops.



"RobinS" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:OTla$(E-Mail Removed)...
>> In the last routine, Button_Leave should be Button_Hover
>>
>>
>> "Henry Jones" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>>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
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
RobinS
Guest
Posts: n/a
 
      1st Dec 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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]" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> 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" <(E-Mail Removed)> schreef in bericht
>> news:(E-Mail Removed)...
>>>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
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
Henry Jones
Guest
Posts: n/a
 
      1st Dec 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>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]" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>>> 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" <(E-Mail Removed)> schreef in bericht
>>> news:(E-Mail Removed)...
>>>>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
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
RobinS
Guest
Posts: n/a
 
      1st Dec 2006
Good! I'm glad. I'm in the SF Bay Area, so it's a bit far
to drive for coffee!

Robin S.
-----------------------
"Henry Jones" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>>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]" <(E-Mail Removed)> wrote in message
>>> news:%(E-Mail Removed)...
>>>> 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" <(E-Mail Removed)> schreef in bericht
>>>> news:(E-Mail Removed)...
>>>>>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
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing text color and textbox color on Access Report Cindy Microsoft Access Reports 0 1st Dec 2010 06:15 PM
Creating a table cell and changing the color on hover. Mufasa Microsoft ASP .NET 2 3rd Jul 2008 08:06 PM
changing cursor hover color =?Utf-8?B?bmlnaHRoYXdrNDFfMQ==?= Windows Vista Performance 1 27th Jun 2007 02:39 AM
Changing Color of TextBox on Hover - With Correct Examples!!! Henry Jones Microsoft VB .NET 2 1st Dec 2006 07:33 PM
Changing the textbox value without changing the textbox format? =?Utf-8?B?T3pndXIgUGFycw==?= Microsoft Excel Programming 2 8th Sep 2006 06:41 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:01 AM.