45. SaveFileDialog

In this tutorial, we cover the SaveFileDialog object and how it can be integrated with the StreamWriter to save a string to a text file with your desired file name.

Code

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SaveFileDialog1.ShowDialog()
    End Sub

    Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
        Dim FileToSaveAs As String = SaveFileDialog1.FileName

        Dim objwriter As New System.IO.StreamWriter(FileToSaveAs)
        objwriter.Write(TextBox1.Text)
        objwriter.Close()
    End Sub

End Class