·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> ASP.NET窗体对话框的实现

ASP.NET窗体对话框的实现

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

窗体对话框组件与微软视窗操作系统中的对话框是一样的;也就是说,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(openFileDialog1.OpenFile());
}
}
}

关于读取文件流的进一步信息,请参阅FileStream.BeginRead 方法。

SaveFileDialog 组件

本对话框允许用户浏览文件系统并且选取将被写入的文件。当然,你还必须为文件写入编写具体代码。

下列代码通过 Button 控件的 Click 事件调用 SaveFileDialog 组件。当用户选中某个文件,并且单击 OK 的时候,RichTextBox 控件里的内容将被保存到所选的文件中。

本例假设存在名为 Button1 的 Button 控件,名为 RichTextBox1 的 RichTextBox 控件和名为 OpenFileDialog1 的 SaveFileDialog 控件。

' Visual Basic
' NOTE: You must import the following namespace:
' Imports System.IO
' Without this import statement at the beginning
' of your code, the code example will not function.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
RichTextBox1.SaveFile(SaveFileDialog1.FileName, _
RichTextBoxStreamType.PlainText)
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 code example will not function.
private void button1_Click(object sender, System.EventArgs e)
{
if((saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
}
}

保存文件还可以用 SaveFileDialog 组件的 OpenFile 方法,它将提供一个用于写入的 Stream 对象。

在下面的例子中,有一个包含图片的 Button 控件。 当你单击这个按钮的时候,一个 SaveFileDialog 组件将被打开,它将使用 .gif 、 .jpeg 和 .bmp 类型的文件过滤器。一旦用户通过 Save File 对话框内选中此类文件,按钮上的图片将被存入其中。

本例假设存在名为 Button2 的 Button 控件,并且它的 Image 属性被设为某个扩展名为 .gif 、 .jpeg 或者 .bmp 的图片文件。

'Visual Basic
' NOTE: You must import the following namespaces:
' Imports System.IO
' Imports System.Drawing.Imaging
' Without these import statements at the beginning of your code,
' the code example will not function.
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
' Display an SaveFileDialog so the user can save the Image
' assigned to Button2.
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"
saveFileDialog1.Title = "Save an Image File"
saveFileDialog1.ShowDialog()

' If the file name is not an empty string open it for saving.
If saveFileDialog1.FileName <> "" Then
' Save the Image via a FileStream created by the OpenFile method.
Dim fs As FileStream = CType(saveFileDialog1.OpenFile(), FileStream)
' Save the Image in the appropriate ImageFormat based upon the
' file type selected in the dialog box.
' NOTE that the FilterIndex property is one-based.
Select Case saveFileDialog1.FilterIndex
Case 1
Me.button2.Image.Save(fs, ImageFormat.Jpeg)

Case 2
Me.button2.Image.Save(fs, ImageFormat.Bmp)

Case 3
Me.button2.Image.Save(fs, ImageFormat.Gif)
End Select

fs.Close()
End If
End Sub

// C#
// NOTE: You must import the following namespaces:
// using System.IO;
// using System.Drawing.Imaging;
// Without these import statements at the beginning of your code,
// the code example will not function.
private void button2_Click(object sender, System.EventArgs e)
{
// Display an SaveFileDialog so the user can save the Image
// assigned to Button2.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
saveFileDialog1.Title = "Save an Image File";
saveFileDialog1.ShowDialog();

// If the file name is not an empty string open it for saving.
if(saveFileDialog1.FileName != "")
{
// Save the Image via a FileStream created by the OpenFile method.
FileStream fs = (FileStream)saveFileDialog1.OpenFile();
// Save the Image in the appropriate ImageFormat based upon the
// File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
switch(saveFileDialog1.FilterIndex)
{
case 1 :
this.button2.Image.Save(fs,ImageFormat.Jpeg);
break;

case 2 :
this.button2.Image.Save(fs,ImageFormat.Bmp);
break;

case 3 :
this.button2.Image.Save(fs,ImageFormat.Gif);
break;
}

fs.Close();
}
}


关于写入文件流的进一步信息,请参阅 FileStream.BeginWrite 方法。

ColorDialog 组件

此对话框显示颜色列表,并且返回所选的颜色。

与前两种对话框不同,ColorDialog 组件很容易实现其主要功能(挑选颜色)。选取的颜色将成为 Color 属性的设定值。因此,使用颜色就和设定属性值一样简单。在下面的例子中,按钮控制的 Click 事件将会开启一个 ColorDialog 组件。一旦用户选中某种颜色,并且单击了 OK ,按钮的背景将被设成所选的颜色。本例假设存在名为 Button1 的 Button 组件和名为 ColorDialog1 的 ColorDialog 组件。

' Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If ColorDialog1.ShowDialog() = DialogResult.OK Then
Button1.BackColor = ColorDialog1.Color
End If
End Sub

// C#
private void button1_Click(object sender, System.EventArgs e)
{
if(colorDialog1.ShowDialog() == DialogResult.OK)
{
button1.BackColor = colorDialog1.Color;
}
}


ColorDialog 组件具有 AllowFullOpen 属性。当其设为 False 的时候,Define Custom Colors 按钮将会失效,此时用户只能使用预定义的调色板。此外,它还有一个 SolidColorOnly 属性,当其设为 true 时,用户将不能使用抖动颜色。

FontDialog 组件

此对话框允许用户选择字体,以改变其 weight 和 size 等属性。

被选中的字体将成为 Font 属性的设定值。因此,使用字体也和设定属性值一样简单。在本例通过 Button 控件的 Click 事件调用 FileDialog 组件。当用户选中一个字体,并且单击 OK 的时候,TextBox 控件的 Font 属性将被设成所选的字体。本例假设存在名为 Button1 的 Button 控件,名为 TextBox1 的 TextBox 控件和名为 FontDialog1 的 FontDialog 组件。

' Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If FontDialog1.ShowDialog() = DialogResult.OK Then
TextBox1.Font = FontDialog1.Font
End If
End Sub

// C#
private void button1_Click(object sender, System.EventArgs e)
{
if(fontDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Font = fontDialog1.Font;
}
}

FontDialog 元件还包括 MinSize 和 MaxSize 属性,它们决定了允许用户选择的字体的最小和最大点数;还有一个 ShowColor 属性,当其设为 True 时,用户可以从对话框的下拉列表中选取字体的颜色。

PrintDocument 类

以下三个会话框,PrintDialog 组件、 PageSetupDialog 组件和 PrintPreviewDialog 控件,都与 PrintDocument 类有关。PrintDocument 类用于文档打印前的设置:设定其属性,以改变文档外观和打印方式,再将其实例输出到打印机。通常的步骤是:
(1) 生成 PrintDocument 类的一个实例;
(2) 设置 PageSetupDialog 组件的属性;
(3) 使用 PrintPreviewDialog 控件进行预览;
(4) 通过 PrintDialog 组件打印出来。

关于 PrintDocument 类的进一步资料,请参阅 PrintDocument Class 。

PrintDialog 元件

此对话框允许用户指定将要打印的文档。除此之外,它还能用于选择打印机、决定打印页,以及设置打印相关属性。通过它可以让用户文档打印更具灵活性:他们既能打印整个文档,又能打印某个片断,还能打印所选区域。

使用 PrintDialog 组件时要注意它是如何与 PrinterSettings 类进行交互的。PrinterSettings 类用于设定纸张来源、分辨率和加倍放大等打印机特征属性。每项设置都是 PrinterSettings 类的一个属性。通过 PrintDialog 类可以改变关联到文档的 PrinterSetting 类实例(由PrintDocument.PrinterSettings 指定)的特征属性值。

PrintDialog 组件将包含特征属性设置的 PrintDocument 类的实例提交到打印机。应用 PrintDialog 组件进行文档打印的范例,请参见 Creating Standard Windows Forms Print Jobs。

PageSetupDialog 组件

PageSetupDialog 组件用于显示打印布局、纸张大小和其它页面选项。如同其他对话框一样,可以通过 ShowDialog 方法调用 PageSetupDialog 组件。此外,必须生成一个 PrintDocument 类的实例,也即被打印的文档;而且必须安装了一台本地或者远程打印机,否则,PageSetupDialog 组件将无法获取打印格式以供用户选择。

使用 PageSetupDialog 组件时必须注意它是如何与 PageSettings 类进行交互的。PageSettings 类决定页面如何被打印,比如打印方向、页面大小和边距等。每项设置都是 PageSettings 类的一个属性。PageSetupDialog 类可以改变 PageSettings 类实例(由 PrintDocument.DefaultPageSettings 指定)的上述选项。

在下列代码中,Button 控件的 Click 事件处理程序开启一个 PageSetupDialog 组件;其 Document 属性被设成某个存在的文档;其 Color 属性被设成 false 。

本例假设存在名为 Button1 的 Button 控件、名为 myDocument 的 PrintDocument 控件和名为 PageSetupDialog1 的 PageSetupDialog 组件。

' Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' The print document 'myDocument' used below
' is merely for an example.
'You will have to specify your own print document.
PageSetupDialog1.Document = myDocument
' Set the print document's color setting to false,
' so that the page will not be printed in color.
PageSetupDialog1.Document.DefaultPageSettings.Color = False
PageSetupDialog1.ShowDialog()
End Sub

// C#
private void button1_Click(object sender, System.EventArgs e)
{
// The print document 'myDocument' used below
// is merely for an example.
// You will have to specify your own print document.
pageSetupDialog1.Document = myDocument;
// Set the print document's color setting to false,
// so that the page will not be printed in color.
pageSetupDialog1.Document.DefaultPageSettings.Color = false;
pageSetupDialog1.ShowDialog();
}

PrintPreviewDialog 控件

与其他对话框不同,PrintPreviewDialog 控件对整个应用程序或者其它控件没有影响,因为它仅仅在对话框里显示内容。此对话框用于显示文档,主要是打印之前的预览。

调用 PrintPreviewDialog 控件,也是使用 ShowDialog 方法。同时,必须生成 PrintDocument 类的一个实例,也即被打印的文档。

注意:当使用 PrintPreviewDialog 控件时,也必须已经安装了一台本地或者远程打印机,否则 PrintPreviewDialog 组件将无法获取被打印文档的外观。

PrintPreviewDialog 控件通过 PrinterSettings 类和 PageSettings 类进行设置,分别与 PageDialog 组件和 PageSetupDialog 组件相似。此外,PrintPreviewDialog 控件的 Document 属性所指定的被打印文档,同时作用于 PrinterSettings 类和 PageSettings 类,其内容被显示在预览窗口中。

在下列代码中,通过 Button 控件的 Click 事件调用 PrintPreviewDialog 控件。被打印文档在 Document 属性中指定。注意:代码中没有指定被打印文档。

本例假设存在名为 Button1 的 Button 控件,名为 myDocument 的 PrintDocument 组件和名为 PrintPreviewDialog1 的 PrintPreviewDialog 控件。


' Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' The print document 'myDocument' used below
' is merely for an example.
' You will have to specify your own print document.
PrintPreviewDialog1.Document = myDocument
PrintPreviewDialog1.ShowDialog()
End Sub

// C#
private void button1_Click(object sender, System.EventArgs e)
{
// The print document 'myDocument' used below
// is merely for an example.
// You will have to specify your own print document.
printPreviewDialog1.Document = myDocument;
printPreviewDialog1.ShowDialog()
}

小结
.NET 框架里包含了 Windows 用户所熟悉的各种公共对话框,于是在应用程序中提供交互功能变得更加容易。通常,对话框的用途是多种多样的;.NET 框架对此提供了开放支持,你可以选择最佳方案以适合应用程序的需要。我们介绍了与对话框组件有关的一些简单应用。你可以直接使用这些代码,也可以对其稍加修改用于你的应用程序。