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

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

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.

How can I make it so a TextBox will dynamically adjust the input as a currency format?

This app I'm designing has a TextBox named txtValue with the properties MaxLength set to 14 and TextAlign set to Right. I want txtValue to only accept currency, and dynamically format the input so the user doesn't need to add commas, only one period.
I managed to make it so txtValue will only accept numbers and one dot in the event txtValue_KeyPress.
txtValue_LostFocus will convert the input into currency format.
Here's my code so far:
Private Sub txtValue_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtValue.KeyPress
'Allows only one dot
If (e.KeyChar.ToString = ".") And (txtValue.Text.Contains(e.KeyChar.ToString)) Then
e.Handled = True
Exit Sub
End If
'Allows only 0 to 9 and dot (once)
If (e.KeyChar.ToString < "0" OrElse e.KeyChar.ToString > "9") _
AndAlso e.KeyChar <> ControlChars.Back _
AndAlso e.KeyChar.ToString <> "." Then
e.Handled = True
End If
End Sub
Private Sub txtValue_LostFocus(sender As Object, e As EventArgs) Handles txtValue.LostFocus
txtValue.Text = Format(Val(txtValue.Text), "000,000,000.00")
End Sub
I expect the input -q1w23456789012....34 to return the output 123,456,789,012.34, but the actual output after it loses focus is 123,456,789,012.30
This seems like an easy fix, like setting MaxLength to 15, but then if I don't type a period, it'll allow me to type 15 numbers and I only want up to 12 plus 2 after the period.
I expect the input -q1w234....5678 to return the output 1,234.56, but the actual output after it loses focus is 000,000,001,234.56
This seems like a more complex fix, because I don't want to use the LostFocus event to validate what I type. I want the KeyPress event to handle the input and dynamically format what I type.
In this case:
The input 1 would have the output 1.00
The input 123.4 would have the output 123.40
The input 1234.567 would have the output 1,234.56
All of this without needing the LostFocus event, but right now I'm using the LostFocus event because that's all my very limited knowledge allows me to do.
UPDATE
Alright I'm now using the Leave event, but then again I was only using LostFocus as a placeholder because in the end I want the TextBox to adjust what the user types as they type.
An alternative way to handle. For details on formating numbers for display try MS docs https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings or https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings
Private err As New ErrorProvider()
Private d As Decimal 'In case you want to use the value as a number somewhere else
Private Sub TextBox17_Validating(sender As Object, e As CancelEventArgs) Handles TextBox17.Validating
If Not Decimal.TryParse(TextBox17.Text, d) Then
e.Cancel = True
err.SetError(TextBox17, "This text box must contain a number.")
Else
err.Clear()
End If
End Sub
Private Sub TextBox17_Validated(sender As Object, e As EventArgs) Handles TextBox17.Validated
TextBox17.Text = d.ToString("C")
End Sub

Restriciton in combobox should not delete text in combox

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

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

VB2010: Allow Float/Integers, Backspace, and Range of Value in Textbox

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