How can I show a message box with two buttons (For example: "on"
, "off"
)?
It is a modal window, blocking other actions in the application until the user closes it. A MessageBox can contain text, buttons, and symbols that inform and instruct the user.
MessageBox is used to provide confirmations of a task being done or to provide warnings before a task is done. Create a Windows Forms app in Visual Studio and add a button on it. Something like this below. Let's say, you want to show a message on a button click event handler.
You probably want to do something like this:
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
To add an icon:
result = MsgBox ("Yes or No?", vbYesNo + vbQuestion, "Yes No Example")
Other icon options:
vbCritical or vbExclamation
The VBScript Messagebox is fairly limited as to the labels you can apply to the buttons, your choices are pretty much limited to:
So you are going to have to build your own form if you want "ON"/"OFF"
Better yet, why not rephrase the prompt in the box so one of the above options works.
For example:
Do you want the light on?
[Yes] [No]
And for God's sake don't do one of these UI monstrosities!
Switch setting? (Click "yes" for ON and "No" for Off)
[Yes] [No]
Remember - if you set the buttons to vbOkOnly
- it will always return 1.
So you can't decide if a user clicked on the close or the OK button. You just have to add a vbOk
option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With