8.4 Displaying an Open File Dialog Box

 <  Day Day Up  >  

8.4 Displaying an Open File Dialog Box

You want to allow the user to browse for a file using the standard Windows open file dialog box.


Technique

You can create an open file dialog box using the forms designer. Open the form in the forms designer that will need to have access to an open file dialog box, and drag and drop the OpenFileDialog control from the toolbox onto the form. The OpenFileDialog is a nonvisual control, so it is displayed at the bottom of the form designer. Select the control and change any properties appropriate to your application. The most common properties associated with an OpenFileDialog include the Filter , Title , and Multiselect properties.

To display the open file dialog to the user, call the ShowDialog method from the object that was generated when you created the control. This method returns a DialogResult enumerated data type. If the user clicks the Open button, then it returns the value DialogResult.OK .

Comments

In addition to common visual controls, the forms designer also lets you create instances of nonvisual controls using the same drag and drop methods . To distinguish visual and nonvisual controls from each other, the nonvisual controls appear at the bottom of the forms designer within a list view. This view displays the toolbox icon of the control as well as the generated private member variable that was created within the form class.

You will undoubtedly have to change the value of three properties. The Title , as you have probably guessed, changes the caption of the open file dialog when it appears. The Filter property controls the file type drop-down box on the open file dialog. The Filter property is a string that uses a pipe ( ) separated list of values for its display. These values are grouped using a friendly name for the file type separated by the file extension of that file type using a wildcard specification. For instance, to specify two items in the drop-down box where the first item displays all text ( .txt ) files and the second item displays all files regardless of file type, you use the following to specify the Filter property:

 
 this.openFileDialog1.Filter = "Text Files*.txtAll Files*.*"; 

The Multiselect property allows a user to select multiple files at one time. With a single-selection open file dialog box, you can access the path of the file the user selected through the FileName property of the OpenFileDialog object. If Multiselect is True , however, you have to access each file using the FileNames collection. It is a collection of strings containing the path of the files that you can use in a foreach loop or other enumeration process.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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