Home

The Open/Save Dialog Boxes

 

Introduction to the Open File Dialog Box

 

Description

Besides saving files, another common operation performed by users consists of opening files. This refers to existing files since a file must primarily exist. To support this operation, Microsoft Windows provides a standard object: the Open File dialog box:

Description of the Open File Dialog Box

The Open File Dialog Box Creation

To provide file opening support, the .NET Framework provides the OpenFileDialog class which is derived from the FileDialog class that in fact provides most of its functionality:

type OpenFileDialog =  
    class 
        inherit FileDialog 
    end

To create an Open dialog box, declare a variable of type OpenFileDialog and use the new operator to allocate memory using its default constructor. Here is an example:

let ofd = new OpenFileDialog()
 
   
 

Characteristics of an Open File Dialog Box

 

The Filename

One of the most important properties of an Open dialog box is the file it presents to the user. This is represented by the FileName property:

member FileName : string with get, set

If you want a default file to be specified when the dialog box comes up, you can specify this using the FileName property. If you need to use this property, you should make sure the file can be found. If the file is located in the same folder as the application, you can provide just its name. If the file is located somewhere else in the hard drive, you should provide its complete path. Most of the time, you will not be concerned with this property if you are creating an application that will allow the users to open any files of their choice. Once a file is located, it can be accessed using the OpenFileDialog.FileName property.

The Filter

To make your application more effective, you should know what types of files your application can open. This is taken care of by specifying a list of extensions for the application. To control the types of files that your application can open, specify their extensions using the Filter property:

member Filter : string with get, set

The Filter string is created exactly like that of a SaveFileDialog control as we saw earlier. Here is an example:

let ofd = new OpenFileDialog()
ofd.Filter <- "HTML Files (*.htm)|*.htm|" +
              "Active Server Pages (*.asp)|*.asp|" +
              "Apache Files (*.php)|*.php|" +
              "Perl Script (*.pl)|*.pl|" +
              "All Files|"

The Filter Index

Like the SaveFileDialog control, the default extension is the one the dialog box would first filter during file opening. If you want the Open dialog box to easily recognize a default type of file when the dialog box opens, you can specify the extension's type using the DefaultExt property:

member DefaultExt : string with get, set

You can also use the FilterIndex we saw earlier to indicate the default index of the Files of Type combo box.

The Initial Folder

For convenience, or for security reasons, Open dialog boxes of applications are sometimes asked to first look for files in a specific location when the Open File dialog box comes up. This default folder is specified using the InitialDirectory property:

member InitialDirectory : string with get, set

Showing the Dialog Box

The essence of using the Open dialog box is to be able to open a file. This job is handled by the ShowDialog() method. Here is an example:

open System
open System.Windows.Forms

let exercise : Form = new Form()

let ofd = new OpenFileDialog()
ofd.Filter <- "HTML Files (*.htm)|*.htm|" +
              "Active Server Pages (*.asp)|*.asp|" +
              "Apache Files (*.php)|*.php|" +
              "Perl Script (*.pl)|*.pl|" +
              "All Files|"
ofd.DefaultExt <- "htm"

ofd.ShowDialog() |> ignore

[<STAThread>]
[<EntryPoint>]
let main argv = 
    Application.Run exercise
    0

After opening the dialog box, if the user selects a file and clicks OK or presses Enter, the file would be opened.

Opening Many Files

Most of the time, users are interested in opening one file to view or manipulate. It is also possible for a user to want to select various files and open them at the same time. When the user clicks OK after using the Open dialog box, before taking the next step, you may need to find out whether the user selected various files. To get this information, you can check the value of the OpenFileDialog.Multiselect Boolean property:

member Multiselect : bool with get, set

If the user had selected various files, this property produces a true result. If the user selected only one file, this property renders a false result. The result of this checking process allows you either to agree to open the files or to take some action of your choice.

Opening a File As Read-Only

After a file has been opened, the user may want to alter it. For example, if it is a text document, the user may want to add and/or delete some words. In some cases, you may want the user to be able to open a file but not to be able to modify it. To provide this restriction, you can set the document as read-only. In some other cases, you may want to let the user decide on this.

If you want to give this option to the user, you can start by displaying a read-only check box on the dialog box. To support this, the OpenFileDialog class is equipped with the ShowReadOnly property:

member ShowReadOnly : bool with get, set

If you set it to true, the dialog box would be equipped with an Open As Read-Only check box in its lower section.

By default, the Open As Read-Only check box is cleared when the dialog box comes up. The user has the option of keeping it that way or checking it when opening a file. The OpenFileDialog class provides the ReadOnlyChecked property to accompany the read-only option:

member ReadOnlyChecked : bool with get, set

If you want to display a check mark in the Open As Read-Only check box, you can set the ReadOnlyChecked property to true:

open System
open System.Windows.Forms

let exercise : Form = new Form()

let ofd = new OpenFileDialog()
ofd.Filter <- "HTML Files (*.htm)|*.htm|" +
              "Active Server Pages (*.asp)|*.asp|" +
              "Apache Files (*.php)|*.php|" +
              "Perl Script (*.pl)|*.pl|" +
              "All Files|"
ofd.ShowReadOnly <- true
ofd.ReadOnlyChecked <- true

ofd.ShowDialog() |> ignore

[<STAThread>]
[<EntryPoint>]
let main argv = 
    Application.Run exercise
    0

Open

On the other hand, when the user selects a file to open, you can check the value of the ReadOnlyChecked property. If it is true, this indicates that the user had clicked the Open As Read-Only check box.

 
 
   
 

Home Copyright © 2014-2015, FunctionX Home