Click here to Skip to main content
15,922,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
I have made a project. It is almost completed. Now query is raised that when TextBox GotFocus, its back color should be changed and when it lostFocus, it shuld be normal.

Many more TextBox are on form. Is there any coding to resolve it without disturbing to one-by-one ?

Please reply...
Posted

In VB.NET you can have a single function handle several events - you don't have to double-click on every text box or anything like that :-)

I created a form with 6 textboxes on it (TextBox1, TextBox2 etc) then used these two functions to handle all of the Enter (GotFocus) and Leave (LostFocus) events

VB
Private Sub TextBoxX_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Enter, TextBox2.Enter, TextBox3.Enter, TextBox4.Enter, TextBox5.Enter, TextBox6.Enter
    Dim tb As TextBox = sender
    tb.BackColor = Color.Tomato
End Sub
Private Sub TextBoxX_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave, TextBox2.Leave, TextBox3.Leave, TextBox4.Leave, TextBox5.Leave, TextBox6.Leave
    Dim tb As TextBox = sender
    tb.BackColor = Color.Violet
End Sub

If you need more then just add the text box events at the end of the list e.g.
Handles TextBox1.Enter, TextBox2.Enter, TextBox3.Enter, TextBox4.Enter, TextBox5.Enter, TextBox6.Enter ,TextBox7.Enter 


If you hit any limits on the number of handlers you can add then just create another pair of handlers that call these ones e.g.
VB
Private Sub T6_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.Leave
    TextBoxX_Leave(sender, e)
End Sub
Private Sub T8_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.Enter
    TextBoxX_Enter(sender, e)
End Sub
 
Share this answer
 
v2
Comments
CHill60 24-Apr-14 11:44am    
Intrigued about the downvote... always happy to have my errors pointed out but just downvoting doesn't help me understand what is wrong with this solution!
Richard Deeming 24-Apr-14 12:24pm    
Your answer requires that the code is compiled with Option Strict Off. You should probably add a DirectCast to your handlers:

Dim tb As TextBox = DirectCast(sender, TextBox)

(The downvote wasn't me, BTW.)
CHill60 24-Apr-14 15:25pm    
Ah...thank you - much appreciated. Need to set that as default!
Check this fiddle it is for border color just make it for backcolor (if this is a web app)

Fiddle
 
Share this answer
 
v2
Comments
CHill60 24-Apr-14 7:17am    
OP is using VB.NET!
Try this if it is a desktop application

VB
Private Sub Text_GotFocus()

    Me.Text.BackStyle = normal

End Sub
Private Sub Text_LostFocus()

    Me.Text.BackStyle = Transparent

End Sub
 
Share this answer
 
Comments
CHill60 24-Apr-14 7:16am    
This looks like VB6 not VB.NET ... and only works for one text box not all of them
VB
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim tbx As TextBox
        For Each tbx In Me.Controls
            AddHandler tbx.MouseEnter, AddressOf tbx_MouseEnter
            AddHandler tbx.MouseLeave, AddressOf tbx_MouseLeave
        Next
    End Sub

    Private Sub tbx_MouseEnter(sender As Object, e As EventArgs)
        sender.Backcolor = Color.Red
    End Sub

    Private Sub tbx_MouseLeave(sender As Object, e As EventArgs)
        sender.Backcolor = Color.White
    End Sub
End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900