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这里的,在此致谢!

原文地址:https://www.cnblogs.com/yieryi/p/4599584.html