How to Get a Response From a Messagebox

How to Get a Response From a Messagebox thumbnail
Get feedback from your users by using messagebox controls.

Programming languages such as JavaScript, C# and VBScript provide a way for computer users to communicate with the application. For example, messageboxes, are pop-up windows that display a set of buttons such as "Yes," "No" and "Cancel." When a user clicks one of the buttons, the program can detect the clicked button and take the appropriate actions. Some developers create their own messagebox controls. You can save time by using the functionality built into your programming language to get responses from users.

Instructions

  1. Use JavaScript

    • 1

      Open one of your HTML files using Notepad or an HTML editor.

    • 2

      Add the following JavaScript function to the "<head>" section of the document:

      <script type="text/javascript">

      // Lines 1-2

      var checkWindow = window.confirm("Click OK or Cancel");

      var userResponse;

      // Lines 3-4

      if (checkWindow)

      userResponse = "OK";

      // Lines 5-6

      else

      userResponse = "Cancel";

      alert("You clicked " + userResponse);

      </script>

      Line one opens a messagebox and displays two buttons: "OK" and "Cancel." The variable "checkWindow" holds the result of the user's choice. Lines 3-6 set the "userResponse" variable to "OK" if the user clicks "OK" and "Cancel" if the user clicks "Cancel."

    • 3

      Save the file and open it in your browser. The messagebox will open and display the buttons. Click "OK" or "Cancel" and an alert message will display the button that you clicked.

    Use C#

    • 4

      Open Visual Studio. Click "File" and select "New Project."

    • 5

      Double-click "Windows Forms Application." Visual Studio will create a new Windows Forms application and display a blank form in the design panel.

    • 6

      Locate the "Properties" panel on the right side of the screen and click the "Events" button. The "Events" button has a lightning bolt icon.

    • 7

      Double-click "Load." A code window will open and display the code for the form's load event. The code looks like this:

      private void Form1_Load(object sender, EventArgs e)

      {

      // ADD CODE BELOW

      }

    • 8

      Add the following code below the line, "ADD CODE BELOW."

      // Line 1

      DialogResult userResponse = MessageBox.Show("Do you wish to continue?", "Question", MessageBoxButtons.YesNo);

      MessageBox.Show("You clicked " + userResponse);

      Line one opens a messagebox and displays two buttons: "Yes" and "No." The variable "userResponse" holds the result of the user's choice.

    • 9

      Press "F5." The application will run and display the messagebox. Click one of the buttons. An alert box will display your choice.

    Use VBScrpt

    • 10

      Open Notepad.

    • 11

      Add the following code to the document:

      Set WshShell = CreateObject("WScript.Shell")

      messageString = "Click OK or Cancel"

      title = "Window Title"

      secondsToWait = 0

      clicked = WshShell.Popup (messageString, secondsToWait, title, 1)

      The last line opens a messagebox and displays an "OK" button and a "Cancel" button. The variable "messageString" holds the message that you want to display. "Title" holds the title of the messagebox. The variable "secondsToWait" determines the number of seconds the messagebox will appear before closing automatically. Set it to zero to make the messagebox remain open until the user closes it. The number one in the last line defines the set of buttons to display. In VBScript, the number one means, "Display the OK and Cancel" buttons. Another number, such as 4 means, "Display the Yes and No" buttons. Microsoft has a list of button combinations and the numbers that you must use to display them in a messagebox.

    • 12

      Press "CTRL+S" to open the "Save As" window. Type "VBScript_MessageBoxTest.vbs" in the "File Name" box and click "Save."

    • 13

      Open Widows Explorer and locate the file. Double-click it. The messagebox will open and display the buttons. Click one of the buttons. The code will run and store the return value of the button that you clicked in the variable "clicked." Microsoft also has a return code values for each button. For example, if the return value is two, that means that you clicked the "OK" button. Microsoft also has a list of VBScript return values (See Resources).

Tips & Warnings

  • Different languages have different methods of displaying messageboxes and getting confirmation from the user. As you can see from the samples, JavaScript provides the simplest method while VBScript requires you to use cryptic return values to determine the buttons that a user cilcks.

Related Searches:

References

Resources

  • Photo Credit Ryouchin/Digital Vision/Getty Images

Comments

You May Also Like

Related Ads

Featured