I've created my program.
I want to open a .txt file and display it in my text box

I have named my text box textbox1 and my open file dialog is openfiledialog1

How do I do this?

Recommended Answers

All 13 Replies

Here you have

OpenFileDialog1.CheckFileExists = True
OpenFileDialog1.CheckPathExists = True
OpenFileDialog1.DefaultExt = "txt"
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
OpenFileDialog1.Multiselect = False
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
   TextBox1.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
End If

By the way, I gave this snippet to you already?

it doesn't work

my.computer is underlined. It says that MY is not declared so when I put Dim My As String it is still underlined and says that COMPUTER is not a member of string.

what do I do to correct it

thanks in advance 4 any help

In his code above, just remove the My.Computer.

Removing My.Computer does not work because the code would then refer to Microsoft.VisualBasic.Filesystem namespace which does not have ReadAllText method.

Here's an alternative code which uses System.IO

Imports System.IO

Dim oReader As StreamReader

OpenFileDialog1.CheckFileExists = True
OpenFileDialog1.CheckPathExists = True
OpenFileDialog1.DefaultExt = "txt"
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
OpenFileDialog1.Multiselect = False
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
  oReader = New StreamReader(OpenFileDialog1.FileName, True)
  TextBox1.Text = oReader.ReadToEnd
End If

I didn't check if this is compatible with VB2003/.NET 1.1

ah, yes... I was thinking file.readall :/

OK.
I'll try that new code ya gave
:)

Here's an alternative code which uses System.IO

Imports System.IO

Dim oReader As StreamReader

OpenFileDialog1.CheckFileExists = True
OpenFileDialog1.CheckPathExists = True
OpenFileDialog1.DefaultExt = "txt"
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
OpenFileDialog1.Multiselect = False
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
  oReader = New StreamReader(OpenFileDialog1.FileName, True)
  TextBox1.Text = oReader.ReadToEnd
End If

I didn't check if this is compatible with VB2003/.NET 1.1

This doesn't work either :/
oReader.ReadToEnd AND StreamReader are underlined

says they are not defined. when I dim them as string
still underlined.

HELP!?

My question is: Do you understand what they posted for you or are you just copy/paste and trying a few different things?

Do you have to change the imports to

Imports System.IO.StreamReader

I'm not sure, I know StreamReader is in IO . . .

I understand MOST of what they posted
but I dont understand the whole streamreader thing

don't get why it dont work.

me either - did you try to add StreamReader after IO as I suggested above? (I edited the post after original post, so you might not have seen the suggestion . . .)

Use RichTextBox instead of Textbox

RichTextBox1.LoadFile("e:\Encyclopedia.txt", RichTextBoxStreamType.PlainText)

this is my current code:

OpenFileDialog1.ShowDialog()
        OpenFileDialog1.Title = "Open Text File:"
        OpenFileDialog1.Filter = "Text Files|*.txt"

I just wanna select a file when the dialog appears and then have it display in the text box!

help please

Use RichTextBox instead of Textbox

RichTextBox1.LoadFile("e:\Encyclopedia.txt", RichTextBoxStreamType.PlainText)

nice n simple :)
thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.