Ok this is the code you needed!
Code:
Imports System
Imports System.IO
Imports System.Windows.Forms
Namespace MoreFileOps2
Public partial Class Form1
Inherits Form
Private filename As String = ""
Public Sub New()
InitializeComponent()
End Sub
Private Sub OpenButton_Click(ByVal sender As Object, ByVal e As EventArgs)
OpenFile()
End Sub
Private Sub OpenFile()
Dim openFileDialog As OpenFileDialog = New OpenFileDialog()
openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
openFileDialog.CheckFileExists = False 'it would check for us, but we want to do this ourselves
openFileDialog.DefaultExt = "txt" 'they dont have to enter an extension if they dont want to
openFileDialog.AddExtension = True
If openFileDialog.ShowDialog(Me) = DialogResult.OK Then
Try
'if the file doesn't exist we'll throw our own exception
'because ReadAllText will try to read a nonexistant file
'and set our textbox to empty
If Not File.Exists(openFileDialog.FileName) Then
Throw New FileNotFoundException()
End If
richTextBox1.Text = File.ReadAllText(openFileDialog.FileName) 'this fills the textbox and closes the file
filename = openFileDialog.FileName 'save the filename for later. this also serves as a flag that a file is considered open
Catch FileNotFoundException
'This file does not exist. We'll ask them if they wish to create one
CreateSpecifiedFile(openFileDialog.FileName)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
'Notice that we are not actually leaving the file open. Since we are not writing to the file
'unless they click a save button, it doesnt need to stay open physically. The advantage is when they exit
'the program we don't have any file cleanup to do. The user doesn't know the file
'doesnt STAY open, and they dont care. As long as there is a filename in the filename var
'the file is virtually open. This is mostly just programmers preference, I like it. Its clean, simple and
'requires less file maintenance
End Sub
Private Sub richTextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
'this is a way to handle the error case if there is nothing in the textbox
'when save is clicked. We prevent the error from occurring by not enabling the buttons unless
'there is something to save. Another way to handle this would be always have the buttons enabled and put up a messagebox saying
'the textbox is empty, inside the save button handlers.
If richTextBox1.Text <> String.Empty Then
SaveButton.Enabled = True
SaveAsButton.Enabled = True
Else
SaveButton.Enabled = False
SaveAsButton.Enabled = False
End If
End Sub
Private Sub SaveButton_Click(ByVal sender As Object, ByVal e As EventArgs)
SaveFile()
End Sub
Private Sub SaveFile()
'if we have a filename already, just save it
If filename <> "" Then
'this handy utility opens the file, writes the text and closes the file
File.AppendAllText(filename, richTextBox1.Text)
Else
'we don't have a filename, so ask them if they want to create one
CreateFile()
End If
End Sub
Private Sub CreateFile()
If MessageBox.Show("No file has been specified. Do you wish to create one?","Create new file?",MessageBoxButtons.YesNoCancel) = DialogResult.Yes Then
'they do so call the SaveAsFile routine
SaveAsFile()
End If
End Sub
Private Sub SaveAsButton_Click(ByVal sender As Object, ByVal e As EventArgs)
SaveAsFile()
End Sub
Private Sub SaveAsFile()
'They want to do a SaveAs, so find out what they want to name the file
Dim saveFileDialog As SaveFileDialog = New SaveFileDialog()
saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
saveFileDialog.DefaultExt = "txt"
saveFileDialog.AddExtension = True
If saveFileDialog.ShowDialog(Me) = DialogResult.OK Then
filename = saveFileDialog.FileName 'save the filename for later. this also serves as a flag that a file is open
'our handy utility creates the file if needed
File.AppendAllText(filename, richTextBox1.Text)
End If
End Sub
Private Sub CreateSpecifiedFile(ByVal filename As String)
If MessageBox.Show(String.Format("File {0} does not exist. Do you wish to create it?",filename),"Create new file?",MessageBoxButtons.YesNoCancel) = DialogResult.Yes Then
Me.filename = filename 'save just the filename cause there is nothing to write
End If
End Sub
End Class
End Namespace
'----------------------------------------------------------------
' Converted from C# to VB .NET using CSharpToVBConverter(1.2).
Bookmarks