title: Search through text file and return a line of text
body: <p>I am trying to create a program that will search a text file for a line of text and then return the full line of information. </p>
  
  <p>Example line: Joe Blogs JBL 1234 </p>
  
  <p>Search: Joe Blogs</p>
  
  <p>Search returns: Joe Blogs JBL 1234 </p>
  
  <p>To make it as simple as possible, I have 2 text boxes &amp; 1 button. </p>
  
  <p>Textbox1 = search </p>
  
  <p>Textbox2 = Search results </p>
  
  <p>Button = Search button </p>
  
  <p>Can anyone tell me how to do this because I'm finding it really difficult. I'm new to VB coding so the simplest of code would be helpful! </p>
  
  <p>This is what I have so far: </p>
  
  <pre><code>Imports System.IO
  
  Public Class Form1
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  
      ' Input Text Error 
  
      If TextBox1.TextLength = 0 Then
          MsgBox("Please enter a Staff Name or Staff Code", MsgBoxStyle.Information, "Error")
      End If
  
      'Perform Search
  
      Dim strText As String = SearchFile("F:\Documents\Documents\Visual Studio 2015\Projects\ExtentionLocator\ExtentionLocator\Extentionlist.txt", TextBox1.Text)
      If strText &lt;&gt; String.Empty Then
          TextBox2.Text = strText
      End If
  
  End Sub
  
  'Search Function
  
  Public Shared Function SearchFile(ByVal strFilePath As String, ByVal strSearchTerm As String) As String
      Dim sr As StreamReader = New StreamReader(strFilePath)
      Dim strLine As String = String.Empty
  
      Try
          Do While sr.Peek() &gt;= 0
              strLine = String.Empty
              strLine = sr.ReadLine
              If strLine.Contains(strSearchTerm) Then
                  sr.Close()
                  Exit Do
              End If
          Loop
  
          Return strLine
      Catch ex As Exception
          Return String.Empty
      End Try
  End Function
  
  'Clear Button
  
  Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
      TextBox2.Text = ""
      TextBox1.Text = ""
  End Sub
  
  ' Open The text file 
  
  Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
      Process.Start("C:\Users\kylesnelling\Documents\Visual Studio 2015\Projects\ExtentionLocator\ExtentionLocator\Extentionlist.txt")
  End Sub
  
  End Class
  </code></pre>
  
  <p>Whenever I perform a search, all I get back is the last line of the text file... does anyone know why?</p>