2014-07-18 78 views
0

我有很多的文本框,我希望他們在重點時更改backColor。更改發件人TextBox的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 

如何獲得TextBox控件並更改其背景色?因爲我得到的發送者爲對象

+1

VB6? ((TextBox)sender).BackColor = System.Drawing.ColorTranslator.FromOle(&HFFFF9D)。根據您的需要添加錯誤處理。 – jac

+1

@jac儘管這是c#風格的含義是相同的。投射元素 – jscripter

+0

[Casting in Visual Basic?](http://stackoverflow.com/questions/748725/casting-in-visual-basic) –

回答