[英]Allow only a decimal point after an input of three digits in a textbox in VB.NET?
How do I allow only a decimal point after an input of three digits in a textbox in VB.NET? 在VB.NET的文本框中输入三位数后,如何只允许小数点?
Let's say I inputed "123" after that I can only put a decimal else it wont allow any other input. 假设我输入了“123”,之后我只能输入小数,否则它不会允许任何其他输入。 So the result would be "123." 结果就是“123”。
Dim KeyAscii As Integer
KeyAscii = Asc(myE.KeyChar)
Select Case KeyAscii
Case Asc("0") To Asc("9"), Asc(ControlChars.Back)
myE.Handled = False
Case Asc(".")
If InStr(myTextbox.Text, ".") = 0 Then
myE.Handled = False
Else : myE.Handled = True
End If
Case myE.KeyChar = Chr(127)
myE.Handled = False
Case Else
myE.Handled = True
End Select
In WinForms you could accomplish this by using the TextChanged-Event of the Textbox and RegularExpressions: 在WinForms中,您可以通过使用Textbox和RegularExpressions的TextChanged-Event来完成此操作:
Example: 例:
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
'** Regex Pattern
Dim pattern As String = "^(([0-9]{1,3})|([0-9]{1,3}(\.){1,1}([0-9]){0,3}))$"
'** Copy of the Textbox Content
Dim strText As String = TextBox1.Text
'** Remove chars at the end of the string until the Textbox is empty or the contained chars are valid
While Not Regex.IsMatch(strText, pattern) AndAlso Not strText = ""
strText = strText.Substring(0, strText.Length - 1)
End While
'** Set the new text
TextBox1.Text = strText
'** Set the caret to the end of the string in the textbox
TextBox1.Select(TextBox1.Text.Length, 0)
End Sub
End Class
This example lets you write 123
, 345.
, 12.
, 123.1
, 123.123
and so on... 这个例子可以让你写123
, 345.
, 12.
, 123.1
, 123.123
等等...
To improve the number of digits before or after the decimal you can edit the {0,3}
in the pattern (The first 2 times are for the digits before the decimal and the third time for the digits after the decimal). 要改善小数点前后的位数,可以编辑模式中的{0,3}
(前两次是小数点前的数字,第三次是小数点后的数字)。 Just set instead of 3 the number of digits you like (or replace it with a *
or {0,}
for unlimited) 只需设置您喜欢的数字而不是3(或用*
或{0,}
代替无限制)
Hope this helps. 希望这可以帮助。
EDIT 1: 编辑1:
"^[0-9]{0,3}(\\.){0,1}$"
to "^(([0-9]{1,3})|([0-9]{1,3}(\\.){1,1}([0-9]){0,3}))$"
to allow digits after the decimal 将模式从"^[0-9]{0,3}(\\.){0,1}$"
更改为"^(([0-9]{1,3})|([0-9]{1,3}(\\.){1,1}([0-9]){0,3}))$"
允许小数点后的数字 Textbox1.Text = ""
to strText = ""
修正了While循环条件从Textbox1.Text = ""
到strText = ""
Try it with: 尝试使用:
Select Case myE.KeyChar
Case "0"c To "9"c, "."c
myE.Handled = InStr(myTextbox.Text, ".") > 0
Case ControlChars.Back, Convert.ToChar(127)
myE.Handled = False
Case Else
myE.Handled = True
End Select
Note: There is no point in converting the KeyChar to Integer and then use Asc() to compare. 注意:将KeyChar转换为Integer并使用Asc()进行比较是没有意义的。
EDIT : According to your comment the decimal point must be placed after the third digit and 2 or 3 more digits can follow. 编辑 :根据您的评论,小数点必须放在第三个数字后面,并且可以跟随2或3个数字。
Select Case myE.KeyChar
Case "0"c To "9"c
myE.Handled = myTextbox.Text.Length = 3 OrElse myTextbox.Text.Length >= 7
Case "."c
myE.Handled = myTextbox.Text.Length <> 3
Case ControlChars.Back, Convert.ToChar(127)
myE.Handled = False
Case Else
myE.Handled = True
End Select
Try this: 尝试这个:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged Dim wherePointIs As Integer = TextBox1.Text.IndexOf(".") If wherePointIs <> 3 Then 'Whats supposed to happen End If End Sub Private Sub TextBox1_TextChanged(ByVal sender As System.Object,ByVal e As System.EventArgs)处理TextBox1.TextChanged Dim wherePointIs As Integer = TextBox1.Text.IndexOf(“。”)if wherePointIs <> 3那么'什么应该发生结束If结束子
This will only check if there is a point at the point of three. 这只会检查三点是否有点。 You can change it so it checks if there is only ONE decimal point, etc. 您可以更改它,以便检查是否只有一个小数点,等等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.