Difference between "Save" and "Save As"

aman_VB

Member
Joined
Apr 27, 2010
Messages
23
Programming Experience
1-3
I have a TextBox, btnOpen, btnSave and btnSaveAs in Form1. I have no problem instantiating btnOpen and btnSaveAs to display a dialog box and read/write a simple text. The problem is how do you instantiate btnSave without showing the dialog box?
-----------------------------------------------------------
Imports System.IO

Public Class Form1

Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
Dim myStream As Stream = Nothing
Dim myStreamReader As System.IO.StreamReader
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "C:\Documents\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = openFileDialog1.OpenFile()
myStreamReader = System.IO.File.OpenText(openFileDialog1.FileName)
TextBox1.Text = myStreamReader.ReadToEnd
myStreamReader.Close()
If (myStream IsNot Nothing) Then
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
' Check this again, since we need to make sure we didn't throw an exception on open.
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
End If

End Sub

Private Sub btnSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveAs.Click
Dim myStream As Stream
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = saveFileDialog1.OpenFile()
Using sw As StreamWriter = New StreamWriter(myStream)
sw.WriteLine(TextBox1.Text)
sw.Close()
End Using
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End If
End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim myStream As Stream
Using sw As StreamWriter = New StreamWriter(myStream) '<---Error message: Value can not be null
sw.WriteLine(TextBox1.Text)
sw.Close()
End Using
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Sub
End Class
 
A Save button should save to an existing file if there is one or, if there isn't, it should behave EXACTLY the same as a Save As button. That implies that they should both use the same code in part. I would tend to implement that something like this:
VB.NET:
Private filePath As String

Private Sub openButton_Click() Handles openButton.Click
    Using dialogue As New OpenFileDialog
        'Configure dialogue here.

        If dialogue.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Me.filePath = dialogue.FileName

            'Open file here.
        End If
    End Using
End Sub

Private Sub saveButton_Click() Handles saveButton.Click
    If Me.filePath Is Nothing Then
        Me.SaveAs()
    Else
        Me.Save()
    End If
End Sub

Private Sub saveAsButton_Click() Handles saveAsButton.Click
    Me.SaveAs()
End Sub

Private Sub Save()
    'Save file using current filePath.
End Sub

Private Sub SaveAs()
    Using dialogue As New SaveFileDialog
        'Configure dialogue here.

        If dialogue.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Me.filePath = dialogue.FileName
            Me.Save()
        End If
    End Using
End Sub
 
Back
Top