Ok so I am trying to save a file and I want to limit the user to a couple options being *.txt and . . I want to make the default *.txt. The code works to save the file, but I don't see the "save as type" option as having any choices and the default .txt extension is not in the file name field. What am I missing or doing wrong?
Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
'Write the converted string to the file
Dim FileToSaveAs As String = SaveFileDialog1.FileName
Dim FileWriter As New StreamWriter(FileToSaveAs)
With SaveFileDialog1
.DefaultExt = ".txt"
.FilterIndex = 2
.RestoreDirectory = True
.Filter = "Text Files (*.txt)|*.txt|All files (*.*)|*.*"
.InitialDirectory = "C:\AppTemp"
.Title = "Save processed file"
End With
FileWriter.Write(TextOut)
FileWriter.Close()
End Sub
You have to set SaveFileDialog params before you show it.
Try this:
public sub SaveFile
With SaveFileDialog1
.DefaultExt = ".txt"
.FilterIndex = 1
.RestoreDirectory = True
.Filter = "Text Files (*.txt)|*.txt|All files (*.*)|*.*"
.InitialDirectory = "C:\AppTemp"
.Title = "Save processed file"
If .ShowDialog() = DialogResult.OK then
Dim FileWriter As New StreamWriter(.Filename)
FileWriter.Write(TextOut)
FileWriter.Close()
End If
End With
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.