Order up!

A recurring question I’ve seen over the years in a number of VBScript forums is how to present a menu dialog in a script. Other than InputBox, MsgBox or the WshShell’s Popup Method, there aren’t too many GUI alternatives.  And none of them (at least at first glance) looks like it could be used to present a menu. Now, if you want to got all out with fancy formatting and buttons, then you should look to creating an HTA or use an instance of Internet Explorer.  But if your needs are simple, I can show you how to accomplish this with a simple InputBox.

Here’s a script sample of how you might accomplish this.  I’ve also attached the script as text file for your convenience. (There really is a file attached.  It’s just less than 1KB)

‘MyMenu.vbs
strTitle=”My Menu”
strErrMsg=”Invalid option! You must choose a number between 1 and 5.”
strMenu=”Please select an option:” & VbCrLf &_
        “1 Kung Pao Shrimp” & VbCrLf &_
        “2 Sweet and Sour Chicken” & VbCrLf &_
        “3 Mongolian Been” & VbCrLf &_
        “4 Pan Fried Dumplings” & VbCrLf &_
        “5 Stir Fry Surprise”
rc=InputBox(strMenu,strTitle,”0″)
If rc=”” Then WScript.quit
If IsNumeric(rc) Then
    Select Case (rc)
        Case 1
            WScript.Echo “One order of Kung Pao Shrimp!”
        Case 2
            WScript.Echo “Would you like rice with your chicken?”
        Case 3
            WScript.Echo “How spicy would you like the Mongolian Beef?”
        Case 4
            WScript.Echo “How many dumplings?”
        Case 5
            WScript.Echo “You are definitely in for a surprise.”
        Case Else
            WScript.Echo strErrMsg
            WScript.quit
    End Select
Else
    WScript.Echo strErrMsg
End If

The only real “trick” to this script is properly formatting a message for the input box. You don’t have unlimited space so you need to design something simple and to the point. If you can use numbers as menu choices as I’ve done, here it makes life a little easier. Because I’m expecting a numeric value, I can test for it and handle errors such as when someone isn’t paying attention and types in a word.  (I’ve also used some error handling in case the return value is blank in which case the user either clicked Cancel or tried to enter a blank value.)

Once I know the user entered a numeric value, I can use a Select Case statement to decide what to do based on the menu number they entered.  When you run the script, you’ll see a dialog box like this:

I gave the InputBox a default value of 0 to give the user the idea that they need to enter a number. There is error handling in the Select Case statement if the user clicks OK with a value of 0. You may prefer to set the default value to a menu choice.

So the next time you need to take an order in your script, an InputBox may be all that you need.