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
-----------------------------------------------------------
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