VB.net HELP!

hity645

[H]F Junkie
Joined
May 11, 2005
Messages
8,334
I have a text box that user can enter any one character into. What I'm searching for is either for that character to be a "J" or "F". If a lowercase of either is entered, and errorprovider and messagebox spits out a message. If anything other than "J" or "F" is entered the same errorprovider and messagebox pops up. If the user puts in one of the two correct characters the errorprovider is cleared and all is happy in the world.

Everything I've tried has failed. I've tried <>, multiple strings, different values, If not =, and more. Any help would be appreciated.
 
When I see VB I think vbulletin, but now I guess we are talking about visual basic.

I know in vbulletin if I wanted to set J and F to be the only terms allowed for a username, I would use (J|F) for the regular expression (PCRE).

is there a preg_match equivalent for visual basic?
 
lol

When I see VB I think vbulletin, but now I guess we are talking about visual basic.

I know in vbulletin if I wanted to set J and F to be the only terms allowed for a username, I would use (J|F) for the regular expression (PCRE).

is there a preg_match equivalent for visual basic?
 
Need more information (i.e. what code are you trying) for what you're trying to do (and what part isn't working specifically), but it sounds like it'd take about 3-5 lines...
 
Code:
If string <> "F" or string  <> "J" Then
MessageBox.Show("Please fill in all required fields.", "Warning")
ErrorProvider.seterror(txtbox, "Please enter F or J.")
End If

This is what I'm trying but every time I run the program and enter something other than F or J, it closes and highlights the first line. I have also tried:

Code:
If string <> "F" Then
MessageBox.Show("Please fill in all required fields.", "Warning")
ErrorProvider.seterror(txtbox, "Please enter F or J.")
Else if string  <> "J" Then
MessageBox.Show("Please fill in all required fields.", "Warning")
ErrorProvider.seterror(txtbox, "Please enter F or J.")
End If

But this will show the error message regardless if J or F is entered.
 
Code:
If string <> "F" or string  <> "J" Then
MessageBox.Show("Please fill in all required fields.", "Warning")
ErrorProvider.seterror(txtbox, "Please enter F or J.")
End If

This is what I'm trying but every time I run the program and enter something other than F or J, it closes and highlights the first line. I have also tried:

Code:
If string <> "F" Then
MessageBox.Show("Please fill in all required fields.", "Warning")
ErrorProvider.seterror(txtbox, "Please enter F or J.")
Else if string  <> "J" Then
MessageBox.Show("Please fill in all required fields.", "Warning")
ErrorProvider.seterror(txtbox, "Please enter F or J.")
End If

But this will show the error message regardless if J or F is entered.

In the first example, which ilne gets highlighted? The If or the Messagebox.show? I'm guessing its the messagebox.show, which would imply there's something that is disliked when it is run. Think about it - the only time you get the error is when the conditions aren't met, that would imply your If statement is working as you want it to, its just whatever you're trying to do in that scenario isn't working. What does the debugger tell you the problem is? Have you researched Messagebox.Show? Its a rather overloaded function and might not like what you're feeding it.

Going with the assumption that the if statement is working, comment it out and work with the error provider and message box until it works how you want it to, then add the if statements back in.

In the second example, even if the logic was correct, I would stamp fail on that code for not being simple and elegant. You wrote out the error statements TWICE. Not good practice. Either way, if you walk through the if logic used, you'll see that it is always going to be true, no matter what the input is.

I would assume that you are performing data input validation elsewhere in your code and you're also clearing the error provider elsewhere in the code since that appears to be a requirement of your assignment.
 
Thanks for the input, I redid the code out for my first example and now no matter what I get the error message and error provider if either of the wanted inputs is entered. The error provider is not being cleared elsewhere. My second example we'll just ignore, I thought it wouldn't work and it doesn't.

Code:
        If string <> "F" Or string <> "J" Then
ErrorProvider.SetError(textbox, "Please enter either J or F")
MessageBox.Show("Error", "Error")
ElseIf string = "F" Or string = "J" Then
ErrorProvider.SetError(textbox, "")
End If

I also tried:
Code:
If string <> "F" or "J" then
ErrorProvider.SetError(textbox, "Please enter either J or F")
MessageBox.Show("Error", "Error")
ElseIf string = "F" Or string = "J" Then
ErrorProvider.SetError(textbox, "")
End If

In this the "If string <> "F" or "J" then" gets highlighted and a message pops up that says:
"InvalidCastException was unhandled", "When casting from string "J" to type Boolean' is not valid".
 
Thanks for the input, I redid the code out for my first example and now no matter what I get the error message and error provider if either of the wanted inputs is entered. The error provider is not being cleared elsewhere. My second example we'll just ignore, I thought it wouldn't work and it doesn't.

Code:
        If string <> "F" Or string <> "J" Then
ErrorProvider.SetError(textbox, "Please enter either J or F")
MessageBox.Show("Error", "Error")
ElseIf string = "F" Or string = "J" Then
ErrorProvider.SetError(textbox, "")
End If

I also tried:
Code:
If string <> "F" or "J" then
ErrorProvider.SetError(textbox, "Please enter either J or F")
MessageBox.Show("Error", "Error")
ElseIf string = "F" Or string = "J" Then
ErrorProvider.SetError(textbox, "")
End If

In this the "If string <> "F" or "J" then" gets highlighted and a message pops up that says:
"InvalidCastException was unhandled", "When casting from string "J" to type Boolean' is not valid".

The logic in the new second attempt doesn't work. Ultimatly, an if statement must be evaluated as a yes or a no. Its breaking it down by evaluating (string <> "F") or "J". If string = "G", then the evaluation would be 1 or "J", which doesn't fit. "J" must be evaluated to either a 1 or 0 as well.

Going back to the first, there's no need for an else if with conditions since there's only two possibilities left (J or F) after the if processes - simple and elegant would say to do else turn off error provider.

I presume that string is declared as a string and is being populated from textbox.value? Could there be extra garbage coming thorugh like an extra space? I'd throw down a break point and examine the contents of the string variable before doing the if statement, then step through to see where its tripping up.
 
Code:
        If TextBox1.Text <> "F" And TextBox1.Text <> "J" Then
            If TextBox1.Text = "f" Or TextBox1.Text = "j" Then
                ErrorProvider1.SetError(TextBox1, "Please make sure to capitalize F or J")
                MessageBox.Show("Please check capitalization", "Input Error")
                Exit Sub
            End If
            ErrorProvider1.SetError(TextBox1, "Please enter F or J")
            MessageBox.Show("Please enter F or J", "Input Error")
        Else
            ErrorProvider1.SetError(TextBox1, "")
        End If

Your "Or" should be an "And." Think about it for a second.

If you have "If TextBox1.Text <> "F" Or TextBox1.Text <> "J"" and you entered "F," what is it going to evaluate to? Well... the first part of the statement will be false because the text does equal "F." But... the second part of the statement will be true because it does not equal "J." With the "Or" keyword, you are asking for the code within the if statement to execute if either of the statements are true. That is why it is always executing on you... your text would have to be both "F" and "J" at the same time in order for it to return false.

By changing that "Or" to an "And," you are stating that the text cannot equal "F" and it also cannot equal "J." If the text equals either "F" or "J," the expression will return false and code within will not execute.

In the example code above, you can see that after we test the input, if is not what we are looking for ("F" or "J"), we then test to see if it is either "f" or "j." This time, you would use the "Or" keyword because you want that if statement to return true if it is either "f" or "j."

Of course you could just initially check for what you want instead of for what you don't want. That might make it a bit easier to read. Like everything else in programming, there are a bunch of different ways to do the same thing. Just pick what is easiest for you.

Code:
        If TextBox1.Text = "F" Or TextBox1.Text = "J" Then
            ErrorProvider1.SetError(TextBox1, "")
        ElseIf TextBox1.Text = "f" Or TextBox1.Text = "j" Then
            ErrorProvider1.SetError(TextBox1, "Please make sure to capitalize F or J")
            MessageBox.Show("Please check capitalization", "Input Error")
        Else
            ErrorProvider1.SetError(TextBox1, "Please enter F or J")
            MessageBox.Show("Please enter F or J", "Input Error")
        End If
 
Your "Or" should be an "And." Think about it for a second.

If you have "If TextBox1.Text <> "F" Or TextBox1.Text <> "J"" and you entered "F," what is it going to evaluate to? Well... the first part of the statement will be false because the text does equal "F." But... the second part of the statement will be true because it does not equal "J." With the "Or" keyword, you are asking for the code within the if statement to execute if either of the statements are true. That is why it is always executing on you... your text would have to be both "F" and "J" at the same time in order for it to return false.

By changing that "Or" to an "And," you are stating that the text cannot equal "F" and it also cannot equal "J." If the text equals either "F" or "J," the expression will return false and code within will not execute.

In the example code above, you can see that after we test the input, if is not what we are looking for ("F" or "J"), we then test to see if it is either "f" or "j." This time, you would use the "Or" keyword because you want that if statement to return true if it is either "f" or "j."

Of course you could just initially check for what you want instead of for what you don't want. That might make it a bit easier to read. Like everything else in programming, there are a bunch of different ways to do the same thing. Just pick what is easiest for you.

Thanks for taking the time to explain this. I removed my Else If changed OR to AND, and now my code it functioning the way I wanted. I was thinking it would be easier to say what I didn't want than what I wanted in the program. I wasn't thinking outside the box I guess.
 
If I'm understanding this correctly I would handle it this way:;)

On the KeyPress event of your textbox.

Code:
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
 
If e.KeyChar <> Microsoft.VisualBasic.ChrW(Keys.J) Then
 
If e.KeyChar <> Microsoft.VisualBasic.ChrW(Keys.F) Then
MsgBox("You must enter J or F only")
End If
 
End If
 
End Sub

Less is more...

http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.keychar.aspx
 
Last edited:
If I'm understanding this correctly I would handle it this way:;)

On the KeyPress event of your textbox.

Code:
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
 
If e.KeyChar <> Microsoft.VisualBasic.ChrW(Keys.J) Then
 
If e.KeyChar <> Microsoft.VisualBasic.ChrW(Keys.F) Then
MsgBox("You must enter J or F only")
End If
 
End If
 
End Sub

Less is more...

http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.keychar.aspx

If less is more, why not:

Code:
    Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Not (e.KeyChar = Chr(70) Or e.KeyChar = Chr(74)) Then
            MsgBox("You must enter F or J")
        End If
    End Sub

I'll answer my own question... Because that doesn't necessarily solve the OP's problem. The ErrorProvider is completely left out for one thing. In the code I provided in my previous post, I left it up to the OP to attach it to whatever action he wanted. Whether that be a button click, a keypress, or whatever. I also distinguished between a user entering a lowercase F or J because he made that distinction in his original post. I didn't know if he was being specific about the fact that only a capital F or J should work, or if he needed to output a different message if the lowercase values were entered. I just showed how one might make that distinction so as to head off another question before it was needing to be asked. It is quite simple to take those couple of lines of code out if they are not needed, and they also help to illustrate the difference between using And or Or.

I also always use MessageBox.Show() vs. MsgBox so that I can customize the messagebox title without having to enter in as many arguments. Of course, that is just splitting hairs.

At any rate, I always like my code to be as short and efficient as possible too. However, sometimes readability trumps shorter especially when you are trying to teach a concept.

As an aside, if the test was supposed to be done after a keypress, I would also make sure to clear that textbox if it wasn't an F or J. Then you wouldn't get another messagebox popup when you pressed the backspace key to clear the inappropriate value.
 
If less is more, why not:

Code:
    Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Not (e.KeyChar = Chr(70) Or e.KeyChar = Chr(74)) Then
            MsgBox("You must enter F or J")
        End If
    End Sub

I'll answer my own question... Because that doesn't necessarily solve the OP's problem. The ErrorProvider is completely left out for one thing. In the code I provided in my previous post, I left it up to the OP to attach it to whatever action he wanted. Whether that be a button click, a keypress, or whatever. I also distinguished between a user entering a lowercase F or J because he made that distinction in his original post. I didn't know if he was being specific about the fact that only a capital F or J should work, or if he needed to output a different message if the lowercase values were entered. I just showed how one might make that distinction so as to head off another question before it was needing to be asked. It is quite simple to take those couple of lines of code out if they are not needed, and they also help to illustrate the difference between using And or Or.

I also always use MessageBox.Show() vs. MsgBox so that I can customize the messagebox title without having to enter in as many arguments. Of course, that is just splitting hairs.

At any rate, I always like my code to be as short and efficient as possible too. However, sometimes readability trumps shorter especially when you are trying to teach a concept.

As an aside, if the test was supposed to be done after a keypress, I would also make sure to clear that textbox if it wasn't an F or J. Then you wouldn't get another messagebox popup when you pressed the backspace key to clear the inappropriate value.

Thats fine, I was only demonstrating an alternative,way of coding
the same thing.

The problem I have with chr(70), chr(74) is that its nondescriptive
and mysterous to many people. Using capital J & F solves the
uppecase issues, and gets to the heart of validation.

As far as using the error provider, that can easily be put in there.
 
Back
Top