3.8. Using a Message Dialog to Display a MessageThe programs discussed thus far display output in the Console window. Visual Basic programs often use message dialog windows to display output. Visual Basic provides class MessageBox for creating message dialogs. We use a message dialog in Fig. 3.24 to display the square root of 2. For this program to compile and execute, you must perform several steps discussed later in this section. Be sure to read the entire section as you create this example. Figure 3.24. Displaying text in a message dialog.
Note that in the output for Fig. 3.24, the square root of 2 is displayed with many digits to the right of the decimal point. In many applications, you won't need such precise output. In the next chapter, we demonstrate how to display only a few digits to the right of the decimal point. Displaying a MessageBoxFigure 3.24 presents a program that creates a simple GUI (i.e., the message dialog). The .NET Framework Class Library (FCL) contains a rich collection of classes that can be used to construct GUIs. FCL classes are grouped by functionality into namespaces, which make it easier for programmers to find the classes needed to perform particular tasks. Line 4 is an Imports statement indicating that we are using the features provided by the namespace System.Windows.Forms, which contains windows-related classes (i.e., forms and dialogs). We will discuss this namespace in more detail after we discuss the code in this example. Line 10 calls the Sqrt method of the Math class to compute the square root of 2. The value returned is a floating-point number, so we declare the variable root as type Double. Variables of type Double can store floating-point numbers. We declare and initialize root in a single statement. Note the use of spacing in lines 1314 of Fig. 3.24. To improve readability, long statements may be split over several lines using the line-continuation character, _ . Line 13 uses the line-continuation character to indicate that line 14 is a continuation of the preceding line. A single statement can contain as many line-continuation characters as necessary. At least one whitespace character must precede each line-continuation character. Common Programming Error 3.4
Common Programming Error 3.5
Common Programming Error 3.6
Common Programming Error 3.7
Good Programming Practice 3.6
Lines 1314 (Fig. 3.24) call method Show of class MessageBox. This method takes a comma-separated argument list. The first argument is the string that is displayed in the message dialog. The second argument is the string that is displayed in the message dialog's title bar. Good Programming Practice 3.7
Analyzing the MessageBoxWhen executed, lines 1314 (Fig. 3.24) display the message dialog shown in Fig. 3.25. The message dialog includes an OK button that allows the user to dismiss (i.e., close) the message dialog by clicking the button. After you dismiss the message dialog, the program reaches the end of Main and terminates. You can also dismiss the message dialog by clicking the dialog's close boxthe button with an X in the dialog's upper-right corner. You will see various dialogs throughout this book, some with more buttons than the OK button and the close box. In Chapter 13, you'll see how to determine which button was pressed and have your application respond accordingly. For instance, you can have one action occur when the user clicks the OK button and a different action occur when the user clicks the close box. By default, these buttons simply dismiss the dialog. Figure 3.25. Message dialog displayed by calling MessageBox.Show.If you create this example and enter the code in Fig. 3.24, you will notice that line 13 gives you the error Name 'MessageBox' is not declared. Some classes provided by Visual Basic (such as MessageBox) must be added to the project before they can be used in a program. These compiled classes are located in a file, called an assembly, that has a .dll (dynamic link library) file extension. Information about the assembly our program must reference so that it can use class MessageBox can be found in the Visual Basic Express documentationalso called the MSDN (Microsoft Developer Network) Documentation. To locate this information, select Help > Index. This displays the Visual Basic Express documentation in a separate window that contains an Index dialog (Fig. 3.26). Figure 3.26. Obtaining documentation for a class by using the Index dialog.Type the class name MessageBox in the Look for: box, and select the appropriate filter, which narrows the search to a subset of the documentationwe selected Visual Basic Express in Fig. 3.26. You should select whichever edition of Visual Basic you are using. Next, click the about MessageBox class link (Fig. 3.26) to display documentation for the MessageBox class (Fig. 3.27). The documentation lists the assembly that contains the class. Class MessageBox is located in assembly System.Windows.Forms.dll. Note that the file name is listed in Fig. 3.27 using lowercase letters. Uppercase and lowercase letters can be used interchangeably, because file names are not case sensitive. Figure 3.27. Documentation for the MessageBox class.Adding a Reference to an AssemblyWe must add a reference to this assembly if we wish to use class MessageBox in our program. Visual Basic 2005 Express provides a simple process for adding a reference. To see which assemblies are currently referenced in a project, you need to view the project's References folder. To do this, click the Show All Files button of the Solution Explorer (Fig. 3.28), which displays any of the project's files that are normally hidden by the IDE. If you expand the References folder (i.e., click the + to the left of the folder name), you will see that System.Windows.Forms is not listed. It is listed in Fig. 3.29(c)we will discuss this figure shortly. Figure 3.28. Viewing a project's references.Figure 3.29. Adding an assembly reference to a project in the Visual Basic 2005 Express IDE.(a) Add Reference dialog displayed when you select Project > Add Reference...
To add a reference to an existing project, select Project > Add Reference to display the Add Reference dialog (Fig. 3.29(a)). Scroll through the list of DLLs in the .NET tab and click System.Windows.Forms.dll to add it to the References folder, then click OK. Note that System.Windows.Forms was not originally listed in the References folder (Fig. 3.29(b)), but after we add the reference using the Add Reference dialog, it is listed (Fig. 3.29(c)). Common Programming Error 3.8
Now that the assembly System.Windows.Forms.dll is referenced, we can use the classes that are part of the assembly. The namespace that includes class MessageBox, System.Windows.Forms, also is specified with the Imports statement in line 4 of our code (Fig. 3.24). The Impors statement is not added to the program by Visual Basic 2005 Express; you must add this line to your code. We did not have to add references to any of our previous programs because Visual Basic 2005 Express adds references to some assemblies when the project is created. The references added depend on the project type that you select in the New Project dialog. In addition, some assemblies do not need to be referenced. For example, class Console is located in the assembly mscorlib.dll, but we did not need to reference this assembly explicitly to use it. GUI Components and the System.Windows.Forms NamespaceThe System.Windows.Forms namespace contains many classes that help Visual Basic programmers define graphical user interfaces (GUIs) for their applications. GUI components (such as Buttons) facilitate both data entry by the user and presenting data outputs to the user. For example, Fig. 3.30 is an Internet Explorer window with a menu bar containing various menus, such as File, Edit and View. Below the menu bar is a tool bar that consists of buttons. Each button, when clicked, executes a task. Beneath the tool bar is a combo box in which the user can type the location of a Web site to visit. To the left of this combo box is a label that indicates the purpose of the text box (in this case, Address). The menus, buttons, text boxes and labels are part of Internet Explorer's GUI. They enable users to interact effectively with the Internet Explorer program. Visual Basic provides classes for creating the GUI components shown here. Other classes that create GUI components are described throughout this book (beginning in the next chapter), and covered in detail in Chapter 13, Graphical User Interface Concepts: Part 1 and Chapter 14, Graphical User Interface Concepts: Part 2. Figure 3.30. Internet Explorer window with GUI components. |