與以往的 Microsoft Visual Basic 6.0 等 Windows 程序設計語言相似,.NET 框架提供了 Windows 用戶耳熟能詳的對話框。對話框的具體用途(如 Printdialog 可用于文件打印等)通常是多種多樣的。故而在 .NET 框架提供的基礎類中不包含用于文件打印、顏色選擇等具體操作的代碼,而你卻可以根據應用程序的需要靈活地實現它們。因此,在 .NET 框架下,你不但可以直接應用標準對話框,而且能根據用戶的選擇作出不同的響應。本文提供的代碼其用途就在于此。
注意,關于各種對話框的屬性、方法和事件的完整描述,可以在相應類的 Members 頁面中找到。比如要查看 OpenFileDialog 組件的某一方法,就可以在文檔索引的“OpenFileDialog class, all members”欄目中找到相關的主題。
1.OpenFileDialog 組件
OpenFileDialog 對話框使得用戶能夠通過瀏覽本地或者遠程的文件系統以打開所選文件。它將返回文件路徑和所選的文件名。
注意,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();
}
}
本例假設存在名為 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 方法。
2.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)
{
原文轉自:http://www.anti-gravitydesign.com