AM尝试为表单上的某些控件捕获特定事件。我试过这个,但它没有激发事件
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
任何人都可以帮我解决这个问题吗?
修改
我发现将控件置于多个控件中,在窗体上是一个面板,面板内部是不同的groupbox,这些组框有控件我试图捕获他们的事件
答案 0 :(得分:4)
因为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
答案 1 :(得分:0)
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