I tried to get help on this on a different thread, but I didnt get any help on specifically for this, so im just making a thread just for this. The first problem is im trying to make a five digit string, it needs to be 5 digits long basically it cant be less then 10000 and more then 99999, but when I run the code, all the label says is "this is a five digit code" from the time I start putting in numbers to even if I got a whole lot of numbers are in it, it remands the same label, what could be wrong or am I doing this wrong

Dim myDouble As String

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        If myDouble >= 10000 Or myDouble <= 99999 Then

            lblmessage.Text = "this is a five digit code"

        Else

            lblmessage.Text = "Not a 5 digit code, please renter a new one"
        End If
    End Sub

For the three character sting, I have
ON: Motor is online and running
OFF: Motor is online, but not running
MNT: Motor is undergoing maintenance and cleaning
NA: Motor is not available
I dont know how to start this, do I make a specific thing saying that these are the only ones you can use and if you entered something else then a text box saying incorrect or something like that?

Have you tried using the length function in VB.Net?

If isNumeric(myDouble) = True Then '// Checks to see that string entered is numbers
        If Len(myDouble) = 5 Then '// Checks length of the entered string is 5
            lblmessage.Text = "this is a five digit code"
        Else
            lblmessage.Text = "Not a 5 digit code, please enter a new one" '// Took the 'r' off renter on this line, type i presume
        End If
Else 
        lblmessage.Text = "Not a numeric code, please enter a new one" '// New error message for numeric check     
End If
    End Sub

And for the second string you could use the following to lock down the entry option to the four options, i would suggest using a label beside it or above to tell the user what they can enter, unless they already knew this.

'// Validation for string containing: On Off Mnt Na
        '// Change "Textbox1.text" to your textbox
        If Not TextBox1.Text = "ON" Then '// If textbox doesnt contain ON then
            If Not TextBox1.Text = "OFF" Then '// If textbox doesnt contain OFF then
                If Not TextBox1.Text = "MNT" Then '// If textbox doesnt contain MNT then
                    If Not TextBox1.Text = "NA" Then '// If textbox doesnt contain NA then
                        MsgBox("Must be either: ON/OFF/MNT/NA, MsgBoxStyle.Exclamation") '// Error message displayed
                    End If
                End If
            End If
        End If

Edited 5 Years Ago by Mike Askew: Added in solution for the second text string

>>I tried to get help on this on a different thread, but I didnt get any help on specifically for this...
See if this helps.
It contains a Boolean which will allow you to add items to your ListBox or not, depending if a correct value in the TextBox.

Public Class Form1

    Private bIsValidValue As Boolean '// used to validate if TextBox value is valid or not.

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        bIsValidValue = False '// reset/set to False.
        With TextBox1 '// shorten code.
            If .Text = "" Then '// if .Text is empty, clear Label.Text and skip remaining code in this Sub.
                Label1.Text = ""
                Exit Sub
            End If
            If IsNumeric(.Text) Then '// if .Text is Numeric and does not contain letters.
                If .TextLength = 5 Then '// check if length is 5.
                    If CInt(.Text) >= 10000 AndAlso CInt(.Text) <= 99999 Then '// check for value, if greater/less than...
                        Label1.Text = "This is a five digit code :)"
                        bIsValidValue = True '// set to True since it is Valid.
                    Else
                        Label1.Text = "Value must be between 10,000 and 99,999"
                    End If
                Else
                    Label1.Text = "Not a 5 digit code, please renter a new one"
                End If
            Else '// if not Numeric.
                Label1.Text = "Only Numeric values will be accepted"
            End If
        End With
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If bIsValidValue = True Then
            '// add code here to add to your ListBox(as specified in your other thread)
            MsgBox("Valid value in TextBox, proceed with code here...")
        Else
            MsgBox("Not a valid value in TextBox")
        End If
    End Sub
End Class

Edited 5 Years Ago by codeorder: n/a

MikeyIsMe
For the first code, it keeps saying "Not a numeric code, please enter a new one" if I put numbers (or even letters, which I was just seeing if there would be a change) in it, it doesn’t change and for the second code (the 3 character one) is what im looking for, but when I put the code in and loaded it up, There was one problem, when I tried to type something like O for ON, it comes up as an error, but when I exit the error message and but the N, it was fine, but if I put another letter it comes up as an error (what it should do), basically my question is why does it comes up with an error when I try to start typing in letters?

Codeorder
Even yours comes up with the same problem, I don’t know if its something that im doing wrong, because both are having the same issue, yours is, when I enter the numbers and then click the button this always comes up. “Not a valid value in TextBox” I don’t know why and is a bollean a string? because I need to use a string for this and nothing else

My code is to only get a proper 5 digit number, nothing else.

Try this.
Replace bIsValidValue = False '// reset/set to False. at the top of Private Sub TextBox1_TextChanged( , with Button1.Enabled = False AndAlso, replace the bIsValidValue = True '// set to True since it is Valid. from the same event, with Button1.Enabled = True .
When your Button1 enables after typing in the TextBox, you have a valid number.

Edited 5 Years Ago by codeorder: n/a

Ok I made the changes and its still coming up as "Not a valid value in TextBox." I know your trying to help me out on this, but my assignment says that it needs to be in a string and your using a boolean, Not to be rude but is there a way we could do it in a sting, kinda like the way I started to do it?

You do not need the Boolean anymore, if you are using the Button1.Enabled=True/False.
.Remove Private bIsValidValue As Boolean '// used to validate if TextBox value is valid or not. and also only have your MsgBox code, if you need it, in Button1's.Click event.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            MsgBox("Valid value in TextBox, proceed with code here...")

    End Sub

I would also set Button1.Enabled=False on Form1_Load.

.Text from a TextBox is a "String".

Apologies, have run my code properly now and ironed out any issues. This code was made on a form using two textbox and two labels.

Private Sub TextBox1_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFiveString.LostFocus '// Can change the to .TextChanged if preferred
        '// For the Five String issue //
        If IsNumeric(txtFiveString.Text) = True Then '// Checks to see that string entered is numbers
            If Len(txtFiveString.Text) = 5 Then '// Checks length of the entered string is 5
                lblFiveString.Text = "this is a five digit code"
            Else
                lblFiveString.Text = "Not a 5 digit code, please enter a new one" '// Took the 'r' off renter on this line, type i presume
            End If
        Else
            lblFiveString.Text = "Not a numeric code, please enter a new one" '// New error message for numeric check     
        End If
    End Sub

    Private Sub txtThreeString_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtThreeString.LostFocus
        '// For the Three String issue //
        '// Validation for string containing: On Off Mnt Na
        '// Change "Textbox1.text" to your textbox
        If Not txtThreeString.Text = "ON" Then '// If textbox doesnt contain ON then
            If Not txtThreeString.Text = "OFF" Then '// If textbox doesnt contain OFF then
                If Not txtThreeString.Text = "MNT" Then '// If textbox doesnt contain MNT then
                    If Not txtThreeString.Text = "NA" Then '// If textbox doesnt contain NA then
                        lblThreeString.Text = "Must be either: ON/OFF/MNT/NA" '// Error message displayed
                    Else
                        lblThreeString.Text = "Correct Entry"
                    End If
                Else
                    lblThreeString.Text = "Correct Entry"
                End If
            Else
                lblThreeString.Text = "Correct Entry"
            End If
        Else
            lblThreeString.Text = "Correct Entry"
        End If
    End Sub

I was missing the .text off the end of the length check on my first set of code stopping the five string bit working. For the three string i simply changed it from .textchanged to .lostfocus , therefore when the next textbox in the list of entry is selected or any other form item it will run the validation checks.

Heres another version.

Public Class Form1

    Private CorrectInput() As String = {"ON", "OFF", "MNT", "NA"}

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        'Real world would put the correct input items in a dropdownlist.
        ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
        ComboBox1.DataSource = CorrectInput
    End Sub

    Private Sub TextBoxThreeString_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxThreeString.LostFocus
        Select Case Array.IndexOf(CorrectInput, TextBoxThreeString.Text.Trim.ToUpper)
            Case 0
                'On
            Case 1
                'Off
            Case 2
                'Mnt
            Case 3
                'Na
            Case Else
                'Invalid
        End Select
    End Sub

    Private Sub TextBox5Digit_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox5Digit.LostFocus
        If Not IsNumeric(TextBox5Digit.Text) OrElse CInt(TextBox5Digit.Text) < 10000 OrElse CInt(TextBox5Digit.Text) > 99999 Then
            'Bad when leaving
        End If
    End Sub

    Private Sub TextBox5Digit_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5Digit.TextChanged
        'You can't validate a length of less than 5 while typing. You can only
        'validate that the text is numeric and its not greater than 5.
        If Not IsNumeric(TextBox5Digit.Text) OrElse TextBox5Digit.Text.Length > 5 Then
            'Bad while typing.
        End If
    End Sub

End Class
This article has been dead for over six months. Start a new discussion instead.