MessageBox詳解


MessageBox.Show();可謂是winform開發中用的次數最多的東東啦。先貼一張msdn的圖解

msdn好像沒有更新哎,只提供了這幾種方法,並且參數名稱和最新的有差別,但實際上messagebox.show()有21種重載方法,用的時候再細細查看吧。基本上都有返回結果,返回結果的如下表,一般在if判斷中使用,比如DialogResult.OK這樣的。

下面簡單舉幾個例子。

1個參數。
1.     1個參數。
                  MessageBox.Show(string text);
//     顯示具有指定文本的消息框。
//
// 參數:
//   text:
//     要在消息框中顯示的文本。
//
// 返回結果:
//     System.Windows.Forms.DialogResult 值之一。
2個參數。
2.     2個參數。
               MessageBox.Show(string text, string caption);
//     顯示具有指定文本和標題的消息框。
//
// 參數:
//   text:
//     要在消息框中顯示的文本。
//
//   caption:
//     要在消息框的標題欄中顯示的文本。
//
// 返回結果:
//     System.Windows.Forms.DialogResult 值之一。
3個參數
3.     3個參數。
             MessageBox.Show(string text, string caption, MessageBoxButtons buttons);
//     顯示具有指定文本、標題和按鈕的消息框。
//
// 參數:
//   text:
//     要在消息框中顯示的文本。
//
//   caption:
//     要在消息框的標題欄中顯示的文本。
//
//   buttons:
//     System.Windows.Forms.MessageBoxButtons 值之一,可指定在消息框中顯示哪些按鈕。
//
// 返回結果:
//     System.Windows.Forms.DialogResult 值之一。
//
// 異常:
//   System.ComponentModel.InvalidEnumArgumentException:
//     指定的 buttons 參數不是 System.Windows.Forms.MessageBoxButtons 的成員。
//
//   System.InvalidOperationException:
//     試圖在運行模式不是用戶交互模式的進程中顯示 System.Windows.Forms.MessageBox。這是由 System.Windows.Forms.SystemInformation.UserInteractive
//     屬性指定的。
4個參數
4.     4個參數。
                    MessageBox.Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon);
//     顯示具有指定文本、標題、按鈕和圖標的消息框。
//
// 參數:
//   text:
//     要在消息框中顯示的文本。
//
//   caption:
//     要在消息框的標題欄中顯示的文本。
//
//   buttons:
//     System.Windows.Forms.MessageBoxButtons 值之一,可指定在消息框中顯示哪些按鈕。
//
//   icon:
//     System.Windows.Forms.MessageBoxIcon 值之一,它指定在消息框中顯示哪個圖標。
//
// 返回結果:
//     System.Windows.Forms.DialogResult 值之一。
//
// 異常:
//   System.ComponentModel.InvalidEnumArgumentException:
//     指定的 buttons 參數不是 System.Windows.Forms.MessageBoxButtons 的成員。- 或 - 指定的 icon
//     參數不是 System.Windows.Forms.MessageBoxIcon 的成員。
//
//   System.InvalidOperationException:
//     試圖在運行模式不是用戶交互模式的進程中顯示 System.Windows.Forms.MessageBox。這是由 System.Windows.Forms.SystemInformation.UserInteractive
//     屬性指定的。
5個參數
      
5.     5個參數。
                  MessageBox.Show(string text, string caption, MessageBoxButtons buttons,
                                          MessageBoxIcon icon, MessageBoxDefaultButton defaultButton);
//     顯示具有指定文本、標題、按鈕、圖標和默認按鈕的消息框。
//
// 參數:
//   text:
//     要在消息框中顯示的文本。
//
//   caption:
//     要在消息框的標題欄中顯示的文本。
//
//   buttons:
//     System.Windows.Forms.MessageBoxButtons 值之一,可指定在消息框中顯示哪些按鈕。
//
//   icon:
//     System.Windows.Forms.MessageBoxIcon 值之一,它指定在消息框中顯示哪個圖標。
//
//   default Button:
//     System.Windows.Forms.MessageBoxDefaultButton 值之一,可指定消息框中的默認按鈕。
//
// 返回結果:
//     System.Windows.Forms.DialogResult 值之一。
//
// 異常:
//   System.ComponentModel.InvalidEnumArgumentException:
//     buttons 不是 System.Windows.Forms.MessageBoxButtons 的成員。- 或 - icon 不是 System.Windows.Forms.MessageBoxIcon
//     的成員。- 或 - defaultButton 不是 System.Windows.Forms.MessageBoxDefaultButton 的成員。
//
//   System.InvalidOperationException:
//     試圖在運行模式不是用戶交互模式的進程中顯示 System.Windows.Forms.MessageBox。這是由 System.Windows.Forms.SystemInformation.UserInteractive
//     屬性指定的。
6個參數
6.     6個參數。
                 MessageBox.Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
                                                       MessageBoxDefaultButton defaultButton, MessageBoxOptions options);
//     顯示具有指定文本、標題、按鈕、圖標、默認按鈕和選項的消息框。
//
// 參數:
//   text:
//     要在消息框中顯示的文本。
//
//   caption:
//     要在消息框的標題欄中顯示的文本。
//
//   buttons:
//     System.Windows.Forms.MessageBoxButtons 值之一,可指定在消息框中顯示哪些按鈕。
//
//   icon:
//     System.Windows.Forms.MessageBoxIcon 值之一,它指定在消息框中顯示哪個圖標。
//
//   defaultButton:
//     System.Windows.Forms.MessageBoxDefaultButton 值之一,可指定消息框中的默認按鈕。
//
//   options:  //
//     System.Windows.Forms.MessageBoxOptions 值之一,可指定將對消息框使用哪些顯示和關聯選項。若要使用默認值,請傳入
//     0。
//
// 返回結果:
//     System.Windows.Forms.DialogResult 值之一。
//
// 異常:
//   System.ComponentModel.InvalidEnumArgumentException:
//     buttons 不是 System.Windows.Forms.MessageBoxButtons 的成員。- 或 - icon 不是 System.Windows.Forms.MessageBoxIcon
//     的成員。- 或 - 指定的 defaultButton 不是 System.Windows.Forms.MessageBoxDefaultButton
//     的成員。
//
//   System.InvalidOperationException:
//     試圖在運行模式不是用戶交互模式的進程中顯示 System.Windows.Forms.MessageBox。這是由 System.Windows.Forms.SystemInformation.UserInteractive
//     屬性指定的。
//
//   System.ArgumentException:
//     options 同時指定了 System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnly 和
//     System.Windows.Forms.MessageBoxOptions.ServiceNotification。- 或 - buttons
//     指定了無效的 System.Windows.Forms.MessageBoxButtons 組合。
7個參數 
 
7.     7個參數。
                MessageBox.Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
                                        MessageBoxDefaultButton defaultButton, MessageBoxOptions options, bool displayHelpButton);
//     顯示一個具有指定文本、標題、按鈕、圖標、默認按鈕、選項和“幫助”按鈕的消息框。
//
// 參數:
//   text:
//     要在消息框中顯示的文本。
//
//   caption:
//     要在消息框的標題欄中顯示的文本。
//
//   buttons:
//     System.Windows.Forms.MessageBoxButtons 值之一,可指定在消息框中顯示哪些按鈕。
//
//   icon:
//     System.Windows.Forms.MessageBoxIcon 值之一,它指定在消息框中顯示哪個圖標。
//
//   defaultButton:
//     System.Windows.Forms.MessageBoxDefaultButton 值之一,可指定消息框中的默認按鈕。
//
//   options:
//     System.Windows.Forms.MessageBoxOptions 值之一,可指定將對消息框使用哪些顯示和關聯選項。若要使用默認值,請傳入
//     0。
//
//   helpButton:
//     如果顯示“幫助”按鈕,則為 true;否則為 false。默認為 false。
//
// 返回結果:
//     System.Windows.Forms.DialogResult 值之一。
//
// 異常:
//   System.ComponentModel.InvalidEnumArgumentException:
//     buttons 不是 System.Windows.Forms.MessageBoxButtons 的成員。- 或 - icon 不是 System.Windows.Forms.MessageBoxIcon
//     的成員。- 或 - 指定的 defaultButton 不是 System.Windows.Forms.MessageBoxDefaultButton
//     的成員。
//
//   System.InvalidOperationException:
//     試圖在運行模式不是用戶交互模式的進程中顯示 System.Windows.Forms.MessageBox。這是由 System.Windows.Forms.SystemInformation.UserInteractive
//     屬性指定的。
//
//   System.ArgumentException:
//     options 同時指定了 System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnly 和
//     System.Windows.Forms.MessageBoxOptions.ServiceNotification。- 或 - buttons
//     指定了無效的 System.Windows.Forms.MessageBoxButtons 組合。
也是 7 個參數
8.  (也是 7 個參數)
                        MessageBox.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 值之一。
//
// 異常:
//   System.ComponentModel.InvalidEnumArgumentException:
//     buttons 不是 System.Windows.Forms.MessageBoxButtons 的成員。- 或 - icon 不是 System.Windows.Forms.MessageBoxIcon
//     的成員。- 或 - 指定的 defaultButton 不是 System.Windows.Forms.MessageBoxDefaultButton
//     的成員。
//
//   System.InvalidOperationException:
//     試圖在運行模式不是用戶交互模式的進程中顯示 System.Windows.Forms.MessageBox。這是由 System.Windows.Forms.SystemInformation.UserInteractive
//     屬性指定的。
//
//   System.ArgumentException:
//     options 同時指定了 System.Windows.Forms.MessageBoxOptions.DefaultDesktopOnly 和
//     System.Windows.Forms.MessageBoxOptions.ServiceNotification。- 或 - buttons
//     指定了無效的 System.Windows.Forms.MessageBoxButtons 組合。

代碼是借鑒http://www.cnblogs.com/music-liang/archive/2011/09/14/2176021.html這里的,在此致謝!

 


免責聲明!

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



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