简体   繁体   中英

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?

Let's say I inputed "123" after that I can only put a decimal else it wont allow any other input. So the result would be "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:

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...

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). Just set instead of 3 the number of digits you like (or replace it with a * or {0,} for unlimited)

Hope this helps.

EDIT 1:

  • Changed pattern from "^[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
  • Corrected the While Loop Condition from Textbox1.Text = "" to 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.

EDIT : According to your comment the decimal point must be placed after the third digit and 2 or 3 more digits can follow.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM