Introduction to the Save File Dialog Box

Description

Many applications allow users to open or display a document. Such applications expect the user to create a document. Once a document has been created, a user would usually want to store its contents on a medium (hard drive, USB stick, etc). Mos operating systems, including Microsoft Windows and Linux, provide a common dialog box for this purpose. In Microsoft Windows, it is the Save File dialog box:

The Save As Dialog Box

The primary role of the Save File dialog box is to allow users to store a file on a drive of a computer. To make this efficient and complete, the user must supply two valuable pieces of information: the location and the name of the file. The location of a file is also known as its path. The name of a file follows the directives of the operating system.

Two objects are particularly important on the Save File dialog box: The Save In combo box and the File Name text box or combo box. To help with this, the Save File dialog box is equipped with a Save As Type combo box. This combo box allows the user to select a file extension. The available extensions have to be created by the programmer so the user can select from a preset list. If you, the programmer, specify a series of extensions, the user can select one of them.

If the user does not specify an extension, the operating system would associate the extension of the Save As Type combo box.

After working on a Save As dialog box, the user can click Save or press Enter to save the file. Otherwise, the user can click Cancel or press Esc to give up saving the file.

Practical LearningPractical Learning: Introducing File Processing Accessories

Creating a Save As Dialog Box

To support the Save File dialog box, the .NET Framework provides a class named SaveFileDialog. To make a Save File dialog box available to your application, on the Toolbox, click the SaveFileDialog button and click the form. To programmatically provide this dialog box, declare a SaveFileDialog variable and initialize it using the class's default constructor. Here is an example:

private void btnFileProcessing_Click(object sender, EventArgs e)
{
    SaveFileDialog sfd = new SaveFileDialog();
}

The SaveFileDialog class inherits from a class named FileDialog:

public sealed class SaveFileDialog : System.Windows.Forms.FileDialog

The Save File dialog box gets most of its characteristics and behaviors from the FileDialog class.

Characteristics of the Save As Dialog Box

The Title of the Dialog Box

By default, when the Save File dialog box comes up, it displays a caption on its title bar. To support the caption of this dialog box, the FileDialog class is equipped with a property named Title:

public string Title { get; set; }

To visually specify the caption of the dialog box, type the desired string in the Title field of the Properties window of the Save file dialog box. To do this programmatically, assign the desired string to the FileSaveDialog.Title property.

The File Extension

To make sure that your application can open the appropriate types of files, you should create a list of extensions that you want the users to be able to use.

The Filter

The extensions allowed on an application form a group called a filter. The filter is like a funnel that selects the good items. To support a list of allowable extensions for a FileSaveDialog object, the FileDialog class is equipped with a property named Filter:

public string Filter { get; set; }

You can programmatically create a list of file extensions as a string. If the Save File dialog box will need only one extension, you can create the string using the following syntax:

Prompt|Extension

The Prompt is a section that defines what the user would see in the Save As Type combo box. An example would be 24-bit Bitmap. Such a string doesn't let the user know what actual extension the file would use. Therefore, as a courtesy, you can specify, between parentheses, the extension that would be applied if this extension is used.

The Prompt can be 24-bit Bitmap (*.bmp). In this case, the extension used would be bmp. The asterisk * lets the user know that whatever is provided as the file name would be used in place of the asterisk. The period indicates the separation from the file to its extension. This means that the characters on the left of the period would be the file name, the characters on the right side of the period would be used as the actual file extension.

To specify the extension that the operating system would use to associate to the file, you provide a second part of the string as Extension. An extension can use one letter such as "c" for a file of the C programming language, two characters such as cs for a file of the C# programming language, three letters such as "bmp" for a graphics file, four letters such as "html" (or html for a Web file) for some HTML files, etc. The extension depends on the programmer (or the company that is publishing the application). Here is an example of creating a filter:

24-bit Bitmap (*.bmp)|*.bmp

If you want to provide various extensions to your Save File dialog box, you can separate them with a | (beam) symbol. An example would be:

HTML Files (*.htm)|*.htm|Active Server Pages (*.asp)|*.asp|Perl Script (*.pl)|*.pl

If you added a FileSaveDialog control to a form, as mentioned earlier, you can type the filter string in the Filter field of the Properties window. If you are programmatically creating the dialog box, you can assign the string to the Filter property. Here is an example:

public class Exercise : Form
{
    private SaveFileDialog sfd;

    public Exercise()
    {
	InitializeComponent();
    }

    void InitializeComponent()
    {
        sfd = new SaveFileDialog();
		sfd.Filter = "HTML Files (*.htm)|*.htm|" +
                     "Active Server Pages (*.asp)|*.asp|" +
                     "Apache Files (*.php)|*.php|" +
                     "Perl Script (*.pl)|*.pl|" +
                     "All Files|";
    }
}

This would produce:

The Save As dialog box with various file extensions

The Filter Index

A filter organizes its extensions and categories as indexes. The above filter has the following indexes:

Index 1 = HTML Files (*.htm)
Index 2 = Active Server Pages (*.asp)
Index 3 = Apache Files (*.php)
Index 4 = Perl Script (*.pl)
Index 5 = All Files;

After creating a filter, when the dialog box comes up, the Save As Type combo box displays the first index of the filter. If you want, instead of displaying the first index by default, you can specify another index. To let you specify the desired index, the FileDialog class is equipped with a property named FilterIndex:

public int FilterIndex { get; set; }

To visually set the filter, access the Properties window of the Save File dialog box. Click the FilterInder field and type the appropriate value. To programmatically specify it, assign a value to the FileSaveDialog.FilterIndex property. Here is an example:

public class Exercise : Form
{
    private SaveFileDialog sfd;

    public Exercise()
    {
	InitializeComponent();
    }

    void InitializeComponent()
    {
       	sfd = new SaveFileDialog();
		sfd.Filter = "HTML Files (*.htm)|*.htm|" +
                     "Active Server Pages (*.asp)|*.asp|" +
                     "Apache Files (*.php)|*.php|" +
                     "Perl Script (*.pl)|*.pl|" +
                     "All Files|";

    	sfd.FilterIndex = 3;
    }
}

The Default Extension

The default extension is the extention that automatically applies when a user saves a document. To support the default index, the FileDialog is equipped with a property named DefaultExt:

public string DefaultExt { get; set; }

To visually specify the default extension for your FileSaveDialog object, type the desired extension in the DefaultExt field of the Properties window. If you had created a Filter and if you provide a default extension for a FileSaveDialog object, make sure it is one of the file extensions specified in the Filter list. To programmatically specify a default extension, assign it to the DefaultExt property of your FileSaveDialog variable. Here is an example:

public class Exercise : Form
{
    private SaveFileDialog sfd;

    public Exercise()
    {
		InitializeComponent();
    }

    void InitializeComponent()
    {
        sfd = new SaveFileDialog();
		sfd.Filter = "HTML Files (*.htm)|*.htm|" +
                     "Active Server Pages (*.asp)|*.asp|" +
                     "Apache Files (*.php)|*.php|" +
                     "Perl Script (*.pl)|*.pl|" +
                     "All Files|";
    	sfd.FilterIndex = 3;

    	sfd.DefaultExt = "htm";
    }
}

The Initial Directory

When a user decides to save a file, usually by default, the Save File dialog box chooses a folder named Documents (or My Documents) as the target repository. In some cases, you want to designage a directory of your choice. To support this change, the FileDialog class is equipped with a property named InitialDirectory:

public string InitialDirectory { get; set; }

To specify the primary folder that the Save File dialog box must select, assign a path to this property. Of course, you can do this visually or programmatically.

The File Name

Probably the most important issue users care about, as far as they are concerned, is a name for the file they are trying to save. Users know that they can set the name of the file in the File Name box. To make this action a little faster, you can provide a default name for a file in case a user does not want to specify a file name.

To support the name of the file of a Save File dialog box, the SaveDialog class is equipped with a property named FileName. To support this change, the FileSaveDialog class is equipped with a property named InitialDirectory:

public string FileName { get; set; }

To specify a default file name, access the Properties window of a Save File box. Type the desired name in the FileName field.

Displaying the Dialog Box

Once a SaveFileDialog object is ready, to let you display it to the user, the class is equipped with a method named ShowDialog. Here is an example of calling it:

private void btnFileProcessing_Click(object sender, EventArgs e)
{
    SaveFileDialog sfd = new SaveFileDialog();
    sfd.Filter = "HTML Files (*.htm)|*.htm|" +
                 "Active Server Pages (*.asp)|*.asp|" +
                 "Apache Files (*.php)|*.php|" +
                 "Perl Script (*.pl)|*.pl|" +
                 "All Files|";
    sfd.FilterIndex = 3;
    sfd.DefaultExt = "htm";

    sfd.ShowDialog();
}

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, most operating systems, including Microsoft Windows and Linux, provide a standard window. In Microsoft Windows, this is the job of 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 a class named OpenFileDialog. Like the SaveFileDialog class, the OpenFileDialog is derived from the FileDialog class:

public sealed class OpenFileDialog : System.Windows.Forms.FileDialog

To visually provide an Open File dialog box to your application, in the Toolbox, click the OpenFileDialog button and click the form. You can click anywhere on the form because the OpenFileDialog button would not appear to the user. To dynamically 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:

private void btnFileProcessing_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
}

Characteristics of an Open File Dialog Box

The File Name

Like the Save File dialog box, the Open File dialog box inherits the FileName property from the FileDialog class. If you want a default file to be specified when the dialog box comes up, you can specify this in the FileName property of the Properties window.

The Filter

Like the Save File dialog box, the Open File dialog box uses a filter to identify the types of files its application can deal with. The extensions of the files that an appliction supports are created as we saw already. Here is an example:

private void btnFileProcessing_Click(object sender, EventArgs e)
{
    OpenFileDialog 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 Save File Dialog 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. 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.

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:

private void btnFileProcessing_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new();

    ofd.Filter = "HTML Files (*.htm)|*.htm|" +
                 "Active Server Pages (*.asp)|*.asp|" +
                 "Apache Files (*.php)|*.php|" +
                 "Perl Script (*.pl)|*.pl|" +
                 "All Files|";
    
    ofd.ShowDialog();
}

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

Opening Various 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 File dialog box, before taking the next step, you may need to find out whether the user selected various files. To help you get this information, the OpenFileDialog is quipped with a Boolean property named Multiselect:

public bool Multiselect { get; set; }

If the user had selected various files, this property produces a true result. If the user selected only one file, this property produces 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. To let you control this aspect, the OpenFileDialog class is equipped with a property named ShowReadOnly:

public bool ShowReadOnly { get; set; }

If you set this property to true, the dialog box would be equipped with an Open As Read-Only check box in its lower section. Here is an example:

void InitializeComponent()
{
        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;
}

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.

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2008-2024, FunctionX Friday 10 June 2022 Next