Override GotFocus event
- Hi Guys, I would like to somehow override the "GotFocus" and "LosFocus" event of the textbox.
Here's the deal. I have big registration form with +- 200 TextBoxes and I want the one that has focus to be highlighter [e.g. let's say change the background color to yellow]. I know, I could write the same code for 200 TextBoxes, but that would be time-killing and I'm really not in favour writing the same thing 200 times as well as with such a huge amount of copy-paste I'm almost sure I would make mistake somewhere.
Could you please help me on this ?
Answers
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For Each c As Control In Controls If TypeOf c Is TextBox Then AddHandler c.Enter, AddressOf TextBox_Enter AddHandler c.Leave, AddressOf TextBox_Leave End If Next End Sub Private Sub TextBox_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) CType(sender, TextBox).BackColor = Color.Yellow End Sub Private Sub TextBox_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) CType(sender, TextBox).BackColor = Color.White End Sub
coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
Please format the code in your posts with the
button . Makes it easier to read .- Marked As Answer byBrunoLaurinec Saturday, November 07, 2009 12:40 PM
All Replies
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For Each c As Control In Controls If TypeOf c Is TextBox Then AddHandler c.Enter, AddressOf TextBox_Enter AddHandler c.Leave, AddressOf TextBox_Leave End If Next End Sub Private Sub TextBox_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) CType(sender, TextBox).BackColor = Color.Yellow End Sub Private Sub TextBox_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) CType(sender, TextBox).BackColor = Color.White End Sub
coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
Please format the code in your posts with the
button . Makes it easier to read .- Marked As Answer byBrunoLaurinec Saturday, November 07, 2009 12:40 PM
- Works like a charm. Thank you very much!!!! You saved my day :)


