How do I see if a textbox contains this OR contains that properly?
I have this code driving a validator:
Code:
If TextBox1.Text.Contains(".urw") Or TextBox1.Text.Contains(".URW") = False Then
TextBox2.AppendText("Input file is not a valid. Please try again.")
But I also have an opposite one under the "true" condition.
However, no matter what I do, my utility only validates ".URW" correctly and not ".urw".
Does anybody know why?
Re: How do I see if a textbox contains this OR contains that properly?
shouldn't that be:
vb Code:
If TextBox1.Text.Contains(".urw") = False and TextBox1.Text.Contains(".URW") = False Then
TextBox2.AppendText("Input file is not a valid. Please try again.")
???
Re: How do I see if a textbox contains this OR contains that properly?
Nope still getting the error :(
I've also tried replacing the "and"s with "or"s
Re: How do I see if a textbox contains this OR contains that properly?
are you sure you're using it correctly? theres no reason it shouldn't work
vb Code:
If not TextBox1.Text.Contains(".urw") and not TextBox1.Text.Contains(".URW") Then
TextBox2.AppendText("Input file is not a valid. Please try again.")
Re: How do I see if a textbox contains this OR contains that properly?
I need to validate files that end with wither .urw or .URW.
Re: How do I see if a textbox contains this OR contains that properly?
Re: How do I see if a textbox contains this OR contains that properly?
vb Code:
If not TextBox1.Text.Contains(".urw") and not TextBox1.Text.Contains(".URW") Then
TextBox2.AppendText("Input file is not a valid file. Please try again.")
else
'file is valid
end if
Re: How do I see if a textbox contains this OR contains that properly?
You should be using AndAlso and OrElse rather than And and Or, as they work better. In the first example, it may be that it was being evaluated in this fashion:
(contains(A) or contains(B)) = False
Re: How do I see if a textbox contains this OR contains that properly?
Code:
If TextBox1.Text.Contains(".urw") OrElse TextBox1.Text.Contains(".URW") Then
Debug.WriteLine("TextBox1 contains .urw or .URW")
Else
Debug.WriteLine("TextBox1 does not contain .urw or .URW")
End If
'or
If Not (TextBox1.Text.Contains(".urw") OrElse TextBox1.Text.Contains(".URW")) Then
Debug.WriteLine("TextBox1 does not contain .urw or .URW")
Else
Debug.WriteLine("TextBox1 contains .urw or .URW")
End If
this
If TextBox1.Text.Contains(".urw") Or TextBox1.Text.Contains(".URW") = False Then
should only catch ".urw", at least that is what it did on my PC.
It is hard to know what you are trying to test for? Is it
.urw or .URW are valid?
.urw is valid but .URW isn't and vice versa.
Re: How do I see if a textbox contains this OR contains that properly?
Do you really want to limit the user to all upper case or all lower case? Should case matter at all?
vb.net Code:
If Me.TextBox1.Text.IndexOf(".urw", StringComparison.CurrentCultureIgnoreCase) = -1 Then
Re: How do I see if a textbox contains this OR contains that properly?
Or
Code:
If Me.TextBox1.Text.ToLower().Contains(".urw") Then
Re: How do I see if a textbox contains this OR contains that properly?
If you want to check for a certain file extension, don't use String.Contains method. It can give you false result. For example, if the textbox.text = "C:\Test\myfile.urw.txt", the actual file extension in this case is .txt yet string.contains will still return true since the file path does contain ".urw".
You should use System.IO.Path.GetExtension(filePath) to get the file extension and test to see if it is ".urw", something like this:
Code:
Dim filePath As String = TextBox1.Text
Dim ext As String = System.IO.Path.GetExtension(filePath).ToLower()
If ext <> ".urw" Then
TextBox2.AppendText("Input file is not a valid. Please try again.")
Else
'The file extension is valid, do whatever desired here
End If