SaveFileDialog in Visual Basic .NET
Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013
This lesson describes the SaveFileDialog control
Visual Basic offers a wide range of dialog boxes that allow the user to make standard selections and each of them has a very specific purpose.
The purpose of the SaveFileDialog is selecting a file for saving.
To display a file saving dialog, this component calls its ShowDialog method, which returns DialogResult.OK if the user selects a file and clicks OK, and DialogResult.Cancel if the user cancels.
This component provides a lot of properties for determining what kind of files the user can specify.
For the truth's sake, most of these properties are the same as those provided by the OpenFileDialog component described earlier. For more information about those properties see the lesson OpenFileDialog
Unlike OpenFileDialog, this component does not provide the properties MultiSelect, ReadOnlyChecked, and ShowReadOnly because those properties don't make sense when the user is saving a file.
The FileNames collection is also less useful for this component because the user will always select only one file, so you can use the FileName property instead.
The SaveFileDialog component provides one additional property which is CreatePrompt, and if this property is True and the user enters the name of a file that doesn't exist, the dialog asks the user if it should create the file.
If the user clicks No, the dialog box continues letting the user select a different file.
Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click SaveFileDialog1.DefaultExt = "*.rtf" SaveFileDialog1.Filter = "RTF Files|*.rtf" SaveFileDialog1.CreatePrompt = True If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.RichText) End If End SubPrevious Lesson | Next Lesson