Yea, i've made a lot of threads xD well, im learning and im doing things for different projects.

Is there a shorter way to change the backcolor of the textboxes instead of doing this:

Private Sub TextBox1_GotFocus(sender As Object, e As EventArgs) Handles TextBox1.GotFocus
    Me.TextBox1.BackColor = Color.FromArgb(65, 65, 65)
End Sub

Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
    Me.TextBox1.BackColor = Color.FromArgb(60, 60, 60)
End Sub

So, is there a better way to change the color of many textboxes when it gets/lost focus ?

Recommended Answers

All 11 Replies

well i think , you can just make two public functions for got focus and lost focus , and just call them , this will reduce your code ,

Well yeah, but i still would have to create each handles X.GotFocus() and all that, there could be a better way...

I'm also looking for a solution

If you want to handle all textboxes the same way you can create two generic handlers such as

    Private Sub TextBox_GotFocus(sender As Object, e As System.EventArgs)
        sender.BackColor = Color.LightGray
    End Sub

    Private Sub TextBox_LostFocus(sender As Object, e As System.EventArgs)
        sender.BackColor = Color.White
    End Sub

then on form load you can attach these handlers to every textbox as

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        For Each ctrl As TextBox In Me.Controls.OfType(Of TextBox)()
            AddHandler ctrl.GotFocus, AddressOf TextBox_GotFocus
            AddHandler ctrl.LostFocus, AddressOf TextBox_LostFocus
        Next

    End Sub
commented: I laugh every time I see your picture. +5
commented: Thats what i needed +0

It didn't work, no effect at all but for example if i do this

Private Sub TextBox_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus
    sender.BackColor = Color.FromArgb(65, 65, 65)
End Sub

Works by adding all the handle, is this a good way to do it or what?

Hi iFrolox,

The code created by Reverend Jim works perfect.
Did you do exactly what Jim suggested to do or has your forms backcolor the same color as the gotfocus color.
Do you have any errors?

You can also create one function using Jim's method:

Private Sub FocusChanged(sender As Object, e As EventArgs)

    If CType(sender, TextBox).Focused = True Then
        CType(sender, TextBox).BackColor = Color.FromArgb(65, 65, 65)
    ElseIf CType(sender, TextBox).Focused = False Then
        CType(sender, TextBox).BackColor = Color.FromArgb(60, 60, 60)
    End If

End Sub

And just add the handlers to each textbox:

'Add this code in the form load event'

For Each txt As TextBox In Me.Controls
        AddHandler txt.GotFocus, AddressOf FocusChanged
        AddHandler txt.LostFocus, AddressOf FocusChanged
Next
commented: Thank you +2

Or a little cleaner

Private Sub FocusChanged(sender As Object, e As EventArgs)

    If CType(sender, TextBox).Focused Then
        CType(sender, TextBox).BackColor = Color.FromArgb(65, 65, 65)
    Else
        CType(sender, TextBox).BackColor = Color.FromArgb(60, 60, 60)
    End If

End Sub

or

Private Sub FocusChanged(sender As Object, e As EventArgs)

    If sender.Focused Then
        sender.Tag = sender.BackColor
        sender.BackColor = Color.LightGray
    Else
        sender.BackColor = sender.Tag
    End If

End Sub    

Which saves the current background colour before setting the focused colour (useful if not all of your textboxes are the same colour). If you are a purist you can do

Private Sub FocusChanged(sender As Object, e As EventArgs)

    Dim txt As TextBox = sender

    If txt.Focused Then
        txt.Tag = txt.BackColor
        txt.BackColor = Color.LightGray
    Else
        txt.BackColor = txt.Tag
    End If

End Sub

I have no idea what "phBAH.phTabControl" is. Start a new (blank) project. Add a few textboxes and a few other controls at random. Don't bother setting any properties or names other than the defaults. Now replace the existing code with the following:

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        For Each ctrl As TextBox In Me.Controls.OfType(Of TextBox)()
            AddHandler ctrl.GotFocus, AddressOf TextBox_GotFocus
            AddHandler ctrl.LostFocus, AddressOf TextBox_LostFocus
            ctrl.Tag = ctrl.BackColor
        Next

    End Sub

    Private Sub TextBox_GotFocus(sender As Object, e As System.EventArgs)
        Dim txt As TextBox = sender
        txt.BackColor = Color.LightGray
    End Sub

    Private Sub TextBox_LostFocus(sender As Object, e As System.EventArgs)
        Dim txt As TextBox = sender
        txt.BackColor = txt.Tag
    End Sub

End Class

After consideration I thought it was better to have separate handlers for "got" and "lost" focus. Just my opinion. In any case, try the above code. You can even set the text boxes to have different background colours at design time. The starting colour will be restored after the control loses focus.

commented: Works :) +0

Ok, i will make a new blank project and try.

Edit: Worked, i don't know why it didnt work in my other project...
Well anyways i just added Handles to each 1 that i needed, thank you all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.