Textbox Contains Text?

Michaelk

Well-known member
Joined
Jun 3, 2004
Messages
58
Location
Australia
Programming Experience
3-5
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:
VB.NET:
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.
 
My first instinct is to use the InStr Function
VB.NET:
If instr(TextBox1.Text, "Test") <> 0 Then
'Insert Code "Test" exists in TextBox
Else
'Insert Code "Test" does not exist
End If
 
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

VB.NET:
If the textbox contains (a line from the txt file) Then[/left]
'Insert Code
Else
'Insert Code
End If

Thanks heaps, i've been trying to figure this out for ages!​
 
For that I would use the StreamReader:

VB.NET:
      ' 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
 
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:

VB.NET:
      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:
Back
Top