Asp.net 在VB.NET中,如何使文本框只允许一个小数点<;它前面有3个数字,后面只有1个数字?
正如问题所暗示的,我需要一个文本框,它只允许一个小数点,前面少于三个数字,后面只有一个数字 到目前为止,我已经编译了这段代码Asp.net 在VB.NET中,如何使文本框只允许一个小数点<;它前面有3个数字,后面只有1个数字?,asp.net,vb.net,Asp.net,Vb.net,正如问题所暗示的,我需要一个文本框,它只允许一个小数点,前面少于三个数字,后面只有一个数字 到目前为止,我已经编译了这段代码 Private Sub TextBox14_KeyPress(ByVal sender作为对象,ByVal e作为System.Windows.Forms.KeyPressEventArgs)处理TextBox14.KeyPress Dim keyChar=e.keyChar 如果Char.IsControl(keyChar),则 '允许所有控制字符。 ElseIf Ch
Private Sub TextBox14_KeyPress(ByVal sender作为对象,ByVal e作为System.Windows.Forms.KeyPressEventArgs)处理TextBox14.KeyPress
Dim keyChar=e.keyChar
如果Char.IsControl(keyChar),则
'允许所有控制字符。
ElseIf Char.IsDigit(keyChar)或lse keyChar=“”c然后
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)
如果Integer.TryParse(text,New Integer)和also text.Length>3,则
'拒绝长度超过16位的整数。
e、 已处理=真
ElseIf Double.TryParse(text,New Double)和also text.IndexOf(“.”c)
我遇到的最大问题是,用户可以输入多个小数点,然后基本上我创建的所有规则都消失了。此外,当我需要时,用户不能在小数点前设置2个数字。用我的头解决了这个问题 私有子textbox11keypress(ByVal sender作为对象,ByVal e作为System.Windows.Forms.KeyPressEventArgs)处理TextBox11.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.TextBox11.Text
Dim selectionStart = Me.TextBox11.SelectionStart
Dim selectionLength = Me.TextBox11.SelectionLength
text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)
If TextBox11.Text.Contains("."c) Then
'Forbids a user from entering in two decimal places
If keyChar = "."c Then
e.Handled = True
ElseIf text.Length - text.IndexOf("."c) > 3 Then
e.Handled = True
End If
Else 'no decimal point currently in textbox
If text.Length > 5 And keyChar = ("."c) Then 'Allows only a "." to be written
e.Handled = False
ElseIf text.Length > 5 Then ' Numbers before decimal point above 99,999
e.Handled = True
End If
End If
Else
'Reject all other characters for this textbox.
e.Handled = True
End If
End Sub
你应该学习正则表达式。它们是为这个量身定做的。我一年前做过,但我忘了它们是像^\d+(\.\d{1,2})这样的东西吗?$work?我将如何在代码中实现它?如果您不想处理模式,为什么要将它标记为C#?Regex或屏蔽文本框。