[.Net] C# MessageBox.Show 用法與連結說明檔

好頭痛, user 不會操作程式導致問題連連,災情嚴重.

老闆大大, 希望程式能跳出MessageBox對話盒並連接說明檔( help.htm).

開始研究MessageBox.Show語法…….

首先透過偉大google大神找到

MessageBox . Show方法(String, String, MessageBoxButtons)
開始研究一下…….

正常版顯示沒有回傳值程式碼 :

語法說明如下:

     public static DialogResult Show(string text, string caption);
        // 摘要:
        //     顯示含有指定文字和標題的訊息方塊。
        //
        // 參數:
        //   text:
        //     要顯示在訊息方塊中的文字。
        //
        //   caption:
        //     要顯示在訊息方塊標題列中的文字。

實際coding…..

essageBox.Show("歡迎來到C#世界........", "你好 老套但是不會膩顯示   

執行結果如下:
這裏寫圖片描述
有回傳值…. 語法。

        public static DialogResult Show(string text, string caption, MessageBoxButtons buttons);
        // 摘要:
        //     顯示含有指定文字、標題和按鈕的訊息方塊。
        //
        // 參數:
        //   text:
        //     要顯示在訊息方塊中的文字。
        //
        //   caption:
        //     要顯示在訊息方塊標題列中的文字。
        //
        //   buttons:
        //     其中一個 System.Windows.Forms.MessageBoxButtons 值,指定要在訊息方塊中顯示哪些按鈕。
        //
        // 傳回:
        //     其中一個 System.Windows.Forms.DialogResult 值。
        //
        // 例外狀況:
        //   T:System.ComponentModel.InvalidEnumArgumentException:
        //     指定的 buttons 參數不是 System.Windows.Forms.MessageBoxButtons 的成員。
        //
        //   T:System.InvalidOperationException:
        //     嘗試在不執行使用者互動模式的處理序中顯示 System.Windows.Forms.MessageBox。這個由 System.Windows.Forms.SystemInformation.UserInteractive
        //     屬性所指定。

實際coding….

            //寫法一
            //DialogResult Result;
            //Result = MessageBox.Show("歡迎來到C#世界........", "你好 老套但是不會膩顯示", MessageBoxButtons.OKCancel);
            //寫法二
            DialogResult Result = MessageBox.Show("歡迎來到C#世界........", "你好 老套但是不會膩顯示", MessageBoxButtons.OKCancel);

            if (Result == DialogResult.OK)
            {
                MessageBox.Show("按下OK", "有回傳值顯示");
            }
            else if (Result == DialogResult.Cancel)
            {
                MessageBox.Show("按下Cancel", "有回傳值顯示");
            }

執行結果如下
這裏寫圖片描述

  if (Result == DialogResult.OK)
            {
                MessageBox.Show("按下OK", "有回傳值顯示");
            }

這裏寫圖片描述

  else if (Result == DialogResult.Cancel)
            {
                MessageBox.Show("按下Cancel", "有回傳值顯示");
            }

這裏寫圖片描述
有help檔語法說明

         public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, string helpFilePath);
        // 摘要:
        //     使用指定的說明檔,顯示含有指定文字、標題、按鈕、圖示、預設按鈕、選項和 [說明] 按鈕的訊息方塊。
        //
        // 參數:
        //   text:
        //     要顯示在訊息方塊中的文字。
        //
        //   caption:
        //     要顯示在訊息方塊標題列中的文字。
        //
        //   buttons:
        //     其中一個 System.Windows.Forms.MessageBoxButtons 值,指定要在訊息方塊中顯示哪些按鈕。
        //
        //   icon:
        //     其中一個 System.Windows.Forms.MessageBoxIcon 值,指定那個圖示要顯示在訊息方塊中。
        //
        //   defaultButton:
        //     其中一個 System.Windows.Forms.MessageBoxDefaultButton 值,指定訊息方塊的預設按鈕。
        //
        //   options:
        //     其中一個 System.Windows.Forms.MessageBoxOptions 值,指定訊息方塊使用的顯示及關聯的選項。如果要使用預設值,可以傳遞
        //     0。
        //
        //   helpFilePath:
        //     使用者按一下 [說明] 按鈕時所顯示說明檔的路徑和名稱。
        //
        // 傳回:
        //     其中一個 System.Windows.Forms.DialogResult 值。
        //
        // 例外狀況:
        //   T:System.ComponentModel.InvalidEnumArgumentException:
        //     buttons 不是 System.Windows.Forms.MessageBoxButtons 的成員。-或-icon 不是 System.Windows.Forms.MessageBoxIcon
        //     的成員。-或-指定的 defaultButton 參數不是 System.Windows.Forms.MessageBoxDefaultButton 的成員。
        //
        //   T:System.InvalidOperationException:
        //     嘗試在不執行使用者互動模式的處理序中顯示 System.Windows.Forms.MessageBox。這個由 System.Windows.Forms.SystemInformation.UserInteractive
        //     屬性所指定。
        //
        //   T:System.ArgumentException:
        //     options 指定 System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnly 和 System.Windows.Forms.MessageBoxOptions.ServiceNotification。-或-buttons
        //     指定了無效的 System.Windows.Forms.MessageBoxButtons 組合。

helpFilePath:
//使用者按一下[說明]按鈕時所顯示說明檔的路徑和名稱。

   1. 準備要說明檔: help.htm 或help.chm

       2.指定路徑@"C:/folder/help.htm");

       3.指定路徑@"C:/folder/help.chm");

coding…

            DialogResult Result = MessageBox.Show("Delete this user ?", "Confirm Message", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, 0, @"C:/folder/help.htm");
            if (Result == DialogResult.OK)
            {
                MessageBox.Show("按下OK", "有回傳值顯示");
            }
            else if (Result == DialogResult.Cancel)
            {
                MessageBox.Show("按下Cancel", "有回傳值顯示");
            }

執行結果如下:
這裏寫圖片描述
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章