MessageBox的使用方法比较多,下面对常用的几种做了简单的说明,详细的资料可参考微软官方文档。
- Messagebox.Show(String)
显示具有指定文本的消息框。
private void button1_Click(object sender, EventArgs e){//Show(String).显示具有指定文本的消息框。MessageBox.Show("hello world");}

- Messagebox.Show(String,String)
显示具有指定文本和标题的消息框
private void button2_Click(object sender, EventArgs e){//Show(String,String)显示具有指定文本和标题的消息框。MessageBox.Show("hello world", "title");}

- Messagebox.Show(String,String,MessageBoxButtons)
显示具有指定文本、标题和按钮的消息
private void button3_Click(object sender, EventArgs e){//Show(String,String,MessageBoxButtons)显示具有指定文本、标题和按钮的消息框。MessageBox.Show("hello world", "title",MessageBoxButtons.YesNo);}
MessageBoxButtons枚举值如下
| 参数 | 数值 | 说明 |
|---|---|---|
| AbortRetryIgnore | 2 | 消息框包含“中止”、“重试”和“忽略”按钮。 |
| CancelTryContinue | 6 | 指定消息框包含 “取消”、“重试” 和 “继续” 按钮。 |
| OK | 0 | 消息框包含“确定”按钮。 |
| OKCancel | 1 | 消息框包含“确定”和“取消”按钮。 |
| RetryCancel | 5 | 消息框包含“重试”和“取消”按钮。 |
| YesNo | 4 | 消息框包含“是”和“否”按钮。 |
| YesNoCancel | 3 | 消息框包含“是”、“否”和“取消”按钮。 |

- Messagebox.Show(String, String, MessageBoxButtons, MessageBoxIcon)
显示具有指定文本、标题、按钮和图标的消息框
private void button4_Click(object sender, EventArgs e){//Show(String, String, MessageBoxButtons, MessageBoxIcon)//显示具有指定文本、标题、按钮和图标的消息框。MessageBox.Show("hello world", "Title", MessageBoxButtons.OK,MessageBoxIcon.Information);}

MessageBoxIcon枚举值如下
| 参数 | 数值 | 说明 |
|---|---|---|
| Asterisk | 64 | 消息框包含一个符号,该符号在圆圈中包含小写字母 i。 |
| Error | 16 | 消息框包含一个符号,该符号包含一个红色背景圆圈,圆圈中为白色 X 符号。 |
| Exclamation | 48 | 消息框包含一个符号,该符号包含一个黄色背景三角形,三角形中为感叹号。 |
| Hand | 16 | 消息框包含一个符号,该符号包含一个红色背景圆圈,圆圈中为白色 X 符号。 |
| Information | 64 | 消息框包含一个符号,该符号在圆圈中包含小写字母 i。 |
| None | 0 | 消息框未包含符号。 |
| Question | 32 | 消息框包含一个符号,该符号包含一个圆圈,圆圈中为问号。 不再建议使用问号消息图标,因为这种图标无法清楚地表示特定类型的消息,并且作为问题的消息表述可应用于任何消息类型。 此外,用户可能会将问号符号与帮助信息符合混淆。 因此,请不要在消息框中使用问号符号。 系统继续支持它包含的内容,只为满足反向兼容性。 |
| Stop | 16 | 消息框包含一个符号,该符号包含一个红色背景圆圈,圆圈中为白色 X 符号。 |
| Warning | 48 | 消息框包含一个符号,该符号包含一个黄色背景三角形,三角形中为感叹号。 |
5.Messagebox.Show(String,String,MessageBoxButtons,MessageBoxIcon,MessageBoxDefaultButton)
显示具有指定文本、标题、按钮和图标的消息框
private void button5_Click(object sender, EventArgs e){//Show(String, String, MessageBoxButtons, MessageBoxIcon,MessageBoxDefaultButton)//显示具有指定文本、标题、按钮、图标和默认按钮的消息框。MessageBox.Show("hello world", "Title", MessageBoxButtons.YesNo,MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);}

MessageBoxDefaultButton枚举值如下
| 参数 | 数值 | 说明 |
|---|---|---|
| Button1 | 0 | 消息框上的第一个按钮是默认按钮。 |
| Button2 | 256 | 消息框上的第二个按钮是默认按钮。 |
| Button3 | 512 | 消息框上的第三个按钮是默认按钮。 |
| Button4 | 768 | 指定消息框上的 “帮助” 按钮应为默认按钮。 |
6.Messagebox.Show(String,String,MessageBoxButtons,MessageBoxIcon,MessageBoxDefaultButton,MessageBoxOptions)
显示具有指定文本、标题、按钮、图标、默认按钮和选项的消息框。
private void button6_Click(object sender, EventArgs e){//Show(String, String, MessageBoxButtons, MessageBoxIcon,MessageBoxDefaultButton,MessageBoxOptions)//显示具有指定文本、标题、按钮、图标、默认按钮和选项的消息框。MessageBox.Show("hello world", "Title", MessageBoxButtons.YesNo,MessageBoxIcon.Information, MessageBoxDefaultButton.Button1,MessageBoxOptions.RightAlign);}

MessageBoxOptions 枚举
| 参数 | 数值 | 说明 |
|---|---|---|
| DefaultDesktopOnly | 131072 | 消息框显示在活动桌面上。 此常量类似于 ServiceNotification,但系统仅在交互式窗口工作站的默认桌面上显示消息框。 显示消息框的应用程序没有焦点,并且不使用视觉样式显示消息框。 有关详细信息,请参阅 [用视觉样式呈现控件] |
| RightAlign | 524288 | 消息框文本右对齐。 |
| RtlReading | 1048576 | 指定消息框文本按从右到左的阅读顺序显示。 |
| ServiceNotification | 2097152 | 消息框显示在活动桌面上。 调用方是通知用户某个事件的服务。 即使用户未登录到计算机,Show 也会在当前活动桌面上显示消息框。 |
7.Messagebox.Show(String,String,MessageBoxButtons,MessageBoxIcon,MessageBoxDefaultButton,MessageBoxOptions,Boolean)
显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和“帮助”按钮的消息框。
private void button7_Click(object sender, EventArgs e){//显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和“帮助”按钮的消息框。//Show(String, String, MessageBoxButtons, MessageBoxIcon,MessageBoxDefaultButton,MessageBoxOptions,,Boolean)MessageBox.Show("hello world", "Title", MessageBoxButtons.YesNo,MessageBoxIcon.Information, MessageBoxDefaultButton.Button1,MessageBoxOptions.RightAlign,true);}

displayHelpButton为Boolean型
如果显示“帮助”按钮,则为 true;否则为 false。 默认值为 false。
源程序使用VS2019编辑,使用时注意版本。
CSDN
Github















