我试图为窗体上的某些控件捕获特定事件。我试过了,但是它没有触发事件
For Each ctrl As Control In pnlGeneral.Controls
If TypeOf ctrl Is CheckBox Then
AddHandler (DirectCast(ctrl, CheckBox).CheckedChanged), AddressOf Control_Changed
ElseIf TypeOf ctrl Is TextBox Then
AddHandler (ctrl.TextChanged), AddressOf Control_Changed
ElseIf TypeOf ctrl Is NumericUpDown Then
AddHandler (DirectCast(ctrl, NumericUpDown).ValueChanged), AddressOf Control_Changed
End If
Next
谁能帮我解决这个问题?编辑我发现我将控件放置在多个控件中,窗体上是一个面板,面板内是不同的组框,这些组框有控件,我试图捕获它们的事件
所以,正如Lars所说,它似乎确实有效。
在适当位置编辑以说明容器中的控件
Private Sub Form1_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
Dim ctrl As Control = Me.GetNextControl(Me, True)
Do Until ctrl Is Nothing
If TypeOf ctrl Is CheckBox Then
AddHandler (DirectCast(ctrl, CheckBox).CheckedChanged), AddressOf Control_Changed
ElseIf TypeOf ctrl Is TextBox Then
AddHandler (ctrl.TextChanged), AddressOf Control_Changed
ElseIf TypeOf ctrl Is NumericUpDown Then
AddHandler (DirectCast(ctrl, NumericUpDown).ValueChanged), AddressOf Control_Changed
End If
ctrl = Me.GetNextControl(ctrl, True)
Loop
End Sub
Private Sub Control_Changed(sender As System.Object, e As System.EventArgs)
Debug.WriteLine(sender.ToString)
End Sub
Private Sub Form_Activate(Byval sender As Object,Byval e as System.EventArgs) Handles Form.Activate
Dim Ctrl as System.Windows.Forms.Control
For Each Ctrl in Me.Controls
If Typeof Ctrl Is System.Windows.Forms.TextBox then
AddHandler Ctrl.GotFocus, AddressOf TextFocus
AddHandler Ctrl.LostFocus, AddressOf TextUnfocus
Next
End Sub
Private Sub TextFocus(ByVal sender As Object, ByVal e As System.EventArgs)
CType(sender,System.Windows.Forms.TextBox).BackColor = Color.Blue
End Sub
Private Sub TextUnfocus(ByVal sender As Object, ByVal e As System.EventArgs)
CType(sender, System.Windows.Forms.TextBox).BackColor = Color.White
End Sub