SaveFileDialog Class

   
SaveFileDialog Class

Namespace

System.Windows.Forms

Createable

Yes

Description

Represents a common dialog box for selecting or saving a file. The most common use of this dialog box is to ask the user for the name of a file, after which we can use VB's functions to save an existing file under that name , since the dialog box itself does not handle the process of saving a file.

The SaveFileDialog object has properties for setting the initial appearance and functionality of the dialog box, a property for returning the filename selected by the user, as well as a method for showing the dialog box. The object does not itself save the file, but instead provides the information that allows your code to do this programmatically.

A SaveFileDialog object can be instantiated as follows :

 Dim oSaveDlg As New SaveFileDialog 

Selected SaveFileDialog Members

The following is a brief list of some of the more important members of the SaveFileDialog class:

AddExtension property

Gets or sets a Boolean value that determines whether the default file extension is automatically added to the FileName property if the user fails to enter an extension. Its default value is True .

DefaultExt property

Gets or sets a String that defines the default file extension. The string should consist of the file extension only, without a period.

FileName property

Gets or sets a String containing the name that the user selected or entered in the dialog box.

Filter property

Gets or sets a String containing the current filter, which determines the items that appear in the "Save as type" drop-down listbox. A single item consists of a file description, a vertical bar, and the file extension (usually "*." plus the file extension). If there are multiple extensions in a single item, they are separated by semicolons. If there are multiple items, they are separated by vertical bars. For example, the following code fragment assigns a filter string to a String variable:

 sFilter = "Text files (*.txt; *.vb)*.txt;*.vb" & _            "Visual Basic files (*.vb)*.vb" & _            "All files (*.*)*.*" 
FilterIndex property

Gets or sets an Integer value that determines which of the items defined by the Filter property is selected. The index is one-based, rather than zero-based . When the dialog box is first displayed and no FilterIndex value is specified, it defaults to 1. When the method returns, its value indicates which filter item was selected by the user.

InitialDirectory property

Gets or sets a String that defines the directory initially displayed by the SaveFileDialog dialog box.

OverwritePrompt property

Gets or sets a Boolean value that determines whether a confirmation message is displayed when the user enters or selects an existing file.

RestoreDirectory

Gets or sets a Boolean value indicating whether the current directory is restored before the dialog box closes . Its default value is False .

ShowDialog method

Opens the SaveFileDialog dialog box. Its syntax is:

 oSaveDlg.ShowDialog(  ) 

It returns DialogResult.OK if the user clicks the OK button and DialogResult.Cancel if the user clicks the Cancel button to close the dialog box.

Example

 Dim fd As New SaveFileDialog(  ) fd.OverwritePrompt = True If fd.ShowDialog() = DialogResult(  ).OK Then     Console.WriteLine(fd.FileName) End If 

VB.NET/VB 6 Differences

Whereas the SaveFileDialog class is implemented in the .NET Base Class Library, VB 6 offered the CommonDialog custom control. Although the two offer similar functionality, their public interfaces are almost completely different.

See Also

OpenFileDialog Class

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net