我有一個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
你必須在該行的錯誤多個條件。 你可以逐一檢查每個條件來檢查哪一個給你的錯誤? – nbadaud
如果不是(Char.IsDigit(e.KeyChar)或Char.IsSymbol(e.KeyChar)或Char.IsControl(e.KeyChar)或(e.KeyChar =「。」和boxpoqty.Text.IndexOf(「。」) <0)或(e.KeyChar =「 - 」和boxpoqty.Text.Length = 0))然後這條線 –
是否真的有必要驗證每個擊鍵?你真的需要用一個MessageBox來罵用戶嗎?驗證用戶點擊確定/更新/完成後,您將會更快樂。你應該使用AndAlso和OrElse代替And和或者 – Plutonix