Loading a .txt file into a textbox

bunze

Well-known member
Joined
Aug 4, 2005
Messages
52
Location
Adirolf
Programming Experience
1-3
How would I do this. I want to be able to click a button and it loads a specified (specified in the code) .txt file into a textbox. Also, how to click on a button and it opens the openfiledialog and I select the file?
 
Hope this helps:

VB.NET:
[color=Blue]Dim[/color] OFD [color=Blue]As New[/color] OpenFileDialog
[color=Blue]With [/color](OFD)
	.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
	.ShowDialog()
	[color=Blue]If [/color].FileName.Trim <> "" [color=Blue]Then[/color]
	[color=Blue]Select Case[/color] IO.File.Exists(.FileName)
		[color=Blue]Case True[/color]
[color=Blue]		Try[/color]
			[color=Blue]Dim [/color]oFile [color=Blue]As[/color] System.IO.File
			[color=Blue]Dim [/color]oRead [color=Blue]As[/color] System.IO.StreamReader
			[color=Blue]Dim[/color] tCustomerList [color=Blue]As String[/color]
			oRead = oFile.OpenText(.FileName)
			tCustomerList = oRead.ReadToEnd
			TextBox1.Text = tCustomerList
		[color=Blue]Catch[/color] ex [color=Blue]As Exception[/color]
			MessageBox.Show("Unable to open '" & .FileName & "'", "Error")
		   [color=Blue] Exit Sub[/color]
		[color=Blue]End Try[/color]
		[color=Blue]Case False[/color]
		MessageBox.Show("File '" & .FileName & "' does not exist", "Error")
		[color=Blue]Exit Sub[/color]
	[color=Blue]End Select[/color]
	[color=Blue]End If[/color]
[color=Blue]End With[/color]
 
Thank you that worked but have a new problem. I open a file, edit it, and click on my save button. Then if I reopon it, it has the original text and the text i changed. I cannot totally overwrite it for some reason, just add to it. Can you tell me what is wrong with my save code?

VB.NET:
' Create a SaveFileDialog to request a path and file name to save.
		Dim SaveFileDialog3 As New SaveFileDialog()
		Dim s3 As System.IO.StreamWriter
		If EditTextbox.Text = "" Then
		    MessageBox.Show("There is nothing to save.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
		Else
			If FileNameTextBox.Text = "" Then
			    ' Initialize the SaveFileDialog to specify the RTF extension for the file.
			    SaveFileDialog3.Filter = "cfg file|*.cfg"

			    ' Determine if the user selected a file name from the saveFileDialog.
			    If (SaveFileDialog3.ShowDialog() = System.Windows.Forms.DialogResult.OK) And (SaveFileDialog3.FileName.Length) > 0 Then

				    ' Save the contents of the TextBox into the file.
				    s3 = New System.IO.StreamWriter(SaveFileDialog3.FileName, True, System.Text.Encoding.ASCII)
				    s3.WriteLine(EditTextbox.Text)
				    s3.Close()
 
actually ayozzhero did forget to check for 1 thing in his openfiledialog example

what if the user clicks cancel?
also it's considered bad programming to use Exit statements
also i put my file opening and saving into functions:

VB.NET:
Friend Function OpenTextFile() As String
  Dim OFD As New OpenFileDialog
  Dim dlgResult As DialogResult
  Dim strFileContents As String
  With OFD
	.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
	.FileName = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\*.txt"
	dlgResult = .ShowDialog()
  End With
  If dlgResult <> DialogResult.Cancel Then
	Try
	  Dim srRead As New System.IO.StreamReader(OFD.FileName)
	  strFileContents = srRead.ReadToEnd
	  srRead.Close
	  Return strFileContents
	Catch
	  Return String.Empty
	End Try
  End If
End Function

To use this function simply call it like so:
Textbox1.Text = OpenTextFile()

and for saving the file i'm assuming that the textbox has multiline set to true so all you need to do is save the contents of the file
VB.NET:
Friend Function SaveText(Byval Text As String) As Boolean
  Dim SFD As New OpenFileDialog
  Dim dlgResult As DialogResult
  With SFD
	.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
	.FileName = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\*.txt"
	dlgResult = .ShowDialog()
  End With
  If dlgResult <> DialogResult.Cancel Then
	Try
	  Dim swWrite As New System.IO.StreamWriter(SFD.FileName, False)
	  swWrite.Write(Text)
	  swWrite.Close
	  Return True
	Catch
	  Return False
	End Try
  Else
  Return False
  End If
End Function

To use this function simple call it like so:
If SaveText(Textbox1.Text) = False Then
  Messagebox.Show("Error Saving Text file")
End If

also a side note, the openfiledialog and savefiledialog's both check to see if the filename (and path) exists before it gets assigned to the FileName property also it trims the filename(s) as well
 
Last edited:
Thank you but for the openfile one, I have to have the last line inside of something, do I stick it inside the function or put it in a procedure?

and the savefile, how do i call the function? can i just say "call savefile" from any procedure, like my savebutton_click?
 
the line Textbox1.Text = OpenTextFile() is how you call the function that opens the file and the function returns the contents of that file (which then gets displayed in the textbox)

to save a text file you use the:
If SaveText(Textbox1.Text) = True Then that means that it successfully saved the file but if it = False then you can display an error to the user to notify them that the file couldn't be saved at that time or use whatever error handling method you want
 
Back
Top