Introduced
The form dialog component is the same as the dialog box in the Microsoft Windows operating system; That is, the PrintDialog component is the Print dialog box, the OpenFileDialog component is the Open File dialog box, and so on.
Similar to previous Windows programming languages such as Microsoft Visual Basic 6.0, the. NET Framework provides a familiar dialog box for Windows users. The specific purpose of a dialog box, such as Printdialog can be used for file printing, is usually varied. Therefore, the underlying classes provided in the. NET framework do not contain code for specific operations such as file printing, color selection, and you can implement them flexibly, depending on the needs of your application. Therefore, in the. NET framework, you can not only apply the standard dialog box directly, but also respond differently depending on the user's choice. The code provided in this article is intended for this purpose.
Note that the complete description of the properties, methods, and events for each dialog box can be found in the members page of the corresponding class. For example, to see a method of the OpenFileDialog component, you can find related topics in the "OpenFileDialog class, all Members" section of the document index.
OpenFileDialog Components
The OpenFileDialog dialog box allows the user to open the selected file by browsing the local or remote file system. It returns the file path and the selected file name.
The OpenFileDialog component and the SaveFileDialog component (described in detail below) contain the basic code necessary to browse and select files. With them, you don't have to write any code for these functions, and then you can focus on the specific operations of opening or saving files.
Note that the FilterIndex property of the FileDialog class (which is common to OpenFileDialog and SaveFileDialog classes for inheritance) uses the one-based index (the index that is numbered from 1). This attribute is used in the code below (and is highlighted again in the appropriate location). This is important when an application opens a file through a type filter, or when it needs to be saved as a file in a particular format (for example, when saving as a plain text file instead of a binary file). People often forget it when they use the FilterIndex attribute, so be sure to remember it now.
The following code invokes the OpenFileDialog component through the Click event of the Button control. When the user selects a file and clicks OK, the selected file is opened. In this case, the contents of the file will be displayed in a message box to confirm that the file stream is read.
This example assumes the presence of a Button control named Button1 and a OpenFileDialog control named OpenFileDialog1.
' Visual Basic
' note:you must import the following namespace:
' Imports System.IO
' without this impor T statement at the beginning
' of your code, the example would not function.
Private Sub button1_click (ByVal sender as System.Object, _
ByVal e as System.EventArgs) Handles Button1.Click
If Ope Nfiledialog1.showdialog () = DialogResult.OK Then
Dim sr as New StreamReader (openfiledialog1.filename)
MessageBox.Show (Sr. READTOEND)
Sr. Close ()
End If
End Sub
//C #
//Note:you must import the following namespace:
//using System.IO;
Without this import statement at the beginning
//of your code, the example would not function.
private void Button1_Click (object sender, System.EventArgs e)
{
if (openfiledialog1.showdialog () = = DialogResult.OK)
{
StreamReader sr = new StreamReader (openfiledialog1.filename);
MessageBox.Show (Sr. ReadToEnd ());
Sr. Close ();
}
}
You can also open a file by using the OpenFile method of the OpenFileDialog component, which returns each byte of the file. In the following example, a OpenFileDialog component is instantiated and uses a cursor filter to qualify the user to select only the cursor file (extension. cur). Once a. cur file is selected, the form's cursor is set to the cursor shape that the file depicts.
This example assumes the presence of a Button control named Button1.
' Visual Basic
Private Sub button1_click (ByVal sender as System.Object, _
ByVal e as System.EventArgs) Handles Button1.Click
' Display an OpenFileDialog so the user can select a Cursor.
Dim OpenFileDialog1 as New OpenFileDialog ()
Openfiledialog1.filter = "Cursor files|*.cur"
Openfiledialog1.title = "Select a Cursor File"
' Show the Dialog.
' If the user clicked OK in the dialog and
A. CUR file was selected, open it.
If openfiledialog1.showdialog () = DialogResult.OK Then
If openfiledialog1.filename <> "" Then
' Assign the cursor in the Stream to the Form ' cursor property.
Me.cursor = New Cursor (openfiledialog1.openfile ())
End If
End If
End Sub
C#
private void Button1_Click (object sender, System.EventArgs e)
{
Display an OpenFileDialog so the user can select a Cursor.
OpenFileDialog openFileDialog1 = new OpenFileDialog ();
Openfiledialog1.filter = "Cursor files|*.cur";
Openfiledialog1.title = "Select a Cursor File";
Show the Dialog.
If the user clicked OK in the dialog and
A. CUR file was selected, open it.
if (openfiledialog1.showdialog () = = DialogResult.OK)
{
if (Openfiledialog1.filename!= "")
{
Assign the cursor in the Stream to the Form's cursor property.
This. Cursor = new Cursor (Openfiledialog1.openfile ());
}
}
}
For further information on reading file streams, see the Filestream.beginread method.