Use of OpenFileDialog-----C # Programming 01

Source: Internet
Author: User

The OpenFileDialog (Open File Dialog) component of Windows forms is a pre-configured dialog box. It is the same as the Windows operating system Open File dialog box. The control is inherited from the CommonDialog class. In a Windows-based application, this component enables a simple file selection without having to configure its own dialog box. The Standard Windows dialog box allows you to create an application interface that users are familiar with.

The main properties of the OpenFileDialog component include:


1. Title property: Gets or sets the file dialog box caption. The default value is an empty string ("").
If the title is an empty string, the system uses the default title of "Open".


2. MultiSelect properties: Gets or sets a value that indicates whether the dialog box allows multiple files to be selected.
True if the dialog box allows multiple files to be selected at the same time, or false. The default value is False. Use the FileNames property to access the complete list of selected file names.


3. ShowReadOnly properties: Gets or sets a value that indicates whether the dialog box contains a read-only check box.
True if the dialog box contains a read-only check box, otherwise false. The default value is False.


4. readonlychecked properties: Gets or sets a value that indicates whether the read-only check box is selected.
True if the read-only check box is selected, or false instead. The default value is False.


5. Filter Property: Gets or sets the current file name filter string that determines the selection that appears in the File type box of the dialog box.
For each filtering option, the filter string contains a description of the filter condition followed by a vertical line (|) and a filter mode. Strings for different filtering options are separated by vertical lines.

For example: "Text files (*.txt) |*.txt| All Files (*. *) |*.* ". If you want to add multiple filtering modes to the filter, you can separate the file types with semicolons, for example:

The Image Files (*. bmp;*. jpg;*. GIF) |*. bmp;*. jpg;*. Gif| All Files (*. *) |*.* " Use the FilterIndex property to set the first filtering option that is displayed to the user.

  6 . FilterIndex properties: Gets or sets the index of the currently selected filter condition in the file dialog box. The default value is 1. The index value of the first filtered entry is 1.


7. FileName Property: Gets or sets a string containing the file name selected in the file dialog box. That is, the file name selected in the dialog box, the default value is an empty string ("").
The filename contains both the file path and the extension. If the file is not selected, an empty string is returned.


8. FileNames properties: Gets the file name of all selected files in the dialog box.
The property value is an array of type String. Each filename contains both a file path and a file name extension. If the file is not selected, the method returns an empty array.

9. InitialDirectory property: Gets the initialization path.

The main methods of OpenFileDialog components include:


1.ShowDialog () Method: Displays the dialog box at run time.


2, OpenFile () method: Opens a user-selected file with read-only permission, specified by the FileName property.
Use this method to quickly open a file from a dialog box in read-only mode.


The following example briefly describes how to use the Open File dialog box (OpenFileDialog) component to select a file's programming technique, as follows:

(1) Start VS2012, create a new C # Windows Application project

(2) Add two label controls to the programming form, set their Text property to "FilePath", "FileName", add a TextBox control, leave its default properties, and add a button control that sets its Text property to " Open Files ". Resize forms and controls to fit the window. As shown below:

(3) Add the following code for the mouse click event for the button control (Open Files):

1OpenFileDialog OpenFileDialog =NewOpenFileDialog ();//declaring and initializing the OpenFileDialog class2OpenFileDialog. InitialDirectory =@"e:\";//set File Open initial directory for e-drive3OpenFileDialog. Title ="Open a text file";//Set the Open File dialog box title4OpenFileDialog. Filter ="All files (*. *) |*.*|txt Files (*.txt) |*.txt";//Set file filter type5OpenFileDialog. FilterIndex =2;//Set file filter type based on file type index6OpenFileDialog. Restoredirectory =true;//sets whether the dialog box remembers the previously opened directory7             if(OpenFileDialog. ShowDialog () = = DialogResult.OK)//Open the appropriate file when you click the OK button in the file dialog box8{//and execute the following block of statements9Tbfilepath. Text = OpenFileDialog. FileName;//gets the full path name of the selected file (with file name)TenTbfilename. Text = Tbfilepath. Text.substring (Tbfilepath. Text.lastindexof ("\\")+1); One}//gets the full filename of the selected file (without the path)
View Code

(4) Run the instance program, click the "Select File" button will pop up your familiar Windows "open" dialog box, after selecting the file, the file name and file path will be displayed in the form's text box.

Use of OpenFileDialog-----C # Programming 01

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.