Windows Dialog對話框


一、MessageBox彈出框

MessageBox.Show(<字符串> Text, <字符串> Title, <整型> nType,MessageBoxIcon);

  • 第一個參數是 String 類型,表示提示框里面的 內容;
  • 第二個參數是String 類型,表示提示框的 標題;
  • 第三個參數是整數類型,表示消息框的類型  ,一般的都使用系統提供的幾種類型;
  • 第四個參數是提示框的 圖標,比如說警告、提示、問題等等。

MessageBoxButtons類型:

  • AbortRetryIgnore: 消息框包含“中止”、“重試”和“忽略”按鈕。
  • OK :消息框包含“確定”按鈕。(默認)
  • OKCancel : 消息框包含“確定”和“取消”按鈕。(上例所示)
  • RetryCancel  :消息框包含“重試”和“取消”按鈕。
  • YesNo :消息框包含“是”和“否”按鈕。
  • YesNoCancel :消息框包含“是”、“否”和“取消”按鈕

MessageBoxIcon圖標樣式:

  • MessageBoxIcon.Question
    MessageBoxIcon.Asterisk
    MessageBoxIcon.Information
    MessageBoxIcon.Error
    MessageBoxIcon.Stop
    MessageBoxIcon.Hand
    MessageBoxIcon.Exclamation
    MessageBoxIcon.Warning

舉例

    MessageBox.Show("用戶名或者密碼不能為空");
    MessageBox.Show("用戶名或者密碼不能為空","登錄提示");
    MessageBox.Show("用戶名或者密碼不能為空","登錄提示",MessageBoxButtons.OKCancel);
    MessageBox.Show("用戶名或者密碼不能為空","登錄提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Exclamation);

二、WinForm自帶對話框

除PrintPreviewDialog外,所有的對話框都繼承於抽象類CommonDialog。

CommonDialog的繼承結構

1、文件對話框(FileDialog) 它又常用到兩個:
  打開文件對話框(OpenFileDialog)
  保存文件對話(SaveFileDialog)
2、字體對話框(FontDialog)
3、顏色對話框(ColorDialog)
4、打印預瀏對話框(PrintPreviewDialog)
5、頁面設置(PrintDialog)
6、打印對話框(PrintDialog)

CommonDialog的方法

  • OnHelpRequest(EventArgs):    引發 HelpRequest 事件。
  • Reset():在派生類中被重寫時,將通用對話框的屬性重置為默認值。
  • ShowDialog():    用默認的所有者運行通用對話框。
  • ShowDialog(IWin32Window) :   運行具有指定所有者的通用對話框。

1、打開文件對話框(OpenFileDialog)

基本屬性

  • InitialDirectory 對話框的初始目錄
  • Filter 要在對話框中顯示的文件篩選器,例如,"文本文件(*.txt)|*.txt|所有文件(*.*)||*.*"
  • FilterIndex 在對話框中選擇的文件篩選器的索引,如果選第一項就設為1
  • RestoreDirectory 控制對話框在關閉之前是否恢復當前目錄
  • FileName 獲取或設置一個包含在文件對話框中選定的文件名的字符串。
  • Title 將顯示在對話框標題欄中的字符
  • AddExtension 是否自動添加默認擴展名
  • CheckPathExists 在對話框返回之前,檢查指定路徑是否存在
  • DefaultExt 默認擴展名
  • DereferenceLinks 在從對話框返回前是否取消引用快捷方式
  • ShowHelp 啟用"幫助"按鈕
  • ValiDateNames 控制對話框檢查文件名中是否不含有無效的字符或序列

示例

System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
dlg.Title = "打開文件";
dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Templates);
dlg.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
dlg.FilterIndex = 2;
dlg.RestoreDirectory = true;
if (dlg.ShowDialog() == DialogResult.OK)
{
    if (dlg.FileName != "") //如果dlg.Multiselect=true;可以是dlg.FileNames
    {
        MessageBox.Show("你選擇了" + dlg.FileName);
    }
}

2、保存文件對話框(SaveFileDialog)

屬性

  • Filter 要在對話框中顯示的文件篩選器,例如,"文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"
  • FilterIndex 在對話框中選擇的文件篩選器的索引,如果選第一項就設為1
  • RestoreDirectory 控制對話框在關閉之前是否恢復當前目錄
  • AddExtension 是否自動添加默認擴展名
  • CheckFileExists 獲取或設置一個值,該值指示如果用戶指定不存在的文件名,對話框是否顯示警告。
  • CheckPathExists 在對話框返回之前,檢查指定路徑是否存在
  • Container 控制在將要創建文件時,是否提示用戶。只有在ValidateNames為真值時,才適用。
  • DefaultExt 缺省擴展名
  • DereferenceLinks 在從對話框返回前是否取消引用快捷方式
  • FileName 獲取或設置一個包含在文件對話框中選定的文件名的字符串。
  • InitialDirector 對話框的初始目錄
  • OverwritePrompt 控制在將要在改寫現在文件時是否提示用戶,只有在ValidateNames為真值時,才適用
  • ShowHelp 啟用"幫助"按鈕
  • Title 將顯示在對話框標題欄中的字符
  • ValidateNames 控制對話框檢查文件名中是否不含有無效的字符或序列

示例

System.IO.Stream stream;
System.Windows.Forms.SaveFileDialog saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    if ((stream = saveFileDialog1.OpenFile()) != null)
    {
        // Code to write the stream goes here.
        stream.Close();
     }
}

3、打印預覽對話框和打印對話框

1、打印預覽對話框(PrintPreviewDialog)屬性:

  • AutoScrollMargin 獲取或設置自動滾動邊距的大小。

  • AutoScrollMinSize 獲取或設置自動滾動的最小尺寸。

  • DialogResult 獲取或設置窗體的對話框結果。

  • Document 獲取或設置要預覽的文檔。

  • HelpButton 獲取或設置一個值,該值指示是否應在窗體的標題框中顯示“幫助”按鈕。

2、打印對話框(PrintDialog)屬性:

  • AllowPrintToFile 禁止或使用"打印到文件"復選框

  • AllowSelection 禁止或使用"選定內容"單選框

  • AllowSomePages 禁止或使用"頁"單選按鈕

  • Document 從中獲取打印機設置的PrintDocument

  • PrintToFile 打印到文件"復選框是否選中
  • ShowHelp 控制是否顯示"幫助"按鈕
  • ShowNetWork 控制是否顯示"網絡"按鈕

3、示例:


private void printPreviewButton_Click(object sender, EventArgs e)
{
    StreamReader streamToPrint = new StreamReader("PrintMe.Txt");
    try
    {
        PrintDocument pd = new PrintDocument(streamToPrint); //假定為默認打印機
        if (storedPageSettings != null)
        {
            pd.DefaultPageSettings = storedPageSettings;
        }
        PrintPreviewDialog dlg = new PrintPreviewDialog();
        dlg.Document = pd;
        dlg.ShowDialog();
    }
    finally
    {
        streamToPrint.Close();
    }

}

private void printButton_Click(object sender, EventArgs e)
{

    StreamReader streamToPrint = new StreamReader("PrintMe.Txt");
    try
    {
        PrintDocument pd = new PrintDocument(streamToPrint);
        PrintDialog dlg = new PrintDialog();
        dlg.Document = pd;
        DialogResult result = dlg.ShowDialog();
        if (result == DialogResult.OK)
            pd.Print();

    }
    finally
    {
        streamToPrint.Close();
    }

}

三、自定義對話框

1 模態窗口: ShowDialog():

打開模態窗口后,只要不關閉該窗口,鼠標焦點或者光標就會一直停留在該窗口上。只有關閉該窗口后,調用窗口才能繼續。

模態窗口關閉后,仍可以讀取模態窗口中的信息,如窗口的返回狀態等,以后還可以使用ShowDialog()使其可見。

2 非模態窗口: Show():

打開非模態窗口后,仍可以操作調用窗口。后面的代碼立即執行。

關閉非模態窗口,該窗口將不復存在,會釋放窗口的所有資源,所以無法得到該窗口的任何信息。常用Hide()方法(等效於Visible=false)然后調用Show()方法使其可見。

3、對話框窗體:Form2

public Form1(string para)//獲取參數
{
    InitializeComponent();

    this.StartPosition = FormStartPosition.CenterParent;//啟動位置,父窗口中央
    this.MaximizeBox = false;
    this.MinimizeBox = false;
    this.ShowIcon = false;//不顯示圖標
    this.ControlBox = false;
    this.ShowInTaskbar = false;
    this.FormBorderStyle = FormBorderStyle.FixedDialog;//邊框樣式為固定對話框
    this.btnOK.DialogResult = DialogResult.OK;//"Enter"為OK按鈕
    this.btnCancel.DialogResult = DialogResult.Cancel;//“ESC”為Cancel按鈕
    this.textBox1.Text = para;
}

public string ReturnText //定義一個公共屬性,供調用窗口Form1使用
{
    get { return this.textBox1.Text + "b"; }
}


private void Form1_Load(object sender, EventArgs e)
{
    if (this.Owner.Name != "Form1")//Owner為調用窗體,即調用改對話框的窗體
        MessageBox.Show("非法調用");
}


private void BtnOK_Click(object sender, EventArgs e)
{
    if (this.textBox1.Text.Trim().Length == 0)
        MessageBox.Show("無輸入");
    this.textBox1.Focus();
    this.DialogResult = DialogResult.None;//阻止隱藏對話框,對話框不消失
}

4、主窗體Form1:

Form f2 = new Form2("a");

if (f2.ShowDialog(this) == DialogResult.OK)//對應Form2中的Owner,this為給對話框窗體傳值
    this.textBox1.Text = f2.ReturnText;
f2.Close();
f2.Dispose();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM