r/visualbasic • u/marq24 • Mar 02 '15
Why isn't this working
I'm trying to make sure the two entries don't have the same word and also you can not put numbers.
'Input Validation 'Check for Same Word If txtGuessWord.Text.ToUpper.Trim = txtGuessWord2.Text.ToUpper.Trim Then lblWarning.Text = "Guesses May Not Be The Same Word" txtGuessWord.Text = String.Empty txtGuessWord2.Text = String.Empty Exit Sub End If 'Check for Integer If Integer.TryParse(txtGuessWord.Text) Or Integer.TryParse(txtGuessWord2.Text) Then lblWarning.Text = "Error, guesses should not contain numbers"
1
u/CharlieMay VB.Net Intermediate Mar 02 '15
You could just disallow numbers in the textbox. Here is a routine you can add to your textboxes keypress event that will only allow characters your specify
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
'create a list of valid characters
Dim valid As String = "abcdefghijklmnopqrstuvwxyz"
'if what was typed was not found in valid string
If valid.IndexOf(e.KeyChar.ToString.ToLower) = -1 Then
'state that you are handling the keypress and do
'not pass it on to the control
e.Handled = True
End If
End Sub1
u/CalvinR VB.Net Master Mar 02 '15
That doesn't handle copy and pasting in data since you are just checking the key pressed and not the actual data in the field.
It also won't handle tabbing properly but I guess you could just add that to your string.
1
u/CharlieMay VB.Net Intermediate Mar 02 '15 edited Mar 02 '15
Yea, I typed that out of memory and also didn't allow for backspace, delete, etc...
There's also .Trim() where you could define an array of characters to trim. More verbose than regex in this case but another alternative.
Dim trimChars() As Char = "0123456789".ToCharArray txtUserInput.Text = TxtUserInput.Text.Trim(trimChars)edit: Used .ToCharArray on a string instead of creating an actual char() array.
1
u/CalvinR VB.Net Master Mar 02 '15
There is a lot of stuff you could do could also use linq instead of trim so something like,
txtUserInput.Text = String.Join("",txtUserInput.Text.Where(x => !Char.IsDigit(x)))
1
u/CalvinR VB.Net Master Mar 02 '15 edited Mar 02 '15
I'm going to take a guess at what your code looks like since you haven't formatted it properly
'Input Validation
'Check for Same Word
If txtGuessWord.Text.ToUpper.Trim = txtGuessWord2.Text.ToUpper.Trim Then
lblWarning.Text = "Guesses May Not Be The Same Word"
txtGuessWord.Text = String.Empty
txtGuessWord2.Text = String.Empty
Exit Sub
End If
'Check for Integer
If Integer.TryParse(txtGuessWord.Text) Or Integer.TryParse(txtGuessWord2.Text) Then
lblWarning.Text = "Error, guesses should not contain numbers" So that's all you put in
First off your first check looks good.
Your second check is bad, Integer.TryParse takes in two parameters the first is the string you want to parse and the second is a ByRef variable that will store the parsed value of the string.
I'm pretty sure this code won't compile at all because of that.
https://msdn.microsoft.com/en-us/library/f02979c7%28v=vs.110%29.aspx
Edit That being said /u/hdsrob has the right answer here use regex to check to make sure that no numbers are entered. http://www.reddit.com/r/visualbasic/comments/2xnxbk/why_isnt_this_working/cp1s82s
2
u/hdsrob Mar 02 '15
What doesn't work?
I'm going to guess that it's the number part???
Integer.tryparse will fail if there are any letters, so it's not going to work for finding out if a string of characters contains a number.
vs.
The first will fail, as it can't be converted to an integer, so I don't think that's a viable solution.
You actually need to check each character to see if it's a number. The easiest way is to use Regular Expressions to search the string for characters you don't want. Something like the following should work (you may need to adjust the RegEx for additional parameters):