日期:2009-05-23  浏览次数:20531 次

介绍

窗体对话框组件与微软视窗操作系统中的对话框是一样的;也就是说,PrintDialog 组件是“打印”对话框,OpenFileDialog 组件是 “打开文件”对话框,依此类推。

与以往的 Microsoft Visual Basic 6.0 等 Windows 程序设计语言相似,.NET 框架提供了 Windows 用户耳熟能详的对话框。对话框的具体用途(如 Printdialog 可用于文件打印等)通常是多种多样的。故而在 .NET 框架提供的基础类中不包含用于文件打印、颜色选择等具体操作的代码,而你却可以根据应用程序的需要灵活地实现它们。因此,在 .NET 框架下,你不但可以直接应用标准对话框,而且能根据用户的选择作出不同的响应。本文提供的代码其用途就在于此。

注意,关于各种对话框的属性、方法和事件的完整描述,可以在相应类的 Members 页面中找到。比如要查看 OpenFileDialog 组件的某一方法,就可以在文档索引的“OpenFileDialog class, all members”栏目中找到相关的主题。

OpenFileDialog 组件

OpenFileDialog 对话框使得用户能够通过浏览本地或者远程的文件系统以打开所选文件。它将返回文件路径和所选的文件名。

OpenFileDialog 组件和 SaveFileDialog 组件(下文将会详细描述)包含了用于浏览和选取文件所必需的基本代码。有了它们,你就不必为这些功能编写任何代码,进而能够专心实现打开或者保存文件等具体操作。

注意,FileDialog 类的 FilterIndex 属性(由于继承的原因,为 OpenFileDialog 和 SaveFileDialog 类所共有) 使用 one-based 索引(译者注:指从 1 开始编号的索引)。 此特性将在下文的代码中用到(并且会在相应位置再次强调)。当应用程序通过类型过滤器打开文件时,或者需要保存为特定格式的文件(比如:保存为纯文本文件而不是二进制文件)时,这一点是非常重要的。人们在使用 FilterIndex 属性时却经常忘了它,因此现在务必要把它记住。

下列代码通过 Button 控件的 Click 事件调用 OpenFileDialog 组件。当用户选中某个文件,并且单击 OK 的时候,所选的文件将被打开。在本例中,文件内容将被显示在消息框内,以证实文件流被读入。

本例假设存在名为 Button1 的 Button 控件和名为 OpenFileDialog1 的 OpenFileDialog 控件。

' Visual Basic 

' NOTE: You must import the following namespace:

' Imports System.IO

' Without this import statement at the beginning

' of your code, the example will not function.

Private Sub Button1_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.Click

If OpenFileDialog1.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 will 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();

}

}

打开文件还可以使用 OpenFileDialog 组件的 OpenFile 方法,它将返回文件的每一个字节。在下面的例子中,一个 OpenFileDialog 组件将被实例化,它使用了 cursor 过滤器,以限定用户只能选取光标文件(扩展名为 .cur)。一旦某个 .cur 文件被选中,窗体的光标就被设成该文件描绘的光标形状。

本例假设存在名为 Button1 的 Button 控件。

' 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's 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(openF