Section 2.6. Using Visual Programming to Create a Simple Program that Displays Text and an Image


2.6. Using Visual Programming to Create a Simple Program that Displays Text and an Image

In this section, we create a program that displays the text "Welcome to Visual Basic!" and an image of the Deitel & Associates bug mascot. The program consists of a single form that uses a Label and a PictureBox. Figure 2.27 shows the result of the program as it executes. The program and the bug image are available with this chapter's examples. You can download the examples from www.deitel.com/books/vbforprogrammers2/index.html. Please read the Before You Begin section of the Preface to ensure that you install the examples correctly on your computer.

Figure 2.27. Simple program executing.


To create the program whose output is shown in Fig. 2.27, you will not write a single line of program code. Instead, you will use the techniques of visual programming. Visual Studio processes programmer actions (such as mouse clicking, dragging and dropping) to generate program code. Chapter 3 begins our discussion of writing program code. Throughout the book, you produce increasingly substantial and powerful programs that usually include a combination of code written by you and code generated by Visual Studio. The generated code can be difficult for novices to understandfortunately programmers rarely need to look at this code.

Visual programming is useful for building GUI-intensive programs that require a significant amount of user interaction. Visual programming cannot be used to create programs that do not have GUIsyou must write such code directly.

To create, run and terminate this first program, perform the following steps:

1.

Create the new project. If a project is already open, close it by selecting File > Close Solution. A dialog asking whether to save the current project might appear. Click Save to save any changes. To create a new Windows application for the program, select File > New Project... to display the New Project dialog (Fig. 2.28). From the template options, select Windows Application. Name the project ASimpleProgram and click OK. [Note: File names must conform to certain rules. For example, file names cannot contain symbols (e.g., ?, :, *, <, >, # and %) or Unicode® control characters (Unicode is a special character set described in Appendix E). Also, file names cannot be system reserved names, such as "CON", "PRN", "AUX" and "COM1" or "." and "..", and cannot be longer than 256 characters in length.] We mentioned earlier in this chapter that you must set the directory in which the project will be saved. In the complete Visual Studio, you do this in the New Project dialog. To specify the directory in Visual Basic 2005 Express, select File > Save All to display the Save Project dialog (Fig. 2.29). To set the project location, click the Browse... button, which opens the Project Location dialog (Fig. 2.30). Navigate through the directories, select one in which to place the project (in our example, we use a directory named My Projects) and click OK to close the dialog. Click Save in the Save Project dialog (Fig. 2.29) to save the project and close the dialog.

Figure 2.28. New Project dialog.


Figure 2.29. Save Project dialog.




Figure 2.30. Setting the project location in the Project Location dialog.


When you first begin working in the IDE, it is in design mode (i.e., the program is being designed and is not executing). While the IDE is in design mode, programmers have access to all the environment windows (e.g., Toolbox, Properties), menus and toolbars, as you will see shortly.

2.

Set the text in the form's title bar. The text in the form's title bar is determined by the form's Text property (Fig. 2.31). If the Properties window is not open, click the properties icon in the toolbar or select View > Properties Window. Click anywhere in the form to display the form's properties in the Properties window. Click in the textbox to the right of the Text property box and type "A Simple Program," as in Fig. 2.31. Press the Enter key (Return key) when finished; the form's title bar is updated immediately (Fig. 2.32).

Figure 2.31. Setting the form's Text property in the Properties window.




Figure 2.32. Form with enabled sizing handles.


3.

Resize the form. Click and drag one of the form's enabled sizing handles (the small white squares that appear around the form, as shown in Fig. 2.32). Using the mouse, select the bottom-right sizing handle and drag it down and to the right to make the form larger (Fig. 2.33).

Figure 2.33. Resized form.


4.

Change the form's background color. The BackColor property specifies a form's or control's background color. Clicking BackColor in the Properties window causes a down-arrow button to appear next to the value of the property (Fig. 2.34). When clicked, the down-arrow button displays a set of other options, which vary depending on the property. In this case, the arrow displays tabs for Custom, Web and System (the default). Click the Custom tab to display the palette (a grid of colors). Select the box that represents light blue. Once you select the color, the palette closes and the form's background color changes to light blue (Fig. 2.35).



Figure 2.34. Changing the form's BackColor property.


Figure 2.35. Form with new BackColor property applied.


5.

Add a Label control to the Form. If the Toolbox is not already open, select View > Toolbox to display the set of controls you will use for creating your programs. For the type of program we are creating in this chapter, the typical controls we use will be located in either the All Windows Forms category of the Toolbox or the Common Controls group. If either group name is collapsed, expand it by clicking the plus sign (the All Windows Forms and Common Controls groups are shown in Fig. 2.21). Next, double click the Label control in the Toolbox. This action causes a label to appear in the upper-left corner of the form (Fig. 2.36). [Note: If the Form is behind the Toolbox, you may need to close the Toolbox to see the Label.] Although double clicking any Toolbox control places the control on the form, you also can "drag" controls from the Toolbox to the form (you may prefer dragging the control because you can position it wherever you want). Our Label displays the text Label1 by default. Note that our Label's background color is the same as the form's background color. When a control is added to the form, its BackColor property is set to the form's BackColor. You can change the Label's background color by changing its BackColor property.

Figure 2.36. Adding a Label to the form.


6.

Customize the Label's appearance. Select the Label by clicking it. Its properties now appear in the Properties window. The Label's Text property determines the text (if any) that the Label displays. The form and Label each have their own Text propertyforms and controls can have the same types of properties (such as BackColor, Text, etc.) without conflict. Set the Label's Text property to Welcome to Visual Basic!. Note that the Label resizes to fit all the typed text on one line. By default, the AutoSize property of the Label is set to true, which allows the Label to update its size to fit all of the text if necessary. Set the AutoSize property to False (Fig. 2.37) so that you can resize the Label on your own. Resize the Label (using the sizing handles) so that the text fits. Move the Label to the top center of the form by dragging it or by using the keyboard's left and right arrow keys to adjust its position (Fig. 2.38). Alternatively, when the Label is selected, you can center the Label control horizontally by selecting Format > Center InForm > Horizontally.

Figure 2.37. Changing the Label's AutoSize property to False.


Figure 2.38. GUI after the form and Label have been customized.


7.

Set the Label's font size. To change the font type and appearance of the Label's text, select the value of the Font property, which causes an ellipsis button () to appear next to the value (Fig. 2.39). When the ellipsis button is clicked, a dialog that provides additional valuesin this case, the Font dialog (Fig. 2.40)is displayed. You can select the font name (e.g., Microsoft Sans Serif, MingLiU, Mistral, Modern No. 20the font options may be different depending on your system), font style (Regular, Italic, Bold, etc.) and font size (16, 18, 20, etc.) in this dialog. The text in the Sample area provides sample text with the selected font settings. Under Size, select 24 points and click OK. If the Label's text does not fit on a single line, it wraps to the next line. Resize the Label vertically if it's not large enough to hold the text.



Figure 2.39. Properties window displaying the Label's properties.




Figure 2.40. Font dialog for selecting fonts, styles and sizes.


8.

Align the Label's text. Select the Label's TextAlign property, which determines how the text is aligned within the Label. A three-by-three grid of buttons representing alignment choices is displayed (Fig. 2.41). The position of each button corresponds to where the text appears in the Label. For this program, set the TextAlign property to MiddleCenter in the three-by-three grid; this selection causes the text to appear centered in the middle of the Label, with equal spacing from the text to all sides of the Label. The other TextAlign values, such as TopLeft, TopRight, and BottomCenter, can be used to position the text anywhere within a Label. Certain alignment values may require that you resize the Label larger or smaller to better fit the text.

Figure 2.41. Centering the Label's text.


9.

Add a PictureBox to the form. The PictureBox control displays images. The process involved in this step is similar to that of Step 5, in which we added a Label to the form. Locate the PictureBox in the Toolbox (Fig. 2.21) and double click it to add it to the form. When the PictureBox appears, move it underneath the Label, either by dragging it or by using the arrow keys (Fig. 2.42).



Figure 2.42. Inserting and aligning a PictureBox.


10.

Insert an image. Click the PictureBox to display its properties in the Properties window (Fig. 2.43). Locate the Image property, which displays a preview of the image, if one exists. No picture has been assigned, so the value of the Image property displays (none). Click the ellipsis button to display the Select Resource dialog (Fig. 2.44). This dialog is used to import files, such as images, to any program. Click the Import... button to browse for an image to insert. In our case, the picture is bug.png. In the dialog that appears, locate the image file, select it and click OK. The image is previewed in the Select Resource dialog (Fig. 2.45). Click OK to place the image in your program. Supported image formats include PNG (Portable Network Graphics), GIF (Graphic Interchange Format), JPEG (Joint Photographic Experts Group) and BMP (Windows bitmap). Creating a new image requires image-editing software, such as Jasc® Paint Shop Pro™ (www.jasc.com), Adobe® Photoshop™ Elements (www.adobe.com) or Microsoft Paint (provided with Windows). To size the image to the PictureBox, change the SizeMode property to StretchImage (Fig. 2.46), which scales the image to the size of the PictureBox. Resize the PictureBox, making it larger (Fig. 2.47).

Figure 2.43. Image property of the PictureBox.




Figure 2.44. Select Resource dialog to select an image for the PictureBox.


Figure 2.45. Select Resource dialog displaying a preview of selected image.


Figure 2.46. Scaling an image to the size of the PictureBox.




Figure 2.47. PictureBox displaying an image.


11.

Save the project. Select File > Save All to save the entire solution. The solution file contains the name and location of its project, and the project file contains the names and locations of all the files in the project.

12.

Run the project. Recall that up to this point we have been working in the IDE design mode (i.e., the program being created is not executing). In run mode, the program is executing, and you can interact with only a few IDE featuresfeatures that are not available are disabled (grayed out). The text Form1.vb [Design]* in the project tab (Fig. 2.48) means that we are designing the form visually rather than programmatically. If we had been writing code, the tab would have contained only the text Form1.vb. The * at the end of the text in the tab indicates that the file has been changed and should be saved. To run the program you must first build the solution. Select Build > Build ASimpleProgram to compile the project (Fig. 2.48). Once you build the solution (the IDE will display "Build succeeded" in the lower-left corneralso known as the status bar), select Debug > Start Debugging to execute the program (or you can select the F5 key). Figure 2.49 shows the IDE in run mode (indicated by the title bar text A Simple Program (Running) Microsoft Visual Basic 2005 Express Edition). Note that many toolbar icons and menus are disabled since they cannot be used while the program is running. The running program will appear in a separate window outside the IDE as shown in the lower-right portion of Fig. 2.49.

Figure 2.48. Building a solution.


Figure 2.49. IDE in run mode, with the running program in the foreground.


13.

Terminate execution. Click the running program's close box (the X in the top-right corner of the running program's window). This action stops the program's execution and returns the IDE to design mode.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net