Results 1 to 8 of 8

Thread: Textbox Contains Text?

  1. #1
    Michaelk is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jun 2004
    Location
    Australia
    Posts
    58
    Reputation
    124

    Textbox Contains Text?

    Hi. I've got a text box that contains a range of text. For example:
    "aaaabbbbcccc"
    I'm trying to write an If statment to work out if some of the text is there.
    For example:
    Code:
    If the textbox contains "bbbb" Then
    'Insert Code
    Else
    'Insert Code
    End If
    The code should ignore the rest of the text in the textbox.
    Any ideas?

    Thanks in advance.

  2. #2
    Schenz is offline VB.NET Forum Genius
    .NET Framework
    .NET 1.1 (VS 2003)
    Join Date
    Sep 2004
    Location
    Cincinnati, OH
    Posts
    181
    Reputation
    122
    My first instinct is to use the InStr Function
    Code:
    If instr(TextBox1.Text, "Test") <> 0 Then
    'Insert Code "Test" exists in TextBox
    Else
    'Insert Code "Test" does not exist
    End If
    Brandon Schenz

  3. #3
    Michaelk is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jun 2004
    Location
    Australia
    Posts
    58
    Reputation
    124
    That worked well. Thanks heaps.

    If it's not too much trouble, do you also know how to scan a TXT file for a entry, and if a textbox contains that text it executes the code.
    The txt file has a entry on each line.
    Example:
    Line 1: "aa"
    Line 2: "bb"

    So, i'm trying to do this

    Code:
    If the textbox contains (a line from the txt file) Then
    Code:
    'Insert Code
    Else
    'Insert Code
    End If
    Thanks heaps, i've been trying to figure this out for ages!

  4. #4
    Schenz is offline VB.NET Forum Genius
    .NET Framework
    .NET 1.1 (VS 2003)
    Join Date
    Sep 2004
    Location
    Cincinnati, OH
    Posts
    181
    Reputation
    122
    For that I would use the StreamReader:

    Code:
          ' open file into stream reader
          fileRd = New StreamReader("Full Path to File")
          
          ' get the first line
          Dim lineRead As String = fileRd.ReadLine()
    
          ' loop until the end of file
          While (Not lineRead Is Nothing)
            ' skip empty lines
            If (Not SkipLine(lineRead)) Then
              If instr(TextBox1.Text, lineRead) <> 0 Then
                  'Insert Code lineRead exists in TextBox
              Else
                  'Insert Code lineRead does not exist
              End If
            End If
    
            lineRead = fileRd.ReadLine()
          End While
    Brandon Schenz

  5. #5
    Michaelk is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jun 2004
    Location
    Australia
    Posts
    58
    Reputation
    124
    Thanks for the quick response.
    Code:
    If (Not SkipLine(lineRead)) Then
    Doesn't seem to work, it says that "SkipLine" is not defined?
    Thanks.

  6. #6
    Schenz is offline VB.NET Forum Genius
    .NET Framework
    .NET 1.1 (VS 2003)
    Join Date
    Sep 2004
    Location
    Cincinnati, OH
    Posts
    181
    Reputation
    122
    I guess I did not copy that part of it in.

    SkipLine is a special sub to determine if a line is supposed to be skipped or not.

    Change code to:

    Code:
          Dim fileRd as StreamReader
          
          ' open file into stream reader
          fileRd = New StreamReader("Full Path to File")
          
          ' get the first line
          Dim lineRead As String = fileRd.ReadLine()
    
          ' loop until the end of file
          While (Not lineRead Is Nothing)
            If instr(TextBox1.Text, lineRead) <> 0 Then
                  'Insert Code lineRead exists in TextBox
              Else
                  'Insert Code lineRead does not exist
            End If
    
            lineRead = fileRd.ReadLine()
          End While
    That should work for you.
    (Note: I have not activly tested this and did some copy/paste for speed)
    Last edited by Schenz; 09-25-2004 at 10:17 AM.
    Brandon Schenz

  7. #7
    Michaelk is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jun 2004
    Location
    Australia
    Posts
    58
    Reputation
    124
    That does exactly what i was after. Thanks heaps!

  8. #8
    Schenz is offline VB.NET Forum Genius
    .NET Framework
    .NET 1.1 (VS 2003)
    Join Date
    Sep 2004
    Location
    Cincinnati, OH
    Posts
    181
    Reputation
    122
    No problem

    And you have a nice site BTW.
    Brandon Schenz

Bookmarks

Posting Permissions

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