对象引用未设置为对象的实例(数组文本框的keypresseventargs)

时间:2016-03-03 05:00:04

标签: arrays vb.net

我有一个boxpoamount,boxpounitprice和boxpoqty的数组文本框,我想验证它只会接受一个数字,但我在对象引用中得到一个错误而未设置为该行中对象的实例

 If Not (Char.IsDigit(e.KeyChar) Or Char.IsSymbol(e.KeyChar) Or Char.IsControl(e.KeyChar) Or (e.KeyChar = "." And boxpoqty.Text.IndexOf(".") < 0) Or (e.KeyChar = "-" And boxpoqty.Text.Length = 0)) Then

请帮助

这是完整的代码

      Private Sub loadpokeypressvalidation(ByRef boxpoamount As TextBox, ByRef boxpounitprice As TextBox, ByRef boxpoqty As TextBox)

  Dim e As KeyPressEventArgs
            If Not (Char.IsDigit(e.KeyChar) Or Char.IsSymbol(e.KeyChar) Or Char.IsControl(e.KeyChar) Or (e.KeyChar = "." And boxpoqty.Text.IndexOf(".") < 0) Or (e.KeyChar = "-" And boxpoqty.Text.Length = 0)) Then
                MessageBox.Show("Please enter numbers only")
                e.Handled = True
            End If
            If Not (Char.IsDigit(e.KeyChar) Or Char.IsSymbol(e.KeyChar) Or Char.IsControl(e.KeyChar) Or (e.KeyChar = "." And boxpoamount.Text.IndexOf(".") < 0) Or (e.KeyChar = "-" And boxpoamount.Text.Length = 0)) Then
                MessageBox.Show("Please enter numbers only")
                e.Handled = True
            End If
            If Not (Char.IsDigit(e.KeyChar) Or Char.IsSymbol(e.KeyChar) Or Char.IsControl(e.KeyChar) Or (e.KeyChar = "." And boxpounitprice.Text.IndexOf(".") < 0) Or (e.KeyChar = "-" And boxpounitprice.Text.Length = 0)) Then
                MessageBox.Show("Please enter numbers only")
                e.Handled = True
            End If
        End Sub

     Private Sub loadvalidkeypress()
            Dim controlall As Integer = Val(txtpoitemno.Text)

            For i As Integer = 0 To controlall - 1
                loadpokeypressvalidation(newpounitpricebox(i), newpoamountbox(i), newpoqtybox(i))
            Next

        End Sub

1 个答案:

答案 0 :(得分:1)

KeyPressEventArgs可能存在问题。

为什么不尝试使用Regex,如下所示

    If System.Text.RegularExpressions.Regex.IsMatch(TextboxEmail.Text, "[^0-9]") = False Then
      ' not valid
      ' ...
    End If