ASP.NET表單對話方塊的實現

來源:互聯網
上載者:User
asp.net|對話方塊


表單對話方塊組件與微軟視窗作業系統中的對話方塊是一樣的;也就是說,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 架構對此提供了開放支援,你可以選擇最佳方案以適合應用程式的需要。我們介紹了與對話方塊組件有關的一些簡單應用。你可以直接使用這些代碼,也可以對其稍加修改用於你的應用程式。




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.