Handlers

G

Guest

Hello,

Here is my solution below that works but can be improved upon. The
improvement that I seek is how to define the handler that handles the txt1
and txt2 controls in an automated fashion. If I add three more textboxes to
my winform, I do not want to have to change the hadler evertime I make
changes.


Private txtColor As Color = Color.LightSkyBlue
Private txtOrig As Color

Private Sub HighLight(ByVal Sender As Object, ByVal e As EventArgs) Handles
txt1.Enter, txt2.Enter
txtOrig = DirectCast(Sender, TextBox).BackColor()
DirectCast(Sender, TextBox).BackColor = txtColor
End Sub

Private Sub LowLight(ByVal Sender As Object, ByVal e As EventArgs) Handles
txt1.Leave, txt2.Leave
DirectCast(Sender, TextBox).BackColor = txtOrig
End Sub


Thanks in Advance!
Joseph
 
D

David A. Osborn

If I'm understanding you correctly you can do this to dynimcally add a
handler

AddHandler txt3.Enter, AddressOf HighLight
AddHandler txt3.Leave, AddressOf LowLight
 
C

Chris, Master of All Things Insignificant

I don't have the code in front of me, but what you need to do is loop
through the controls in the form. Check to see if they are of type textbox
and then do the handlers.

Dim ctr As Control
For Each ctr In Me.Controls
If TypeOf ctr Is TextBox Then
MessageBox.Show(ctr.Text)
'Add Handler for Ctr here
End If
Next

Another way to do it, and the way I prefer is to write a class that inherits
from textbox. Then override the events you want to handle. This way you
only write the class once and everytime it is added to your form, it is
taken care of. No need to add handlers!

Chris
 
G

Guest

Everyone,

This is a step through sub in the gui. If a user tabs or mouse clicks or
whatever(focus) to a particlular textbox, it changes backcolor. When the
user goes to the next textbox or just plain leaves the textbox, it returns
the texbox backcolor to normal and if focus is applied to another textbox,
then its backcolor is changed. I am tring to get around naming all the
textboxes in the two subs in the first post - look at the handler.
 
H

Herfried K. Wagner [MVP]

Joseph said:
Here is my solution below that works but can be improved upon. The
improvement that I seek is how to define the handler that handles the txt1
and txt2 controls in an automated fashion. If I add three more textboxes
to
my winform, I do not want to have to change the hadler evertime I make
changes.

Maybe you could write an extender component (see documentation) that extends
controls with this behavior. This would make the code more reusable.
Alternatively you can create your own textbox control that inherits from the
standard Windows Forms textbox. Quick and dirty (overriding 'OnEnter' and
'OnLeave' would be a cleaner solution):

\\\
Public Class FooBar
Inherits TextBox

Private Sub FooBar_Enter( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles MyBase.Enter
Me.BackColor = Color.Yellow
End Sub

Private Sub FooBar_Leave( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles MyBase.Leave
Me.BackColor = Color.White
End Sub
End Class
///
 
J

Jay B. Harlow [MVP - Outlook]

Joseph,
I would use something like what Herfried showed, for other options I would
consider see my response in the entire thread that Cor referenced:
http://groups-beta.google.com/group..._doneTitle=Back+to+Search&&d#0222186d980ed9fe

Here is a more complete version of what Herfried shows originally based on
the above thread:

Imports System.ComponentModel

Public Class TextBoxEx
Inherits TextBox

Public Shared ReadOnly Property DefaultFocusColor() As Color
Get
Return SystemColors.Info
End Get
End Property

Public Event FocusColorChanged As EventHandler

Private m_focusColor As Color
Private m_backColor As Color

Public Sub New()
m_focusColor = DefaultFocusColor
m_backColor = MyBase.BackColor
End Sub

<Category("Appearance"), _
Description("The background color used when the control has focus")> _
Public Property FocusColor() As Color
Get
Return m_focusColor
End Get
Set(ByVal value As Color)
m_focusColor = value
OnFocusColorChanged(EventArgs.Empty)
End Set
End Property

Protected Overridable Sub OnFocusColorChanged(ByVal e As EventArgs)
RaiseEvent FocusColorChanged(Me, e)
End Sub

Public Sub ResetFocusColor()
m_focusColor = DefaultFocusColor
End Sub

Public Function ShouldSerializeFocusColor() As Boolean
Return Not m_focusColor.Equals(DefaultFocusColor)
End Function

Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
MyBase.OnEnter(e)
MyBase.BackColor = m_focusColor
End Sub

Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)
MyBase.OnLeave(e)
MyBase.BackColor = m_backColor
End Sub

Protected Overrides Sub OnBackColorChanged(ByVal e As System.EventArgs)
MyBase.OnBackColorChanged(e)
If MyBase.BackColor.Equals(m_focusColor) Then Exit Sub
m_backColor = MyBase.BackColor
End Sub

End Class


On your form instead of using TextBox controls you would use the above
TextBoxEx controls.

Hope this helps
Jay
 
G

Guest

Thanks David, Chris, Cor, Herfried and Jay!

I used a variant of Cors' and Herfieds' solutions and placed into a static
oops, shared class. I just pass the two colors and the parent controls name.
Thanks again!

Joseph
 
C

Cor Ligthert

Joseph,

I found that shared class not so nice to use because there is no need for
it. It was my first thought when I created it. However the only thing it has
to do is to set the event handlers and than it can be cleaned up by the GC.

Cor
 
J

Jim Burns

I just wrote an extender control you drop it on the form and every textbox
has enable Color_Change and a set Enter_Color
If you want the code Ill post it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top