调用成员事件 vb.net 2005 时 GroupBox 出现问题

发布于 2024-12-03 19:17:07 字数 1375 浏览 1 评论 0原文

我有这个类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘初心 2024-12-10 19:17:07

问题是表单控件不包含子控件。
因此,可以在表单上找到组框,但在文本框上找不到,因为它是组框的子项。

将此行:更改

CommonFunc.NumbersOnlyEvent("txtSbcNum", "TextBox", Me)

为:,

CommonFunc.NumbersOnlyEvent("txtSbcNum", "TextBox", Me.GroupBox1)

以便它可以在组框中找到文本框。

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:

CommonFunc.NumbersOnlyEvent("txtSbcNum", "TextBox", Me)

to this:

CommonFunc.NumbersOnlyEvent("txtSbcNum", "TextBox", Me.GroupBox1)

so that it will find the text box in the group box.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文