Home -> Programming -> Processing -> User Messages

User Messages Using Dialog Boxes

Dialog Box is the generic name given to any window which pops up in front of the main programs window. These window either display information to the user, or ask the user to supply information. We will deal with Dialog Boxes more generally later. For now I will introduce a specific form of the Dialog Box: the Message Box.

Message Box

When we want to communicate with the user, we can display content on the program form, but we can also use the Message Box feature. A Message Box pops a small window up in front of our main form that can display a message and ask for user response based on the buttons shown in the message box. This is often used to inform the user of something. One common usage is in error trapping. We can let the user know they have not entered either required data, or what they have entered is not the correct data type. Our programming purpose is to prevent our program from crashing as it will if we try to process incompatible data. For the user it informs them of something which they may have overlooked or just plain missed. We do need to be careful not to use this tool to force the user into a straight jacket by requiring certain information that may be helpful but is not really required. The basic technique to display a message box is:

    Messagebox.Show(message, title, buttons, icon)

The explanation for this line of code is as follows:
     message: this can be a literal or a variable and it is the text which will be displayed in the body of the message box.
     title: this can be a literal or a variable. Since it is a window which pops up it has a title bar across the top. Whatever we enter here will display in that title bar.
     buttons: this parameter shows up as a pop up menus with the available options shown. Select the one you want to use.
     icon: this is optional and allows us to show one of a variety of icons.

One of the most common uses of the message box is to tell the user something, and if we are only informing them of some fact, we only display the OK button. Do not give the user a choice of buttons if there is no choice to make.

There is another way we can use a message box. In this case we are asking the user to make a decision, and so we need to provide more than just one button so they can make a proper response. We also need to know which button they pressed in order to process each response. This requires us to use a slightly different form of the message box known as the Message Box function. The above example looks much like we are calling a procedure, but here we need to know which button was clicked, or to put it another way, we need a return value which indicates which button was clicked. The general form of this usage is as follows:

    rResponse = MessageBox.Show(message, title, buttons, icon)

Response: this is a variable which is normally declared locally as data type Messagebox.Result to capture the user's response. We can then use the result in a decision structure to send our program down the path that the user has chosen.

Input Box

At times we want some information from the user. Normally we do this by placing controls on a form but sometimes we are after specific information that is only needed at a certain place in the program and not used otherwise. The Input Box provides a small form which is used just for this purpose. It behaves much like the functional message box but allows us to retrieve a piece of data with minimal effort in coding. The general form looks like this:

     response = InputBox.Show(message, title, default, Xpos, Ypos)

Both response and default are strings. Xpos is the display starting position across the window. Ypos is the display starting position vertically.

The programmer needs to check that the user did not click the Cancel botton. If it was clicked the repsonse will be an empty string. If the response is not an empty string then the program should perform normal error trapping to make sure that the data is of the data type that the programmer expects. All the arguments except message are optional.