3 Replies - 6869 Views - Last Post: 30 October 2009 - 09:48 PM Rate Topic: -----

#1 shockin!  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 136
  • Joined: 19-October 09

Open File to TextBox

Posted 30 October 2009 - 08:55 PM

I'm looking to, using an OpenFileDialog, open a file to a TextBox, named TextBox1.

When you press the Button, Button1, it'll show you an OpenFileDialog
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click		  

		 OpenFileDialog1.Title = "Please Select a File"
		 OpenFileDialog1.InitialDirectory = "C:temp"		  

		 OpenFileDialog1.ShowDialog()		  

	 End Sub


That's the code for my open file dialog, and now I'm confused as how to have the file opened using that file open dialog open up into the TextBox1.Text area.

Is This A Good Question/Topic? 0
  • +

Replies To: Open File to TextBox

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Open File to TextBox

Posted 30 October 2009 - 09:16 PM

You could use a StreamReader from the System.IO class. To make things a little easier make sure to import the System.IO namespace. This is how to open and read a text file to a string using the StreamReader class. You should be able to use something similar to this in your program.

Dim textFile As StreamReader
Dim fileContents As String

textFile = File.OpenText(FileName)
fileContents = textFile.ReadToEnd()

textFile.Close()


Was This Post Helpful? 0
  • +
  • -

#3 shockin!  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 136
  • Joined: 19-October 09

Re: Open File to TextBox

Posted 30 October 2009 - 09:23 PM

View PostSixOfEleven, on 30 Oct, 2009 - 08:16 PM, said:

You could use a StreamReader from the System.IO class. To make things a little easier make sure to import the System.IO namespace. This is how to open and read a text file to a string using the StreamReader class. You should be able to use something similar to this in your program.

Dim textFile As StreamReader
Dim fileContents As String

textFile = File.OpenText(FileName)
fileContents = textFile.ReadToEnd()

textFile.Close()



Sorry I'm a bit spacey right now, so that confused me. Here's most of the code for what I'm working with.

		If ow1.Checked Then

			OpenFileDialog1.Title = "File Shredder"
			OpenFileDialog1.InitialDirectory = "C:\"
			OpenFileDialog1.ShowDialog()

		End If


ow1 is a RadioButton, and it's in a list out of 10 buttons total.

So what I'm trying to say is can you elaborate on that a little more?

Thanks : P

This post has been edited by shockin!: 30 October 2009 - 09:24 PM

Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Open File to TextBox

Posted 30 October 2009 - 09:48 PM

View Postshockin!, on 30 Oct, 2009 - 10:23 PM, said:

Sorry I'm a bit spacey right now, so that confused me. Here's most of the code for what I'm working with.

		If ow1.Checked Then

			OpenFileDialog1.Title = "File Shredder"
			OpenFileDialog1.InitialDirectory = "C:\"
			OpenFileDialog1.ShowDialog()

		End If


ow1 is a RadioButton, and it's in a list out of 10 buttons total.

So what I'm trying to say is can you elaborate on that a little more?

Thanks : P


What you would want to do is after you have returned from OpenFileDialog1.ShowDialog() is to use a StreamReader to open and read the file. You would probably want to make sure that you check that the user had clicked the OK button of OpenFileDialog1. You would also probably want to make sure that OpenFileDialog1.CheckFileExists is set to true. What that will do is make sure that your program will not throw an exception if they user enters a file that does not exist. Inside of your If statement you would want to create a StreamReader object. Open the file, read the contents of the file into TextBox1.Text. Make sure that you close the file when you are done with it. You could try something like the following.

		
				If ow1.Checked Then

			OpenFileDialog1.Title = "File Shredder"
			OpenFileDialog1.InitialDirectory = "C:\"
			OpenFileDialog1.ShowDialog()

						Dim textFile As StreamReader

						textFile = File.OpenText(FileName)
						TextBox1.Text = textFile.ReadToEnd()

						textFile.Close()
		End If

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1