2016-03-03 44 views
2

我有一個boxpoamount,boxpounitprice和boxpoqty的數組文本框,我想驗證它只接受一個數字,但即時通訊在對象中出錯引用不設置到對象的實例在此行對象引用未設置爲對象的實例(數組文本框的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 

請幫助

這是完整的代碼

 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 
+0

你必須在該行的錯誤多個條件。 你可以逐一檢查每個條件來檢查哪一個給你的錯誤? – nbadaud

+0

如果不是(Char.IsDigit(e.KeyChar)或Char.IsSymbol(e.KeyChar)或Char.IsControl(e.KeyChar)或(e.KeyChar =「。」和boxpoqty.Text.IndexOf(「。」) <0)或(e.KeyChar =「 - 」和boxpoqty.Text.Length = 0))然後這條線 –

+0

是否真的有必要驗證每個擊鍵?你真的需要用一個MessageBox來罵用戶嗎?驗證用戶點擊確定/更新/完成後,您將會更快樂。你應該使用AndAlso和OrElse代替And和或者 – Plutonix

回答

1

可能有問題與KeyPressEventArgs。

你爲什麼不使用正則表達式嘗試如下,

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

不工作..... –

+0

你需要遍歷你的文本框數組。而在循環內,你需要使用這個條件來確保文本框中只有數字。 –

+0

是的,我已經循環它,但這個代碼的東西是它仍然接受信。 –

相關問題