VB2010: Allow Float/Integers, Backspace, and Range of Value in Textbox - vb.net

I've one Textbox in Visual Basic (Visual Studio 2010, .net frame work 4.0)
Now I have a problem!
I want that user only Enter Integer, float, backspace and range of value?
Confused?
Oh yeah
I want that user only Enter value in between 0 - 4 (value may be in decimal as 3.49)
Now I want complete code:
I have this:
This is working , but I am unable to specifies the range between 0-4
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim FullStop As Char
FullStop = "."
' if the '.' key was pressed see if there already is a '.' in the string
' if so, dont handle the keypress
If e.KeyChar = FullStop And TextBox1.Text.IndexOf(FullStop) <> -1 Then
e.Handled = True
Return
End If
' If the key aint a digit
If Not Char.IsDigit(e.KeyChar) Then
' verify whether special keys were pressed
' (i.e. all allowed non digit keys - in this example
' only space and the '.' are validated)
If (e.KeyChar <> FullStop) And
(e.KeyChar <> Convert.ToChar(Keys.Back)) Then
' if its a non-allowed key, dont handle the keypress
e.Handled = True
Return
End If
End If
End Sub
Please if some one give me the complete code for this so I will be very happy
Thanks in advance

I just used the Benifits of ascii codes of characters to solve your problem,
Try this out, If you have any doubts in the following implementation, then feel free to comment me back.
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
'Ascii code 8 for backspace -- Ascii code 46 for (. period)
If Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 46 Then
'If typed character is a period then we have to ensure that more than one of it
'Should not get allowed to type. And also we have to check whether the period symbol
'may cause any conflicts with MaxNo that is 4
If Asc(e.KeyChar) = 46 Then
If TextBox1.Text.IndexOf(".") <> -1 Or Val(TextBox1.Text.Trim & e.KeyChar) >= 4 Then
e.Handled = True
Else
Exit Sub
End If
Else
'If pressed key is backspace, then allow it.
Exit Sub
End If
End If
'Checking whether user typing more than 4 or not.
If Val(TextBox1.Text.Trim & e.KeyChar) > 4 Then
e.Handled = True
End If
'48 - 57 = Ascii codes for numbers
If (Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57) Then
e.Handled = True
End If
End Sub

Related

Data Type validation in textbox

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.

Automatically remove not authorized character on Regular Expressions just after being typed

I'm using this code
Private Sub MyTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyTextBox.KeyPress
If Not Regex.Match(MyTextBox.Text, "^[a-z /s ']*$", RegexOptions.IgnoreCase).Success Then
MyTextBox.Text = MyTextBox.Text.Remove(MyTextBox.SelectionStart - 1, 1)
MyTextBox.Select(MyTextBox.Text.Count, 0)
End If
End Sub
so the user can only add letters, space and apostrophe
The code works if the user digit
somethin8g
the number 8 is removed
But if the user just digit
somethin8
the number 8 is visible until the user press another key, and even worst, if the user press the "SAVE" button the info is accepted even with the number 8
Is there a better way to automatically remove not authorized character on Regular Expressions just after being typed?
Generic solution adaptable to any situation (Just changing the allowedChars string)
No regular expression needed
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) _
Handles txtName.KeyPress
If Not (Asc(e.KeyChar) = 8) Then
Dim allowedChars As String = "abcdefghijklmnñopqrstuvwxyzáéíóúàèìòùäëïöüâêîôû '"
If Not allowedChars.Contains(e.KeyChar.ToString.ToLower) Then
e.KeyChar = ChrW(0)
e.Handled = True
End If
End If
End Sub
Adapted from the answer of Jhon Woo on Get a textbox to accept only characters in vb.net

Textbox validation, cursor skipping to the start of the text

In vb.net, I have the following code to validate acceptable characters that can be entered into a textbox.
Private Sub txt_mobile_phone_TextChanged(sender As Object, e As EventArgs) Handles txt_mobile_phone.TextChanged
Dim s As String = ""
For Each C As Char In txt_mobile_phone.Text
If (C >= "0" And C <= "9") OrElse (C = " ") OrElse (C = "-") OrElse (C = "+") Then
s &= C
End If
Next
txt_mobile_phone.Text = s
End Sub
The problem is, is that when someone enters an invalid character (for example, an exclamation mark '!'), the cursor position then skips to the start of the text string, and all further characters are entered at the start.Is there a way to make it so that it ignores invalid characters and carries on typing from the end of the string?
If possible, without using txt_mobile_phone.SelectionStart = txt_mobile_phone.Text.Length -1 as this means that clicking the middle of the string to add to the middle of it will break (which currently is possible)
The issue is that you are firing an event on the TextChanged, that is int he middle of something. For your purpose, to validate entries, you have got the KeyPress event, whereby you have the e.Handle to block entries.
Find the example below, which I applied in my application to accept only numeric and should not accept spaces;
Private Sub txt_mobile_phone_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_mobile_phone.KeyPress, _
AcEmpNo.KeyPress
' Accept only numeric values
If (e.KeyChar < Chr(48) OrElse e.KeyChar > Chr(57)) _
AndAlso e.KeyChar <> Chr(8) Then
e.Handled = True
End If
End Sub
You can also use Char convertor if you don't know the code like;
e.KeyChar = ChrW(Keys.Space)
Hope this helps.

How to check individual component in the Visual basic. Error checking

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

textbox accept only digits

Private Sub TextBox1_KeyPress(ByVal sender As 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
this code accept only digits and its working like a charm but if i typed a wrong number its not allowing me to use the delete or the backspace on the keyboard how to solve this problem ?
This goes back a long time, back when terminals were still used. You'll also have to allow the backspace character. Put this in the front of your code:
If e.KeyChar = Chr(8) Then Exit Sub
The Delete key doesn't need any help, that already works.
You should have a look at
Keys Enumeration
Keys.Back : The BACKSPACE key.