我有很多文本框,我希望他们在聚焦时更改backColor。
这是加载表单时的代码
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
这就是听众
Sub changeBg(sender As Object, e As EventArgs)
'Dim c As Control = sender
'sender.BackColor = System.Drawing.ColorTranslator.FromOle(&HFFFF9D)
End Sub
如何获取文本框控件并更改其backColor?因为我把发件人作为对象
答案 0 :(得分:2)
我最后为vb添加了正确的演员
Dim tb As TextBox = DirectCast(sender, TextBox)
tb.BackColor = System.Drawing.ColorTranslator.FromOle(&HFFFF9D)
正如它在这里做的那样
投射事件发件人以控制http://www.java2s.com/Tutorial/VB/0260__GUI/Casteventsendertocontrol.htm