【问题标题】:Allow only a decimal point after an input of three digits in a textbox in VB.NET?在VB.NET的文本框中输入三位数后只允许小数点?
【发布时间】:2011-11-09 14:13:26
【问题描述】:

如何在VB.NET的文本框中输入三位数后只允许小数点?

假设我输入了“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

【问题讨论】:

标签: vb.net winforms


【解决方案1】:

试试看:

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

注意:将 KeyChar 转换为 Integer 再使用 Asc() 进行比较是没有意义的。

编辑:根据您的评论,小数点必须放在第三位数字之后,并且可以再多 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

【讨论】:

    【解决方案2】:

    在 WinForms 中,您可以通过使用文本框的 TextChanged-Event 和正则表达式来完成此操作:

    例子:

    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
    

    这个例子可以让你写123345.12.123.1123.123等等......

    要提高小数点前后的位数,您可以在模式中编辑{0,3}(前两次是小数点前的数字,第三次是小数点后的数字)。只需设置您喜欢的位数而不是 3(或将其替换为 *{0,} 以表示无限制)

    希望这会有所帮助。

    编辑 1:

    • 将模式从 "^[0-9]{0,3}(\.){0,1}$" 更改为 "^(([0-9]{1,3})|([0-9]{1,3}(\.){1,1}([0-9]){0,3}))$" 以允许小数点后的数字
    • 将 While 循环条件从 Textbox1.Text = "" 更正为 strText = ""

    【讨论】:

    • 这太棒了!但我还需要它在小数点后输入两个或三个数字......
    • @Kurusu:在您的问题中,您说“如何在输入三位数字后只允许小数点”。但是现在你说小数点后一定可以输入更多的数字。所以你的问题不是很清楚。
    【解决方案3】:

    试试这个:

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 TextBox1.TextChanged Dim wherePointIs As Integer = TextBox1.Text.IndexOf(".") 如果 wherePointIs 3 那么 '应该发生什么 万一 结束子

    这只会检查三点是否有一个点。您可以更改它以检查是否只有一个小数点等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-30
      • 2013-04-12
      • 1970-01-01
      • 2013-01-22
      相关资源
      最近更新 更多