jscripter
jscripter

Reputation: 840

Change backColor of Sender TextBox

I have a lot of textboxes which i wanted them to change the backColor when focused.

This is the code when load the form

Public Sub New()
    Me.InitializeComponent()
    For Each c  As Control In Me.Controls
        If TypeOf c Is TextBox Then 
            AddHandler c.Enter, AddressOf Me.changeBg
            AddHandler c.Leave, AddressOf Me.restoreBg
        End If
    Next
End Sub

And this the listener

Sub changeBg(sender As Object, e As EventArgs)
    'Dim c As Control = sender
    'sender.BackColor = System.Drawing.ColorTranslator.FromOle(&HFFFF9D)
End Sub

How to get the textbox control and change its backColor? because I get the sender as an object

Upvotes: 0

Views: 568

Answers (1)

jscripter
jscripter

Reputation: 840

I finally added a correct casting for vb

Dim tb As TextBox = DirectCast(sender, TextBox)
tb.BackColor  = System.Drawing.ColorTranslator.FromOle(&HFFFF9D)

As it do it here

Cast event sender to control http://www.java2s.com/Tutorial/VB/0260__GUI/Casteventsendertocontrol.htm

And here Casting in visual basic?

Upvotes: 2

Related Questions