Tuesday, 24 January 2012

How to read text file in vb.net?


How to read text file in vb.net?
Step1:
Open Microsoft visual studio.
Step2:
File ànew project àselect the windows form application à rename it as text file reader
Step3:
Drag and drop two Button, open file dialog, and list box from tool box
Step4:
Change properties for list box and form
for list box, and form:




Step5:
Copy the below code:
Public Class Form1

    Private SubButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton1.Click
        Using FD As New OpenFileDialog()
            FD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
            If FD.ShowDialog = Windows.Forms.DialogResult.OK Then
                ListBox1.Items.Clear()
                ListBox1.Items.AddRange(IO.File.ReadAllLines(FD.FileName))
            End If
        End Using
    End Sub
  
    Private SubButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton2.Click
        Close()
    End Sub
End Class

 Output: