Home

The Open 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
  

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. The easiest way to use it is to click the OpenFileDialog button OpenFileDialog from the Toolbox and click the form. You can click anywhere on the form because the OpenFileDialog object would not be seen at run time. After placing it on the form, you can use the Properties window to configure it.

If you prefer 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:

using System;
using System.Windows.Forms;

public class Exercise : Form
{
    private OpenFileDialog  ofd;

    public Exercise()
    {
	InitializeComponent();
    }

    void InitializeComponent()
    {
        ofd = new OpenFileDialog();
    }
}

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

        return 0;
    }
}
 

Home Copyright © 2007 FunctionX, Inc. Next