VB - Open a txt file

Webnode

Hello, in this tutorial I'll teach you how to open text files using visual basic.

1. Start by creating a new project.
2. Add :
   2x Textbox
   1x Button
   1x OpenFileDialog

Your form should look like this:

3. Click on the OpenFileDialog1 and change the following properties:

*FileName:OpenFileDialog1 becomes FileName:File

4. Double click the button and add the following codes:
OpenFileDialog1.ShowDialog()

5. Go back to the design of your form and double click the OpenFileDialog1 component. Add the following codes:
Textbox1.Text = OpenfileDialog1.FileName

6. Again, add the following codes to OpenFileDialog1:
Dim Doom As New System.IO.StreamReader(TextBox1.Text)
  TextBox2.Text = Doom.ReadLine()
Doom.Close()

It should now look like this:

7. Click F5 or run your application.
When you press the button, it will ask you to select a file. Select a textfile and it will read it and place the text in textbox2

Download the source codes:
OpenFileDialog.rar (62,3 kB)

 


Topic: VB - Open a txt file

No comments found.