[转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法
一、C#中MessageBox.Show用法
MessageBox.Show?(String)?顯示具有指定文本的消息框。?由?.NET?Compact?Framework?支持。
?
MessageBox.Show?(IWin32Window,?String)?在指定對象的前面顯示具有指定文本的消息框。
?
MessageBox.Show?(String,?String)?顯示具有指定文本和標(biāo)題的消息框。由?.NET?Compact?Framework?支持。?
?
MessageBox.Show?(IWin32Window,?String,?String)?在指定對象的前面顯示具有指定文本和標(biāo)題的消息框。??
?
MessageBox.Show?(String,?String,?MessageBoxButtons)?顯示具有指定文本、標(biāo)題和按鈕的消息框。?
?
MessageBox.Show?(IWin32Window,?String,?String,?MessageBoxButtons)?在指定對象的前面顯示具有指定文本、標(biāo)題和按鈕的消息框。?
?
MessageBox.Show?(String,?String,?MessageBoxButtons,?MessageBoxIcon)?顯示具有指定文本、標(biāo)題、按鈕和圖標(biāo)的消息框。?
? ?
MessageBox.Show?(IWin32Window,?String,?String,?MessageBoxButtons,?MessageBoxIcon)?在指定對象的前面顯示具有指定文本、標(biāo)題、按鈕和圖標(biāo)的消息框。?
? ?
MessageBox.Show?(String,?String,?MessageBoxButtons,?MessageBoxIcon,?MessageBoxDefaultButton)?顯示具有指定文本、標(biāo)題、按鈕、圖標(biāo)和默認(rèn)按鈕的消息框。?由?.NET?Compact?Framework?支持。?
? ?
MessageBox.Show?(IWin32Window,?String,?String,?MessageBoxButtons,?MessageBoxIcon,?MessageBoxDefaultButton)?在指定對象的前面顯示具有指定文本、標(biāo)題、按鈕、圖標(biāo)和默認(rèn)按鈕的消息框。?
? ?
MessageBox.Show?(String,?String,?MessageBoxButtons,?MessageBoxIcon,?MessageBoxDefaultButton,?MessageBoxOptions)?顯示具有指定文本、標(biāo)題、按鈕、圖標(biāo)、默認(rèn)按鈕和選項(xiàng)的消息框。?
? ?
MessageBox.Show?(IWin32Window,?String,?String,?MessageBoxButtons,?MessageBoxIcon,?MessageBoxDefaultButton,?MessageBoxOptions)?在指定對象的前面顯示具有指定文本、標(biāo)題、按鈕、圖標(biāo)、默認(rèn)按鈕和選項(xiàng)的消息框。?
? ?
MessageBox.Show?(String,?String,?MessageBoxButtons,?MessageBoxIcon,?MessageBoxDefaultButton,?MessageBoxOptions,?Boolean)?顯示一個(gè)具有指定文本、標(biāo)題、按鈕、圖標(biāo)、默認(rèn)按鈕、選項(xiàng)和"幫助"按鈕的消息框。?
? ?
MessageBox.Show?(String,?String,?MessageBoxButtons,?MessageBoxIcon,?MessageBoxDefaultButton,?MessageBoxOptions,?String)?使用指定的幫助文件顯示一個(gè)具有指定文本、標(biāo)題、按鈕、圖標(biāo)、默認(rèn)按鈕、選項(xiàng)和"幫助"按鈕的消息框。?
? ?
MessageBox.Show?(IWin32Window,?String,?String,?MessageBoxButtons,?MessageBoxIcon,?MessageBoxDefaultButton,?MessageBoxOptions,?String)?使用指定的幫助文件顯示一個(gè)具有指定文本、標(biāo)題、按鈕、圖標(biāo)、默認(rèn)按鈕、選項(xiàng)和"幫助"按鈕的消息框。?
? ?
MessageBox.Show?(String,?String,?MessageBoxButtons,?MessageBoxIcon,?MessageBoxDefaultButton,?MessageBoxOptions,?String,?HelpNavigator)?使用指定的幫助文件和?HelpNavigator?顯示一個(gè)具有指定文本、標(biāo)題、按鈕、圖標(biāo)、默認(rèn)按鈕、選項(xiàng)和"幫助"按鈕的消息框。?
? ?
MessageBox.Show?(String,?String,?MessageBoxButtons,?MessageBoxIcon,?MessageBoxDefaultButton,?MessageBoxOptions,?String,?String)?使用指定的幫助文件和幫助關(guān)鍵字[guan jian zi]顯示一個(gè)具有指定文本、標(biāo)題、按鈕、圖標(biāo)、默認(rèn)按鈕、選項(xiàng)和"幫助"按鈕的消息框。?
? ?
MessageBox.Show?(IWin32Window,?String,?String,?MessageBoxButtons,?MessageBoxIcon,?MessageBoxDefaultButton,?MessageBoxOptions,?String,?HelpNavigator)?使用指定的幫助文件和?HelpNavigator?顯示一個(gè)具有指定文本、標(biāo)題、按鈕、圖標(biāo)、默認(rèn)按鈕、選項(xiàng)和"幫助"按鈕的消息框。?
? ?
MessageBox.Show?(IWin32Window,?String,?String,?MessageBoxButtons,?MessageBoxIcon,?MessageBoxDefaultButton,?MessageBoxOptions,?String,?String)?使用指定的幫助文件和幫助關(guān)鍵字[guan jian zi]顯示一個(gè)具有指定文本、標(biāo)題、按鈕、圖標(biāo)、默認(rèn)按鈕、選項(xiàng)和"幫助"按鈕的消息框。?
? ?
MessageBox.Show?(String,?String,?MessageBoxButtons,?MessageBoxIcon,?MessageBoxDefaultButton,?MessageBoxOptions,?String,?HelpNavigator,?Object)?使用指定的幫助文件、HelpNavigator?和幫助主題顯示一個(gè)具有指定文本、標(biāo)題、按鈕、圖標(biāo)、默認(rèn)按鈕、選項(xiàng)和"幫助"按鈕的消息框。?
? ?
MessageBox.Show?(IWin32Window,?String,?String,?MessageBoxButtons,?MessageBoxIcon,?MessageBoxDefaultButton,?MessageBoxOptions,?String,?HelpNavigator,?Object)?使用指定的幫助文件、HelpNavigator?和幫助主題顯示一個(gè)具有指定文本、標(biāo)題、按鈕、圖標(biāo)、默認(rèn)按鈕、選項(xiàng)和"幫助"按鈕的消息框。???
? ?
用法如:?
DialogResult?ret?=MessageBox.Show(?"確定要?jiǎng)h除[shan chu]記錄么?"? ,??"刪除[shan chu]",?MessageBoxButtons.OKCancel,?MessageBoxIcon.Question);??
MessageBox.Show(?"提示[ti shi]信息",?"??信息標(biāo)題",?MessageBoxButtons.OK);?
MessageBox.Show(?"提示[ti shi]信息",?"??信息標(biāo)題",?MessageBoxButtons.OK,?MessageBoxIcon.Error);
我們在程序中經(jīng)常會(huì)用到MessageBox。? ? ?
MessageBox.Show()共有21中重載[zhong zai]方法。現(xiàn)將其常見用法總結(jié)如下: ? ? ? ?
???1.? ? ??MessageBox? .Show(?"Hello~~~~"? );? ?
??最簡單的,只顯示提示[ti shi]信息。? ?
? ?
? ?
??? ?
???2.? ?MessageBox? .Show(?"There are something wrong!"? ,?"ERROR"? );? ?
????可以給消息框加上標(biāo)[shang biao]題。? ?
? ?
? ?
??? ?
???3.? ? ??if? (?MessageBox? .Show(?"Delete this user?"? , ?"Confirm Message"? , ?MessageBoxButtons? .OKCancel) == ?DialogResult? .OK)? ?
??{? ?
????? ? ?//delete? ? ?
??}? ?
??詢問是否刪除[shan chu]時(shí)會(huì)用到這個(gè)。? ?
? ?
? ?
??? ?
???4.? ?if? (?MessageBox? .Show(?"Delete this user?"? , ?"Confirm Message"? , ?MessageBoxButtons? .OKCancel,?MessageBoxIcon? .Question) == ?DialogResult? .OK)? ?
??{? ?
?????? ? ?//delete? ? ?
??}? ?
??可以給MessageBox加上一個(gè)Icon,.net提供常見的Icon共選擇[xuan ze]。? ?
??? ?
? ?
? ?
? ?
???5.? ?if? (?MessageBox? .Show(?"Delete this user?"? , ?"Confirm Message"? , ?MessageBoxButtons? .OKCancel, ?MessageBoxIcon? .Question,?MessageBoxDefaultButton? .Button2) == ?DialogResult? .OK)? ?
??{? ?
????? ? ?//delete? ? ?
??}? ?
??可以改變MessageBox的默認(rèn)焦點(diǎn),如下:? ?
? ?
? ?
??? ?
???6.? ? ??if? (?MessageBox? .Show(?"Delete this user?"? , ?"Confirm Message"? , ?MessageBoxButtons? .OKCancel, ?MessageBoxIcon? .Question,?MessageBoxDefaultButton? .Button2,?MessageBoxOptions? .RtlReading) == ?DialogResult? .OK)? ?
??{? ?
????? ? ?//delete? ? ?
??}? ?
??反向顯示:? ?
? ?
? ?
??? ?
???7.? ?if? (?MessageBox? .Show(?"Delete this user?"? , ?"Confirm Message"? , ?MessageBoxButtons? .OKCancel, ?MessageBoxIcon? .Question, ?MessageBoxDefaultButton? .Button2, ?MessageBoxOptions? .RightAlign,?true? ) == ?DialogResult? .OK)? ?
??{? ?
?????? ? ?//delete? ? ?
??}? ?
??添加Help按鈕:? ?
? ?
? ?
??? ?
???8.? ? ??if? (?MessageBox? .Show(?"Delete this user?"? , ?"Confirm Message"? , ?MessageBoxButtons? .OKCancel, ?MessageBoxIcon? .Question, ?MessageBoxDefaultButton? .Button1, ?MessageBoxOptions? .RtlReading, ?@"/folder/file.htm"? ) == ?DialogResult? .OK)? ?
??{? ?
????? ? ?//delete? ? ?
??}? ?
??指定幫助文件的路徑[lu jing],點(diǎn)擊即可打開該路徑[lu jing]下的幫助文件。? ?
??? ?
? ?
? ?
??? ?
??9.? ??//HelpNavigator?指定常數(shù)來指示[zhi shi]要顯示的幫助文件元素。? Find ?幫助文件將打開到搜索[sou suo]頁。? ?? ? ? ? ?
???if? (?MessageBox? .Show(?"Delete this user?"? , ?"Confirm Message"? , ?MessageBoxButtons? .OKCancel, ?MessageBoxIcon? .Question, ?MessageBoxDefaultButton? .Button1, ?MessageBoxOptions? .RtlReading, ?@"/folder/file.htm"? , ?HelpNavigator? .Find) == ?DialogResult? .OK)? ?
??{? ?
?????? ? ?//delete? ? ?
??}? ?
還有一些用法,不是太實(shí)用這里就不一一介紹了,有興趣的朋友可以參考下這里:?MSDN??的MessageBox類。
二、VB.NET中MsgBox用法
MsgBox: Prompts a dialog box that displays a message.
Examples:
| MsgBox?("Thank You for the Help!") |
Information from the MSDN:?MsgBox Function
Parameters
PromptSettings
The?MsgBoxStyle?enumeration values are listed in the following table.
| OKOnly | 0 | Displays OK button only. |
| OKCancel | 1 | Displays OK and Cancel buttons. |
| AbortRetryIgnore | 2 | Displays Abort, Retry, and Ignore buttons. |
| YesNoCancel | 3 | Displays Yes, No, and Cancel buttons. |
| YesNo | 4 | Displays Yes and No buttons. |
| RetryCancel | 5 | Displays Retry and Cancel buttons. |
| Critical | 16 | Displays Critical Message icon. |
| Question | 32 | Displays Warning Query icon. |
| Exclamation | 48 | Displays Warning Message icon. |
| Information | 64 | Displays Information Message icon. |
| DefaultButton1 | 0 | First button is default. |
| DefaultButton2 | 256 | Second button is default. |
| DefaultButton3 | 512 | Third button is default. |
| ApplicationModal | 0 | Application is modal. The user must respond to the message box before continuing work in the current application. |
| SystemModal | 4096 | System is modal. All applications are suspended until the user responds to the message box. |
| MsgBoxSetForeground | 65536 | Specifies the message box window as the foreground window. |
| MsgBoxRight | 524288 | Text is right-aligned. |
| MsgBoxRtlReading | 1048576 | Specifies text should appear as right-to-left reading on Hebrew and Arabic systems. |
The first group of values (0–5) describes the number and type of buttons displayed in the dialog box; the second group (16, 32, 48, 64) describes the icon style; the third group (0, 256, 512) determines which button is the default; the fourth group (0, 4096) determines the modality of the message box, and the fifth group specifies whether or not the message box window is the foreground window, along with the alignment and direction of the text. When adding numbers to create a final value for the?Buttons?argument, use only one number from each group.
Return Values
| OK | 1 |
| Cancel | 2 |
| Abort | 3 |
| Retry | 4 |
| Ignore | 5 |
| Yes | 6 |
| No | 7 |
Exceptions/Errors
| ArgumentException | 5 | Prompt?is not a?String?expression, or?Titleis invalid. |
| InvalidOperationException | 5 | Process is not running in User Interactive mode. |
| InvalidEnumArgumentException | 5 | One or more parameters not a member ofMsgBoxResult?or?MsgBoxStyleenumerations. |
Remarks
If the dialog box displays a?Cancel?button, pressing the ESC key has the same effect as clicking?Cancel. If the dialog box contains a?Help?button, context-sensitive Help is provided for the dialog box. However, no value is returned until one of the other buttons is clicked.
Note???To specify more than the first argument, you must use the?MsgBox?function in an expression. If you omit any positional arguments, you must retain the corresponding comma delimiter.Example
This example uses the?MsgBox?function to display a critical-error message in a dialog box with Yes and No buttons. The No button is specified as the default response. This is done by combining the?MsgBox?constant values into one numeric expression. In this case, adding 4 (the Yes/No button combination) and 16 (the Critical Message window) and 256 (the second button as default button) gives a total of 276. The value returned by the?MsgBox?function depends on the button chosen by the user: Yes returns a value of 6; No returns a value of 7.
Dim msg As String Dim title As String Dim style As MsgBoxStyle Dim response As MsgBoxResult msg = "Do you want to continue?" ' Define message. style = MsgBoxStyle.DefaultButton2 Or _MsgBoxStyle.Critical Or MsgBoxStyle.YesNo title = "MsgBox Demonstration" ' Define title. ' Display message. response = MsgBox(msg, style, title) ' or MsgBox(msg, MsgBoxStyle.YesNo, title) If response = MsgBoxResult.Yes Then ' User chose Yes.' Perform some action. Else' Perform some other action. End If原文出處
鏈接①:http://blog.csdn.net/fan158/article/details/5891780
鏈接②:http://www.lob.cn/jq/kfjq/570.shtml
?
轉(zhuǎn)載于:https://www.cnblogs.com/Scl891004X/p/6257692.html
總結(jié)
以上是生活随笔為你收集整理的[转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hub,桥,交换机,路由器的区别
- 下一篇: 单调有界数列一定有极限