Only accept numbers for text field

I found this code in order to make the text field only the accepted number.

Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim allowedChars As String = "0123456789" If allowedChars.IndexOf(e.KeyChar) = -1 Then ' Invalid Character e.Handled = True End If End Sub 

But ... the user cannot delete numbers using the backspace button. How can I do it?

+7
source share
8 answers

You also need to process the inserted text (there may not be a keystroke). The best way to do this is MaskedTextBox .

+7
source
  Private Sub txtValue_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtValue.KeyPress 'Dim allowedChars As String = "0123456789" 'If allowedChars.IndexOf(e.KeyChar) = -1 Then ' ' Invalid Character ' e.Handled = True 'End If 'If (e.KeyChar = Microsoft.VisualBasic.Chr(8)) Then ' e.Handled = True 'End If If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then e.Handled = True End If End Sub 
+8
source

voldemort

I designed my first code to allow the user to delete as well.

Here is the code:

 Dim BACKSPACE As Boolean Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown If e.KeyCode = Keys.Back Then BACKSPACE = True Else BACKSPACE = False End If End Sub Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If BACKSPACE = False Then Dim allowedChars As String = "0123456789" If allowedChars.IndexOf(e.KeyChar) = -1 Then e.Handled = True End If End If End Sub 

I hope my code was useful to you :)

+3
source

Use this code, it will help you.

 Public Function OnlyDigitsOnKeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Try If System.Char.IsDigit(e.KeyChar) = False And e.KeyChar <> Microsoft.VisualBasic.Chr(8) And e.KeyChar <> Microsoft.VisualBasic.Chr(46) Or (InStr(sender.text, ".") > 0 And e.KeyChar = Microsoft.VisualBasic.Chr(46)) Then e.Handled = True End If Catch ex As Exception Common.ErrorHandler(ex) End Try End Function 
+1
source

When I had an input request that only accepts numbers, I usually used the NumericUpDown class. It also handles limits and decimals.

+1
source
  Private Sub txtValue_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtValue.KeyPress Dim allowedChars As String = "." 'If allowedChars.IndexOf(e.KeyChar) = -1 Then ' ' Invalid Character ' e.Handled = True 'End If 'If (e.KeyChar = Microsoft.VisualBasic.Chr(8)) Then ' e.Handled = True 'End If If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False And allowedChars.IndexOf(e.KeyChar) = -1 Then e.Handled = True End If End Sub 
0
source
 Private Sub TMarksTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TMarksTextBox.KeyPress If e.KeyChar < "0" OrElse e.KeyChar > "9" AndAlso e.KeyChar <> ControlChars.Back Then e.Handled = True End If End Sub 
0
source

Here is the code I wrote. It allows the user to delete, and the user can make the text field empty if he wants. It processes when the user enters a forbidden character, and also processes when the user inserts text into a text field. If the user inserts a string into a field that is a combination of valid and invalid characters, the valid characters will be displayed in the text box, and the characters will not be invalid.

It also has logic to ensure that the cursor behaves normally. (The problem with setting the text to a new value is that the cursor moves to the beginning. This code tracks the starting position and makes adjustments to the account for any invalid characters that are deleted.)

This code can be placed in the TextChaned event of any text field. Remember to change the name from TextBox1 to match your text box.

  Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged Dim selStart As Integer = TextBox1.SelectionStart Dim selMoveLeft As Integer = 0 Dim newStr As String = "" 'Build a new string by copying each valid character from the existing string. The new string starts as blank and valid characters are added 1 at a time. For i As Integer = 0 To TextBox1.Text.Length - 1 If "0123456789".IndexOf(TextBox1.Text(i)) <> -1 Then 'Characters that are in the allowed set will be added to the new string. newStr = newStr & TextBox1.Text(i) ElseIf i < selStart Then 'Characters that are not valid are removed - if these characters are before the cursor, we need to move the cursor left to account for their removal. selMoveLeft = selMoveLeft + 1 End If Next TextBox1.Text = newStr 'Place the new text into the textbox. TextBox1.SelectionStart = selStart - selMoveLeft 'Move the cursor to the appropriate location. End Sub 

Note. If you need to do this for a bunch of text fields, you can create a general-purpose version by creating a subsection that takes a link to the text field as a parameter. Then you only need to call sub from the TextChanged event.

0
source

All Articles