VB.Net - SaveFileDialog Control


Advertisements


The SaveFileDialog control prompts the user to select a location for saving a file and allows the user to specify the name of the file to save data. The SaveFileDialog control class inherits from the abstract class FileDialog.

Following is the Save File dialog box:

VB.Net Save File Dialog Box

Properties of the SaveFileDialog Control

The following are some of the commonly used properties of the SaveFileDialog control:

S.N PropertyDescription
1 AddExtension Gets or sets a value indicating whether the dialog box automatically adds an extension to a file name if the user omits the extension.
2 CheckFileExists Gets or sets a value indicating whether the dialog box displays a warning if the user specifies a file name that does not exist.
3 CheckPathExists Gets or sets a value indicating whether the dialog box displays a warning if the user specifies a path that does not exist.
4 CreatePrompt Gets or sets a value indicating whether the dialog box prompts the user for permission to create a file if the user specifies a file that does not exist.
5 DefaultExt Gets or sets the default file name extension.
6 DereferenceLinks Gets or sets a value indicating whether the dialog box returns the location of the file referenced by the shortcut or whether it returns the location of the shortcut (.lnk).
7 FileName Gets or sets a string containing the file name selected in the file dialog box.
8 FileNames Gets the file names of all selected files in the dialog box.
9 Filter Gets or sets the current file name filter string, which determines the choices that appear in the "Save as file type" or "Files of type" box in the dialog box.
10 FilterIndex Gets or sets the index of the filter currently selected in the file dialog box.
11 InitialDirectory Gets or sets the initial directory displayed by the file dialog box.
12 OverwritePrompt Gets or sets a value indicating whether the Save As dialog box displays a warning if the user specifies a file name that already exists.
13 RestoreDirectory Gets or sets a value indicating whether the dialog box restores the current directory before closing.
14 ShowHelp Gets or sets a value indicating whether the Help button is displayed in the file dialog box.
15 SupportMultiDottedExtensions Gets or sets whether the dialog box supports displaying and saving files that have multiple file name extensions.
16 Title Gets or sets the file dialog box title.
17 ValidateNames Gets or sets a value indicating whether the dialog box accepts only valid Win32 file names.

Methods of the SaveFileDialog Control

The following are some of the commonly used methods of the SaveFileDialog control:

S.N Method Name & Description
1

OpenFile

Opens the file with read/write permission.

2

Reset

Resets all dialog box options to their default values.

Example

In this example, let's save the text entered into a rich text box by the user using the save file dialog box. Take the following steps:

  • Drag and drop a Label control, a RichTextBox control, a Button control and a SaveFileDialog control on the form.

  • Set the Text property of the label and the button control to 'We appreciate your comments' and 'Save Comments', respectively.

  • Double-click the Save Comments button and modify the code of the Click event as shown:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
   SaveFileDialog1.Filter = "TXT Files (*.txt*)|*.txt"
      If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK _
	  Then
         My.Computer.FileSystem.WriteAllText _
         (SaveFileDialog1.FileName, RichTextBox1.Text, True)
      End If
End Sub

When the application is compiled and run using Start button available at the Microsoft Visual Studio tool bar, it will show the following window:

VB.Net SaveFile Dialog Example

We have set the Filter property of the SaveFileDialog control to display text file types with .txt extensions only.

Write some text in the text box and click on the Save Comment button to save the text as a text file in your computer.


vb.net_dialog_boxes.htm

Advertisements