OpenFileDialog Class

   
OpenFileDialog Class

Namespace

System.Windows.Forms

Createable

Yes

Description

Represents a common dialog box for selecting or opening a file.

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

Under VB, the most common use for this dialog box is to get the name of a file from the user, after which we can use VB's functions to open that file.

An OpenFileDialog object can be instantiated as follows :

 Dim oOpenDlg As New OpenFileDialog 

Selected OpenFileDialog Members

The following is a brief description of some of the more important members of the OpenFileDialog 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 .

CheckFileExists property

Sets or retrieves a Boolean value indicating whether a warning message should be displayed if the user enters the name of a file that does not exist. The 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

Returns a string that contains the fully qualified name (that is, complete path and filename) of the file selected by the user. If no file is selected, the property returns an empty string.

FileNames property

Returns a String array that contains the fully qualified names (that is, complete paths and filenames) of the files selected by the user. If no file is selected, the property returns an empty array. Note that this property returns a single- element array if the Multiselect property is False and the user selects a file.

Filter property

Gets or sets a String containing the current filter, which determines the items that appear in the "Files of 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 = oFS.Filter="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 are selected. The index is one-based, rather than zero- based. When the dialog 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 OpenFileDialog dialog

Multiselect property

Sets or retrieves a Boolean value indicating whether the user is allowed to select more than one file.

OpenFile method

Opens the file selected by the user, returning a Stream object. The file is opened in read-only mode. As Microsoft puts it: "The OpenFile method is used to provide a facility to quickly open a file from the dialog box. The file is opened in read-only mode for security purposes. To open a file in a read/ write mode, you must use another call . . . "

ReadOnlyChecked property

Sets or retrieves a Boolean value indicating whether the read-only checkbox is selected on the dialog box.

RestoreDirectory property

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

ShowDialog method

The OpenFileDialog class inherits from the FileDialog class, which in turn inherits from the CommonDialog class. This class has a ShowDialog method that shows the dialog box. Once the user has dismissed the dialog box, the FileDialog's FileName and FileNames properties can be used to get the user's choice(s).

ShowReadOnly property

Sets or retrieves a Boolean value indicating whether the dialog box contains a read-only checkbox.

Title property

Gets or sets a String value containing the title of the Open dialog box.

Example

The following code asks the user for one or more files and displays the filenames in the Output window:

 Dim fd As New OpenFileDialog(  ) Dim i As Integer fd.Multiselect = True If fd.ShowDialog(  ) = DialogResult.OK Then     For i = 0 To UBound(fd.FileNames)         Console.WriteLine(fd.FileNames(i))     Next End If 

VB.NET/VB 6 Differences

Whereas the OpenFileDialog 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.

   


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