程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> ASF.net窗體對話框的實現

ASF.net窗體對話框的實現

編輯:關於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 方法。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved