Interaction.MsgBox(Object, MsgBoxStyle, Object) 方法

定義

在對話框中顯示訊息,等待使用者點擊按鈕,然後回傳一個整數表示使用者點擊的按鈕。

public static Microsoft.VisualBasic.MsgBoxResult MsgBox(object Prompt, Microsoft.VisualBasic.MsgBoxStyle Buttons = Microsoft.VisualBasic.MsgBoxStyle.ApplicationModal, object Title = default);
static member MsgBox : obj * Microsoft.VisualBasic.MsgBoxStyle * obj -> Microsoft.VisualBasic.MsgBoxResult
Public Function MsgBox (Prompt As Object, Optional Buttons As MsgBoxStyle = Microsoft.VisualBasic.MsgBoxStyle.ApplicationModal, Optional Title As Object = Nothing) As MsgBoxResult

參數

Prompt
Object

必須的。 String 表達式會顯示在對話框中的訊息。 最大 Prompt 長度約為 1024 個字元,視所用字元的寬度而定。 如果 Prompt 包含多行,你可以用換行字元(Chr(13))、換行字元(Chr(10))或換行/換行字元組合(Chr(13)Chr(10))來分隔行。

Buttons
MsgBoxStyle

Optional. 數值表達式,是指定顯示按鈕數量與類型、圖示樣式、預設按鈕身份及訊息框模式的數值總和。 若省略 Buttons,預設值為零。

Title
Object

Optional. String 表達式顯示在對話框的標題欄中。 如果你省略 Title了,申請名稱會放在標題欄。

傳回

下列其中一個值:

常數Value
OK1
Cancel2
Abort3
Retry4
Ignore5
Yes6
No7

例外狀況

Prompt 不是 String 表達式,或 Title 是無效。

程序未在使用者互動模式下執行。

一個或多個參數,不是列舉的成員MsgBoxResultMsgBoxStyle

範例

此範例使用 MsgBox 了在對話框中顯示關鍵錯誤訊息的功能,並配有「是」與「否」按鈕。 預設的回應是「不」按鈕。 這是透過將常數值合併 MsgBox 成一個數值表達式來完成的。 此時,將 4(是/否鍵組合)、16( 關鍵訊息 視窗)和 256(第二個預設按鈕)加起來,總共是 276。 函式回傳 MsgBox 的值取決於使用者選擇的按鈕:是回傳 6;無退貨,值為7。

' The following example requires that Option Infer be set to On.

' Define the message you want to see inside the message box.
Dim msg = "Do you want to continue?"

' Display a simple message box.
MsgBox(msg)

' Define a title for the message box.
Dim title = "MsgBox Demonstration"

' Add the title to the display.
MsgBox(msg, , title)

' Now define a style for the message box. In this example, the
' message box will have Yes and No buttons, the default will be
' the No button, and a Critical Message icon will be present.
Dim style = MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton2 Or
            MsgBoxStyle.Critical

' Display the message box and save the response, Yes or No.
Dim response = MsgBox(msg, style, title)

' Take some action based on the response.
If response = MsgBoxResult.Yes Then
    MsgBox("YES, continue!!", , title)
Else
    MsgBox("NO, stop!!", , title)
End If

備註

若省略任何位置論元,必須保留相應的逗號分隔符。

如果對話框顯示取消按鈕,按下 ESC 鍵的效果與點擊取消相同

若對話框包含 說明 按鈕,則會針對該對話框提供情境相關的說明。 然而,在選擇其他按鈕之前,不會返回任何數值。 在Windows表單應用程式中,選擇 Help按鈕即可執行表單的 HelpRequested 事件。

備註

MsgBox函式在層級需要 SafeTopLevelWindowsUIPermission ,這可能會影響其在部分信任情況下的執行。 如需詳細資訊,請參閱UIPermission

MsgBoxStyle列舉值列於下表。

會員 Value Description
會員 Value Description
OKOnly 0 僅顯示OK按鈕。
OKCancel 1 顯示OK和取消按鈕。
AbortRetryIgnore 2 顯示中止、重試和忽略按鈕。
YesNoCancel 3 顯示是、否和取消按鈕。
YesNo 4 顯示是與否按鈕。
RetryCancel 5 顯示重試與取消按鈕。
Critical 16 顯示關鍵訊息圖示。
Question 32 顯示警告查詢圖示。
Exclamation 48 顯示警告訊息圖示。
Information 64 顯示資訊訊息圖示。
DefaultButton1 0 第一個按鈕是預設的。
DefaultButton2 256 第二個按鈕是預設的。
DefaultButton3 512 第三個按鈕是預設的。
ApplicationModal 0 應用方式為模態。 使用者必須先回應訊息框,才能繼續在當前應用程式中工作。
SystemModal 4096 系統是模態的。 所有應用程式都會暫停,直到用戶回應消息框為止。
MsgBoxSetForeground 65536 指定訊息方塊視窗做為前景視窗。
MsgBoxRight 524288 文字是靠右對齊。
MsgBoxRtlReading 1048576 指定文字在希伯來語和阿拉伯語系統上應顯示為從右到左閱讀。

第一組數值(0-5)描述對話框中顯示的按鈕數量與類型。 第二組(16、32、48、64)描述了聖像風格。 第三組(0, 256, 512)決定哪個按鈕是預設按鈕。 第四組(0, 4096)決定訊息框的模態,第五組則指定訊息框視窗是否為前景視窗,以及文字的對齊與方向。 在加總數字以產生最終參數值時 Buttons ,請只從每個群組中使用一個數字。

適用於

另請參閱