方法调用验证文本框时出错

时间:2015-11-14 10:50:51

标签: vb.net

我想调用方法来验证文本框,但会发生以下崩溃:

Private e As Object

Public Sub ValidX_keyPress()
    If Not Char.IsNumber(e.keychar) And Not Char.IsControl(e.keychar) Then
        MsgBox("please")
        e.handled = True
    End If
End Sub

Private Sub tbxOne_KeyPress(sender As Object, e As KeyPressEventArgs) Handles tbxOne.KeyPress
    ValidX_keyPress()
End Sub
  

崩溃:附加信息:对象变量或未设置块变量。

1 个答案:

答案 0 :(得分:1)

您在ValidX_keyPress方法中使用e,但没有e。看看解决方案:

    Private e As Object
    Public Sub ValidX_keyPress(e As KeyPressEventArgs)
        If Not Char.IsNumber(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
            MsgBox("please")
            e.Handled = True
        End If
    End Sub
    Private Sub tbxOne_KeyPress(sender As Object, e As KeyPressEventArgs) Handles tbxOne.KeyPress
        ValidX_keyPress(e)
    End Sub

您需要在ValidX_keyPress中将e定义为参数,然后在调用方法时提供e。