调用成员事件 vb.net 2005 时 GroupBox 出现问题
我有这个类:
Public Class common
Public Function NumbersOnlyEvent(ByVal CtrlName As String, ByVal type As String, ByVal formName As Object) As String
Dim ctrlType As String = "System.Windows.Forms." & type
For Each objcontrol As Control In formName.Controls
If objcontrol.GetType.ToString = ctrlType And objcontrol.Name.Contains(CtrlName) Then
AddHandler objcontrol.KeyPress, AddressOf NumbersOnlyHandler
End If
Next
Return True
End Function 'NumbersOnlyEvent
Private Sub NumbersOnlyHandler(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
If Char.IsDigit(e.KeyChar) Or e.KeyChar = Chr(8) Then
e.Handled = False
Else
e.Handled = True
End If
End Sub
End Class
我有这个表单类:
Public Class addbet
Dim CommonFunc As common = New common
Private Sub addbet_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'AddHandler Events
CommonFunc.NumbersOnlyEvent("txtSbcNum", "TextBox", Me)
End Sub
End Class
我有这个文本框:
txtSbcNum1
txtSbcNum2
.
.
.
txtSbcNum15
当我通过在 Form_Load 上调用 CommonFunc.NumbersOnlyEvent("txtSbcNum", "TextBox", Me) 添加事件处理程序时,它工作正常。但是,当我将文本框放在 GroupBox 上时,它不起作用,但当我再次将其删除到 GroupBox 时,它会再次起作用。我错过了什么吗?
I have this class:
Public Class common
Public Function NumbersOnlyEvent(ByVal CtrlName As String, ByVal type As String, ByVal formName As Object) As String
Dim ctrlType As String = "System.Windows.Forms." & type
For Each objcontrol As Control In formName.Controls
If objcontrol.GetType.ToString = ctrlType And objcontrol.Name.Contains(CtrlName) Then
AddHandler objcontrol.KeyPress, AddressOf NumbersOnlyHandler
End If
Next
Return True
End Function 'NumbersOnlyEvent
Private Sub NumbersOnlyHandler(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
If Char.IsDigit(e.KeyChar) Or e.KeyChar = Chr(8) Then
e.Handled = False
Else
e.Handled = True
End If
End Sub
End Class
And i have this Form Class:
Public Class addbet
Dim CommonFunc As common = New common
Private Sub addbet_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'AddHandler Events
CommonFunc.NumbersOnlyEvent("txtSbcNum", "TextBox", Me)
End Sub
End Class
And i have this textboxes:
txtSbcNum1
txtSbcNum2
.
.
.
txtSbcNum15
When I add an eventhandler by calling CommonFunc.NumbersOnlyEvent("txtSbcNum", "TextBox", Me) on Form_Load, it works fine. But when I place my textboxes on a GroupBox, it doesn't work but when I remove it again to GroupBox it works again. Did i missed something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是表单控件不包含子控件。
因此,可以在表单上找到组框,但在文本框上找不到,因为它是组框的子项。
将此行:更改
为:,
以便它可以在组框中找到文本框。
The problem is that the form controls don't include child controls.
So, the group box is found on the form but not the text box, since it is a child of the group box.
Change this line:
to this:
so that it will find the text box in the group box.