Handler manipulation of controls

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello.

What I am tiring to accomplish is highlight and restore the backcolor of "a"
textbox when a cursor hits or leaves it with a general sub and a handler that
contains the fields I want to change. The code below gives you an idea of
what I would like to accomplish and it only demonstrates one cycle or event.
There would be in my thinking another general sub to return the textbox to
the original color.

Private Sub HighLight(ByVal Sender As Object, ByVal e As EventArgs) Handles
txt1.Enter, txt2.Enter
Sender.BackColor = Color.Aquamarine

End Sub

Thanks in Advance!
Brain Dead
 
In this case it might be easier to extend the textbox to add this
behavior and then in any case where you want the behavior use your
extended textbox.

Otherwise, create a helper class like:

class TextboxHighlighter
shared sub Wrap(tbox as textbox)
addhandler tbox.enter, me.enter
addhandler tbox.lostfocus, me.lostfocus
end sub

shared sub enter(sender as object, e as eventargs)
directcast(sender, textbox).backcolor = Color.Aquamarine
end sub

shared sub lostfocus(sender as object, e as eventargs)
directcast(sender, textbox).backcolor = color.white
end sub
end class


then for each textbox just call

TextboxHighlighter.Wrap(textbox)

if you want the original color instead of color.white you can store
the original colors in a hashtable. should really be able to store it
in a single variable since the lostfocus event of the active textbox
should never fire before the enter event of another, but I'd be sure
to test that assumption before suggesting that way.

Which you use (inheritance vs decoration) is a matter of preference.

HTH,

Sam
 
I'm doing exactly what Joseph wants to do with two little routines:

Friend Sub TBEnter(ByVal sender As Object, ByVal e As System.EventArgs)
DirectCast(sender, TextBox).BackColor = Color.GhostWhite
End Sub

Friend Sub TBLeave(ByVal sender As Object, ByVal e As System.EventArgs)
DirectCast(sender, TextBox).BackColor = Color.White
End Sub

Having already established the event handlers:
dim ctrl as Control
For Each ctrl in MyForm.Controls
If TypeOf ctrl is TextBox then
AddHandler ctrl.Leave, AddressOf TBLeave
AddHandler ctrl.Enter, AddressOf TBEnter
EndIf
Next


Terp
 

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

Back
Top