473,322 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Handlers

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

Nov 21 '05 #1
9 1150
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
"Joseph" <Jo****@discussions.microsoft.com> wrote in message
news:CB**********************************@microsof t.com...
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


Nov 21 '05 #2
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

"Joseph" <Jo****@discussions.microsoft.com> wrote in message
news:CB**********************************@microsof t.com...
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

Nov 21 '05 #3
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.

"Joseph" wrote:
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

Nov 21 '05 #4
Joseph,

See this sample I made two days ago and sent too this newsgroup to Stefan

http://groups-beta.google.com/group/...22186d980ed9fe

I hope this helps?

Cor
Nov 21 '05 #5
"Joseph" <Jo****@discussions.microsoft.com> schrieb:
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
///

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

Nov 21 '05 #6
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/...22186d980ed9fe

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
"Joseph" <Jo****@discussions.microsoft.com> wrote in message
news:CB**********************************@microsof t.com...
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

Nov 21 '05 #7
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

"Joseph" wrote:
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

Nov 21 '05 #8
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
Nov 21 '05 #9
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.
"Cor Ligthert" <no************@planet.nl> wrote in message
news:e2****************@tk2msftngp13.phx.gbl...
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


Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jeff Shannon | last post by:
I'm having some difficulty getting my logging configuration set correctly. I'm using a config file (copied at end of post), with the intent of setting several loggers which write to a combination...
0
by: Vinay Sajip | last post by:
Currently, if the logging module is used with no handlers configured for a logger or its parents and you try to log events with that logger, a single message is printed to sys.stderr: No...
10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
9
by: Charles Law | last post by:
I have a form on which user controls are placed at runtime. When a control is added to the form a handler is added for an event that a high-level object raises, which must be handled by the new...
13
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the...
4
by: sm | last post by:
Hi, I have a couple of questions with regards to handling errors and exceptions. 1. If I use On Error goto Errhandler ... Errhandler:
1
by: bneron | last post by:
hello, I use the logging module include in Python 2.4 distribution, and I 'd like to have a logger witth several Handlers . I know how to do this by coding in python, but could I specify this...
16
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some...
14
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event...
3
by: Chris Shenton | last post by:
I am setting up handlers to log DEBUG and above to a rotating file and ERROR and above to console. But if any of my code calls a logger (e.g., logging.error("foo")) before I setup my handlers, the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.