bunze
Well-known member
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?
[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]
' 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()
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()
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