vb Code:
  1. '
  2. Public Class Form1
  3.  
  4.     Private sr As IO.StreamReader
  5.  
  6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  7.         Debug.Assert(Not IsNothing(sr))
  8.  
  9.         ' checking whether we're not at the end yet
  10.         If Not (sr.EndOfStream) Then
  11.             ' Read the next line and display in in the texbox
  12.             TextBox1.Text = sr.ReadLine()
  13.         Else
  14.             MessageBox.Show("Reached the end of file")
  15.         End If
  16.     End Sub
  17.  
  18.     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  19.         sr.Close()
  20.         sr.Dispose()
  21.     End Sub
  22.  
  23.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  24.         Using dlg As New OpenFileDialog With {.CheckFileExists = True}
  25.             Dim ret As DialogResult = dlg.ShowDialog(Me)
  26.             If ret = Windows.Forms.DialogResult.OK Then
  27.                 Try
  28.                     ' Open file for reading
  29.                     sr = New IO.StreamReader(New IO.FileStream(dlg.FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read))
  30.                 Catch ex As Exception
  31.                     ' Catching possible exceptions
  32.                     MessageBox.Show(ex.Message)
  33.                 End Try
  34.             End If
  35.         End Using
  36.  
  37.     End Sub
  38. End Class