Saturday, 14 September 2013

VB.Net - SaveFileDialog Control

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.NPropertyDescription
1AddExtensionGets or sets a value indicating whether the dialog box automatically adds an extension to a file name if the user omits the extension.
2CheckFileExistsGets or sets a value indicating whether the dialog box displays a warning if the user specifies a file name that does not exist.
3CheckPathExistsGets or sets a value indicating whether the dialog box displays a warning if the user specifies a path that does not exist.
4CreatePromptGets 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.
5DefaultExtGets or sets the default file name extension.
6DereferenceLinksGets 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).
7FileNameGets or sets a string containing the file name selected in the file dialog box.
8FileNamesGets the file names of all selected files in the dialog box.
9FilterGets 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.
10FilterIndexGets or sets the index of the filter currently selected in the file dialog box.
11InitialDirectoryGets or sets the initial directory displayed by the file dialog box.
12OverwritePromptGets or sets a value indicating whether the Save As dialog box displays a warning if the user specifies a file name that already exists.
13RestoreDirectoryGets or sets a value indicating whether the dialog box restores the current directory before closing.
14ShowHelpGets or sets a value indicating whether the Help button is displayed in the file dialog box.
15SupportMultiDottedExtensionsGets or sets whether the dialog box supports displaying and saving files that have multiple file name extensions.
16TitleGets or sets the file dialog box title.
16ValidateNamesGets 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.NMethod Name & Description
1OpenFile
Opens the file with read/write permission.
2Reset
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:
  1. Drag and drop a Label control, a RichTextBox control, a Button control and a SaveFileDialog control on the form.
  2. Set the Text property of the label and the button control to 'We appreciate your comments' and 'Save Comments' respectively.
  3. 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 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.

No comments:

Post a Comment