Windows common dialogs - OpenFileDialog
Explains how to integrate the windows common task "open file dialog box" in our application.
Windows common dialogs are the dialog boxes that most windows applications display while performing some common tasks. Some examples are the dialog boxes shown while opening and saving files in applications like MS-Word, MS-Excel, Notepad and MS-Paint, etc. Though these applications differ in their purpose and use, the dialog boxes are the same in all of them. The following image shows the Open dialog box of Notepad:
The Visual C# IDE presents some very useful common dialog controls and we can implement them in our application for file handling and some other tasks. We will learn about them one by one and implement them in our application.
OpenFileDialog - The OpenFileDilaog control is used to display a dialog box for opening files. Let us now add this control to our application:
1. Open the project in the IDE. Drag the OpenFileDialog control from the Toolbox (it is in the Dialogs section) and drop it on the form. Note that we will not get any visible interface of the control. However, the control tray will display the control added to it. The default name provided for the control is openFileDialog1. This is the name to access the control in our code. Though we can change the name, we are going to use this default name for our example application.
2. Select the control by clicking it in the control tray. We will now set some properties of the control using the properties window:
|
Property |
Value |
|
Filter |
This property sets the type of files that can be opened in our application. Since the application works with text files, set the Filter property to - Text Files | *.txt |
|
FileName |
*.txt |
|
ShowReadOnly |
True - This will display a check box that gives the user the option to open the file in read only mode. |
|
Title |
A title for the dialog box. We will use the title `Open File' for our example. |
3. Switch to the code view in the IDE. Add the following line of code at the top, just below the included namespaces:
using System.IO;
We have included this namespace to access the file input-output classes. Now, just below the class definition for Form1, declare a string variable:
namespace Writer { public partial class Form1 : Form { private string myFile ="newfile";
4. Now we will add a method for opening the file. Let us call this method OpenFile(). Here is the code:
void OpenFile() { if (openFileDialog1.ShowDialog() == DialogResult.OK) { myFile = openFileDialog1.FileName;
txtInput.Clear(); txtInput.Text = File.ReadAllText(myFile); } }
Inside the `if' statement, the ShowDialog() method of openFileDialog1 will display the File open dialog box. If the user selects a file and clicks the OK button, the selected file will be referenced by the FileName property of the openFileDialog1 (openFileDialog1.FileName). This file name will then be assigned to the string variable `myFile' that we have declared earlier.
Next, the Clear() method of the textbox control (txtInput.Clear()) will clear the current contents, if any, of the textbox. Finally, we are using the ReadAllText() method of the File class to read the content of the file specified in the myFile variable (File.ReadAllText(myFile)). To use the File class, we have included the System.IO namespace. The ReadAllText() method will read the content of the file and assign it to the Text property of the textbox (txtInput.Text).
5. We will call this method from the click events of the Open menu option and the Open toolbar button. The following code calls the OpenFile() method from the click event of the Open menu option:
private void mnuFileOpen_Click(object sender, EventArgs e) { OpenFile(); }
6. Save the project and then run it. Click the Open menu option. We will get the dialog box to open a file:
Select a file and click the OK button to open it:


