Home

Characteristics of the Save As Dialog Box

 

The File Extension

To make sure that your application can open the allowed types of files, depending on your goals, you should create a list of extensions that you want the users to be able to open. The allowed extensions form a group called a filter. The filter is like a funnel that selects the good items. For a text-based application, you may allow only text files, that is, files with a txt extension. For a rich text-based application, you may allow only Rich Text Format files, which are files with rtf extension. On the other hand, if you are creating an application for web files, you can allow as many file extensions as necessary, such as htm, html, php, asp, etc.

As you may realize, text files or web files are all text-based files. This means that if you create a text-based or rich-text based application, you should allow the users to decide whether the file they are trying to open can be "read" by a text-based control. To provide this ability, you can specify an unknown extension specified as All Files.

The Filter

To create a list of allowable extensions for your FileSaveDialog object, you can use the Filter property from the Properties window. At run time, you can create a list of file extensions as a string. If the Save As 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 does not 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. In Microsoft Windows, most extensions are made of three characters. Some applications use a 2-letter extensions (for example Perl files have a pl extension) and some others use 4 letters (such as html for some HTML files). This depends on the programmer (or the company that is publishing the application). An example of a string as an extension is:

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

If you want to provide various extensions to your Save As dialog box, you can separate them with a | 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 your 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 specify the desired index at design time, change the value of the FilterIndex field in the Properties window. 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;
        }
}

Once you know the types of files that your application will be dealing with, you can make your dialog box friendly by displaying the most likely extension for a document created using your application. For example, if you create a text-based application, users are more likely to create a text file with it. If you create a rich text-based application, users are more likely to create a Rich Text Format file with it. This most likely extension is known as the default extension, it allows the user not to provide an extension in the most likely cases when saving a file. By simply providing a file name and clicking Save, the operating system would associate the file with the default extension. Of course, if you create a filter, the user can specify a desired allowed extension.

The Default Extension

To 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";
    }
}

Displaying the Dialog Box

Once the OpenFileDialog object is ready, to display it to the user, call its ShowDialog() method. Here is an example:

using System;
using System.Windows.Forms;

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";

        sfd.ShowDialog();
    }
}

public class Program
{
    public static int Main()
    {
        Application.Run(new Exercise());

        return 0;
    }
}

Microsoft Windows operating systems, especially since Windows 9X, are configured to have a default folder in which users are most likely to save their files. On Windows 9X, it is C:\My Documents. On Windows NT, it is usually specified by the network administrator. On Windows 2000 and Windows XP, it uses a more customized scenario. These settings are known to the operating system and you will usually not be concerned with them. In some circumstances, if you want to specify the folder in which the users should save their files by default, you can provide it using the InitialDirectory property. This directory usually ends with \My Documents. If you want to find the path to the My Documents for a user, you can call the Environment.GetFolderPath() method and pass it the Personal member of the Environment.SpecialFolder enumerator. The syntax of the GetFolderPath() method is:

public static string GetFolderPath(SpecialFolder folder);

Here is an example:

private System.Void button1_Click(System.Object    sender, System.EventArgs    e)
{
    string  strMyDocuments =
	 Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    this.textBox1.Text = strMyDocuments;
}

Once again, most of the time, you will not be concerned with this issue if you are creating an application for any user.

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. This is done by typing a name in the FileName field of the Properties window. In practicality, the FileName value is the string that displays in the File Name box of the Save As dialog box.

By default, when the Save As dialog box comes up, it displays "Save As" on its title bar. If you want a different caption, set the desired string in the Title field of the Properties window or assign a string to the FileSaveDialog.Title property.

 

Previous Copyright © 2007 FunctionX, Inc. Home