2 Buttons (btnLoad,btnSave)
1 RichTextBox (rtbFileContents)
1 OpenFileDialog component (ofdOpenFile)
1 SaveFileDialog component (sfdSaveFile)
First you need to be at the designer of the form. Open VS toolbox and
find the Dialogs tab. Drag a OpenFileDialog component onto the form, do
the same with a SaveFileDialog. These 2 items will show up in your
component tray at the bottom of the form. Select the OpenFileDialog,
look at the properties for it and change its name to 'ofdOpenFile'.
Select the SaveFileDialog, look at the properties for it and change its
name to sfdSaveFile.
1 RichTextBox (rtbFileContents)
1 OpenFileDialog component (ofdOpenFile)
1 SaveFileDialog component (sfdSaveFile)
First you need to be at the designer of the form. Open VS toolbox and
find the Dialogs tab. Drag a OpenFileDialog component onto the form, do
the same with a SaveFileDialog. These 2 items will show up in your
component tray at the bottom of the form. Select the OpenFileDialog,
look at the properties for it and change its name to 'ofdOpenFile'.
Select the SaveFileDialog, look at the properties for it and change its
name to sfdSaveFile.
- Code:
Public Class Form1
Dim FileName As String = ""
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
sfdSaveFile.Filter = "Text Files(*.txt)|*.txt|Web Files(*.html,*.htm)|*.html;*.htm|PHP Files(*.php)|*.php"
sfdSaveFile.FilterIndex = 1
sfdSaveFile.InitialDirectory = "C:\"
sfdSaveFile.AddExtension = True
sfdSaveFile.Title = "Choose file to save."
If String.IsNullOrEmpty(FileName) Then
sfdSaveFile.FileName = "MyFile"
Else
sfdSaveFile.FileName = FileName
End If
If sfdSaveFile.ShowDialog() = Windows.Forms.DialogResult.OK Then
FileName = sfdSaveFile.FileName
My.Computer.FileSystem.WriteAllText(FileName, rtbFileContents.Text, False)
End If
End Sub
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
ofdOpenFile.Filter = "Text Files(*.txt)|*.txt|Web Files(*.html,*.htm)|*.html;*.htm|PHP Files(*.php)|*.php"
'1 based array so first item is 1
ofdOpenFile.FilterIndex = 1
ofdOpenFile.Multiselect = False
ofdOpenFile.InitialDirectory = "C:\"
ofdOpenFile.FileName = ""
ofdOpenFile.Title = "Choose file to load."
If ofdOpenFile.ShowDialog() = Windows.Forms.DialogResult.OK Then
'load the file that was selected into the Richtextbox
FileName = ofdOpenFile.FileName
rtbFileContents.Text = My.Computer.FileSystem.ReadAllText(FileName)
End If
End Sub
End Class