Visual Basic > Visual Basic Forums > Visual Basic General > Override GotFocus event
Ask a questionAsk a question
 

AnswerOverride GotFocus event

  • Saturday, November 07, 2009 10:59 AMBrunoLaurinec Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

  • Saturday, November 07, 2009 12:16 PMbdbodger Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
        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

  • Saturday, November 07, 2009 12:16 PMbdbodger Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
        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
    •  
  • Saturday, November 07, 2009 12:41 PMBrunoLaurinec Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Works like a charm. Thank you very much!!!! You saved my day :)