I am trying to validate phone number textbox. I just want user to be able to enter only numeric values and only 10 digits in the textbox. I did it using keypress event.
it works fine but the problem is once the length of the input reaches to 10, it wont even allow the backspace.
Here is my code -
Private Sub tbphone_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tbphone.KeyPress
If Not (Char.IsDigit(CChar(CStr(e.KeyChar))) Or e.KeyChar = ChrW(Keys.Back)) Or tbphone.Text.Length >= 10 Then e.Handled = True
End Sub
Now, there can be a scenario where user enters all digits correctly but the last one. In that case he will not be able delete that last digit perhaps he will not be able to do anything in that textbox as the length of the input text is already 10 and now the e.handled is set to true.
Please suggest how can I achieve it...
Disclaimer: I have not touched vb.net in quite some time, say about a year. I am just providing a solution which I used previously and found it helpful.
You may refer to: This question has a similar issue and has been answered.
Regarding your 10 character limit, you can set the MaxLength property of the textbox either via the graphical editor or code.
You may refer to: How to set the textbox MaxLength property. (Please change the language to VB at the top of the page.)
I actually found the solution to my own question, I am putting it here so if anyone stuck validating textboxes with the same issue might get help.
I have set the e.handled to false if user presses BackSpace. And it worked...
Private Sub tbphone_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tbphone.KeyPress
If Not Char.IsDigit(CChar(CStr(e.KeyChar))) Or tbphone.Text.Length >= 10 Then e.Handled = True
If e.KeyChar = ChrW(Keys.Back) Then e.Handled = False
End Sub
i know it is already solved, but if i may add an answer.
If (e.KeyChar <= ChrW(47)) Or (e.KeyChar >= ChrW(58)) Then
If e.KeyChar = vbBack Then
Else
e.KeyChar = ChrW(0)
End If
Else
End If
i use this code in keypress event
Related
Trying to make a validation where only letters can be entered into a textbox. I've got that part working, but I would also like the ability for the user to use the backspace button, and I'm not sure how
Private Sub TxtBoxCustForename_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TxtBoxCustForename.KeyPress
If Asc(e.KeyChar) < 65 Or Asc(e.KeyChar) > 122 Then 'Ensures only letters can be entered by using the ASCII converter'
e.Handled = True
MessageBox.Show("You may only input letters")
End If
End Sub
Input validation is a built-in feature of .NET. You don't need to capture key presses and manually code your own solution.
You might want to use a MaskedTextBox as suggested in this article:
https://learn.microsoft.com/en-us/dotnet/framework/winforms/user-input-validation-in-windows-forms
You can change your If statement like this:
If (Asc(e.KeyChar) < 65 Or Asc(e.KeyChar) > 122) And Asc(e.KeyChar) <> 8 Then
Ascii 8 is the Backspace key.
Rather than using the KeyPress event, use the TextChanged event. Also, I would recommend using the IsNumeric function to test whether it is a number or not.
Private Sub TxtBoxCustForename_KeyPress(sender As Object, e As EventArgs) Handles TxtBoxCustForename.TextChanged
For Each entry as Char in TxtBoxCustForename.Text
If IsNumeric(entry) Then
MessageBox.Show("You may only input letters")
Exit For
End If
Next
End Sub
If you're going to use .NET then use .NET.
Private Sub TxtBoxCustForename_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TxtBoxCustForename.KeyPress
Dim keyChar = e.KeyChar
If Not Char.IsLetter(keyChar) AndAlso Not Char.IsControl(keyChar) Then
e.Handled = True
MessageBox.Show("You may only input letters")
End If
End Sub
This still doesn;t prevent the user pasting invalid text into the control though, so you still need to handle the Validating event or use a custom control that catches paste operations.
Combobox should not accept any inputs and backspace. My code accepts backspace.
Private Sub ComboBox5_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox5.KeyPress
If Asc(e.KeyChar) <> 13 And Asc(e.KeyChar) <> 8 And Not IsNumeric(e.KeyChar) Or IsNumeric(e.KeyChar) Then
e.Handled = True
End If
End Sub
Your description and the code you posted don't match well. I am going to assume you left some words out, and you actually want to allow CR, BKSP, and any numeric. It looks like you want a numbers only ComboBox.
As Vincent said, your If statement is confusing. "Not IsNumeric(e.KeyChar) Or IsNumeric(e.KeyChar)" evaluates always as true, it's basically A or Not A.
For numbers only in ComboBoxes, I like this method, though there are lots of ways to skin that cat:
Private Sub ComboBox5_KeyPress(sender As Object, e As KeyPressEventArgs) Handles ComboBox5.KeyPress
Select Case AscW(e.KeyChar)
Case 13
'Do whatever you need with CR here
Case 8, 3, 22, 24, 26
'backsp copy paste cut undo
'let 'em be
Case Else
e.Handled = Not IsNumeric(e.KeyChar)
End Select
End Sub
If you really didn't want BackSpace, simply delete 8 from that case statement, or the whole case statement if you don't want the user to edit the text at all.
If you want to restrict your comboBox so that it should not accept inputs.
you can set the DropDownStyle property of your ComboBox to DropDownList
You can also set it programmatically
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
This question already has answers here:
How to filter textbox input to numeric only?
(9 answers)
Closed 8 years ago.
I really need to prevent any non-numeric entry (Including paste) to my TextBox and also I want to prevent it from appearing on the TextBox before being erased, It would be wonderful if you put some hints regarding your code suggestion, because I'm just a beginner!
Edited : added CtrlV handling and opimized Keypress event.
You could use this code, wich prevent numeric values from being handled :
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If Char.IsDigit(e.KeyChar) Then ' Check if the value is a number
e.KeyChar = ChrW(0)
e.Handled = True
End If
End Sub
and for the CtrlV handling you can use this code :
Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
' Checking if control + v is clicked or Shift + Insert is clicked
If (e.Control AndAlso Char.ToLower(ChrW(e.KeyValue)) = "v"c) Or (e.Shift AndAlso e.KeyValue = Keys.Insert) Then
e.Handled = True
e.SuppressKeyPress = True
End If
End Sub
Take a look to the following links:
http://www.vbforums.com/showthread.php?570438-Restrict-TextBox-to-only-certain-characters-numeric-or-symbolic
http://www.codigofacilito.com/videos/visual_basic_net_parte_funcion_para_recibir_solo_caracteres_curso_vbnet
If you still have any doubt, let me know.
In the text box, I only want a 3 number combination of 0 and 1 as 011,110,111,001, etc. How can I write to check individual component(from three numbers) is 0 or 1, and specify this for checking error?
I want to have a if statement if possible.
For example,
If the number is 015, this message will be shown.
MsgBox("Please Insert a combination of 0,1 into the text box.")
I would simply restrict character entry to zeroes and ones as the user enters them, and limit the length. You'll have to adjust this code for the proper If checks; I'm a C# guy, not a VB guy.
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar <> ChrW(Keys.Back) Then
If Textbox1.Length < 3 and (e.KeyChar = "0" or e.KeyChar = "1") Then
Else
e.Handled = True
End If
End If
End Sub
Can someone tell me the different variables in this block of code, if it is also possible know details of the process that would also be great:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not ((Asc(e.KeyChar) = 8 OrElse e.KeyChar = " ") OrElse (e.KeyChar >= "A" AndAlso e.KeyChar <= "z")) Then
e.Handled = True
CType(sender, TextBox).Clear()
End If
End Sub
This event handler handles the KeyPress event of a Textbox. See this piece of documentation for further information and also on the parameters provided.
The event is commonly used to handle specific user input in a special way. In this case, the code is used to filter user input and clear the textbox once a a user enters a character other than ASCII 8, a space or a letter in the range from A-Z (both upper and lower case).
In order to accomplish this, the character that has been entered by the user (e.KeyChar) is compared with the range. If the condition is met, e.Handled is set in order to circumvent that the character is processed as usual (e.g. added to the textbox content).
Also, the textbox is cleared by casting the sender parameter that contains the sender of the event to type of Textbox and calling the Clear method. The cast is necessary because the sender parameter is of the more general type Control that does not provide the Clear method.