首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

ASP.NET Windows Forms 常用對話方塊元件完整指南(OpenFileDialog、SaveFileDialog 等)

     編輯:關於ASP.NET

標準對話方塊元件

.NET Framework 提供與 Windows 作業系統一致的標準對話方塊:OpenFileDialog、SaveFileDialog、PrintDialog、ColorDialog、FontDialog 等,繼承自 FileDialog 基類。

OpenFileDialog 核心屬性與方法

  • Filter:檔案類型過濾器,格式 "文字檔|*.txt|所有檔案|*.*"
  • FilterIndex:預設選取的過濾器索引(1-based)
  • ShowDialog():顯示對話方塊,回傳 DialogResult.OK 表示使用者按下確定
  • FileName:使用者選取的完整路徑
  • OpenFile():直接回傳 Stream 讀取檔案內容

C# 完整範例:開啟文字檔並顯示內容

private void button1_Click(object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
        {
            MessageBox.Show(sr.ReadToEnd());
        }
    }
}

範例:限定選取 .cur 游標檔並套用至表單

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.Filter = "Cursor Files|*.cur";
    dlg.Title = "Select a Cursor File";
    if (dlg.ShowDialog() == DialogResult.OK && dlg.FileName != "")
    {
        this.Cursor = new Cursor(dlg.OpenFile());
    }
}

注意事項

  • FilterIndex 為 1-based,切勿誤用 0-based 索引。
  • 對話方塊本身不包含具體業務邏輯(如列印、存檔),開發者需根據回傳路徑自行實作後續操作。
  • 詳細屬性、方法、事件請查閱 MSDN 對應類別的 Members 頁面。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved