2010-06-17 63 views

回答

16

你可能想要做這樣的事情:

result = MsgBox ("Yes or No?", vbYesNo, "Yes No Example") 

Select Case result 
Case vbYes 
    MsgBox("You chose Yes") 
Case vbNo 
    MsgBox("You chose No") 
End Select 

要添加一個圖標:

result = MsgBox ("Yes or No?", vbYesNo + vbQuestion, "Yes No Example") 

其他圖標選項:

vbCritical or vbExclamation 
+0

我可以更改按鈕上的字是從與其他字符串? – yael 2010-06-17 14:28:13

+2

沒有,但作爲JohnFx建議,你可以改寫本聲明的消息框,使「是」和「否」的選項會工作。 – 2010-06-17 14:42:00

0

無法完成。 MsgBox按鈕只能有特定的值。
您必須爲此滾動您自己的表單。

要創建具有兩個選項(是/否)一個MsgBox:

MsgBox("Some Text", vbYesNo) 
+0

如果不是如何用VB兩個可選按鈕來創建? 耶爾 – yael 2010-06-17 14:19:45

3

VBScript Messagebox是相當有限的,以你可以應用到按鈕上的標籤,你的選擇是非常限於:

  • OK
  • 取消
  • 重試
  • 中止
  • 忽略
  • 沒有

所以,你將不得不如果你想建立自己的形式「ON」 /「OFF」

更好的是,爲什麼不能改寫的在框中提示,以便上述選項之一工作。

例如:

Do you want the light on? 
[Yes] [No] 

而偏偏做這些UI怪物之一!

Switch setting? (Click "yes" for ON and "No" for Off) 
[Yes] [No] 
0

記住 - 如果你將按鈕設置爲vbOkOnly - 它將始終返回1.

因此,您不能確定用戶是否單擊關閉或確定按鈕。你只需要添加一個vbOk選項。

0

這是可以做到的,我發現它在網絡上的其他地方......這是沒辦法我的工作! :)

Option Explicit 
' Import 
Private Declare Function GetCurrentThreadId Lib "kernel32"() As Long 

Private Declare Function SetDlgItemText Lib "user32" _ 
    Alias "SetDlgItemTextA" _ 
    (ByVal hDlg As Long, _ 
    ByVal nIDDlgItem As Long, _ 
    ByVal lpString As String) As Long 

Private Declare Function SetWindowsHookEx Lib "user32" _ 
    Alias "SetWindowsHookExA" _ 
    (ByVal idHook As Long, _ 
    ByVal lpfn As Long, _ 
    ByVal hmod As Long, _ 
    ByVal dwThreadId As Long) As Long 

Private Declare Function UnhookWindowsHookEx Lib "user32" _ 
    (ByVal hHook As Long) As Long 

' Handle to the Hook procedure 
Private hHook As Long 

' Hook type 
Private Const WH_CBT = 5 
Private Const HCBT_ACTIVATE = 5 

' Constants 
Public Const IDOK = 1 
Public Const IDCANCEL = 2 
Public Const IDABORT = 3 
Public Const IDRETRY = 4 
Public Const IDIGNORE = 5 
Public Const IDYES = 6 
Public Const IDNO = 7 

Public Sub MsgBoxSmile() 
    ' Set Hook 
    hHook = SetWindowsHookEx(WH_CBT, _ 
          AddressOf MsgBoxHookProc, _ 
          0, _ 
          GetCurrentThreadId) 

    ' Run MessageBox 
    MsgBox "Smiling Message Box", vbYesNo, "Message Box Hooking" 
End Sub 

Private Function MsgBoxHookProc(ByVal lMsg As Long, _ 
           ByVal wParam As Long, _ 
           ByVal lParam As Long) As Long 

    If lMsg = HCBT_ACTIVATE Then 
     SetDlgItemText wParam, IDYES, "Yes :-)" 
     SetDlgItemText wParam, IDNO, "No :-(" 

     ' Release the Hook 
     UnhookWindowsHookEx hHook 
    End If 

    MsgBoxHookProc = False 
End Function 
+0

您不能在VBScript中執行類似Windows API的調用。此代碼是VBA或VB6 – Jobbo 2014-12-05 13:18:38

-1

我做

msgbox "TEXT HERE",3,"TITLE HERE" 
If Yes=true then 
(result) 

else 
msgbox "Closing..." 
+0

MsgBox應作爲函數調用; Yes從哪裏來? upvoter應該感到羞恥。 – 2017-10-16 18:06:34