-1
正如問題所暗示的,我需要一個文本框來允許一個小數點,它之前少於三個數字,並且只有一個數字在它之後。在VB.NET中,如何讓文本框只允許一個小數點,小於3NUMBERS,之後只有1NUMBER?
到目前爲止我已經編譯了這段代碼。
Private Sub TextBox14_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox14.KeyPress
Dim keyChar = e.KeyChar
If Char.IsControl(keyChar) Then
'Allow all control characters.
ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
Dim text = Me.TextBox14.Text
Dim selectionStart = Me.TextBox14.SelectionStart
Dim selectionLength = Me.TextBox14.SelectionLength
text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)
If Integer.TryParse(text, New Integer) AndAlso text.Length > 3 Then
'Reject an integer that is longer than 16 digits.
e.Handled = True
ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 3 Then
'Reject a real number with two many decimal places.
e.Handled = True
End If
Else
'Reject all other characters.
e.Handled = True
End If
End Sub
我得到的最大問題是用戶可以輸入多個小數點,然後基本上我創建的所有規則都消失了。另外,當我想要它們時,用戶不能在小數點之前設置2個數字。
你應該學習正則表達式。他們是爲此量身定製的。 – fk2
我做了一年前,但我忘了他們 – 01923875v19028375v
會像^ \ d +(\。\ d {1,2})?$工作?我將如何將其實施到我的代碼? – 01923875v19028375v