Results 1 to 12 of 12

Thread: How do I see if a textbox contains this OR contains that properly?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    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?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: How do I see if a textbox contains this OR contains that properly?

    shouldn't that be:

    vb Code:
    1. If TextBox1.Text.Contains(".urw")  = False and TextBox1.Text.Contains(".URW") = False Then
    2.    TextBox2.AppendText("Input file is not a valid. Please try again.")
    ???

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    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

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    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:
    1. If not TextBox1.Text.Contains(".urw") and not TextBox1.Text.Contains(".URW") Then  
    2.    TextBox2.AppendText("Input file is not a valid. Please try again.")

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    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.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: How do I see if a textbox contains this OR contains that properly?

    try the code in post#4

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: How do I see if a textbox contains this OR contains that properly?

    vb Code:
    1. If not TextBox1.Text.Contains(".urw") and not TextBox1.Text.Contains(".URW") Then      
    2.    TextBox2.AppendText("Input file is not a valid file. Please try again.")
    3. else
    4.    'file is valid
    5. end if

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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
    My usual boring signature: Nothing

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    Last edited by dbasnett; Sep 7th, 2008 at 06:48 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. If Me.TextBox1.Text.IndexOf(".urw", StringComparison.CurrentCultureIgnoreCase) = -1 Then
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: How do I see if a textbox contains this OR contains that properly?

    Or

    Code:
    If Me.TextBox1.Text.ToLower().Contains(".urw") Then

  12. #12
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width