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

定義

在對話方塊中顯示訊息、等候使用者按一下按鈕,然後傳回表示使用者按下的按鈕之整數。

public static Microsoft.VisualBasic.MsgBoxResult MsgBox (object Prompt, Microsoft.VisualBasic.MsgBoxStyle Buttons = Microsoft.VisualBasic.MsgBoxStyle.OkOnly, object? Title = default);
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.OkOnly, Optional Title As Object = Nothing) As 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

選擇性。 數值運算式,其為指定要顯示的按鈕數目和類型、要使用的圖示樣式、預設按鈕的識別以及訊息方塊是否為強制回應等值的總和。 如果您省略 Buttons,預設值會是零。

Title
Object

選擇性。 String 運算式,會顯示在對話方塊的標題列中。 如果您省略 Title,則會將應用程式名稱置於標題列中。

傳回

下列其中一個值:

常數
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 Form 應用程式中,選擇 [ 說明 ] 按鈕會 HelpRequested 執行表單的事件。

注意

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

MsgBoxStyle下表列出列舉值。

成員 描述
成員 描述
OKOnly 0 只顯示 [確定] 按鈕。
OKCancel 1 顯示 [確定] 和 [取消] 按鈕。
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 時,請只使用每個群組中的一個數位。

適用於

另請參閱