A Program Example Using OOP


Now that you know something about OOP, you can write a simple program with Visual Basic .NET and examine it with your new understanding of OOP. Assume that you have properly installed Visual Basic .NET according to the instructions in Chapter 1. Also assume that you are ready to start a new program and that your computer screen looks similar to Figure 1.29 from Chapter 1.

If you click the New Project button, you quickly see a screen similar to that shown in Figure 2.6. In that figure, notice that Visual Basic .NET has selected Visual Basic Projects by default as the project type. Also notice that Visual Basic .NET has selected Windows Application for the template type by default. Both of these default selections are fine for your application. (The environment used to write Visual Basic .NET programs in this book is capable of writing programs using other languages, too. However, we will stick to Visual Basic .NET for the time being.)

Figure 2.6. The New Project dialog box.

graphics/02fig06.jpg

Near the middle of Figure 2.6, you should see the Name text box filled in with the default name for the new project. In the figure, Visual Basic .NET has automatically supplied the name WindowsApplication1 . This default project name follows a naming convention that is used consistently throughout this book: The first letter of each word in a project name is capitalized. (Notice the W and A in the default project name in Figure 2.6.) In fact, the same naming convention is used for other elements in a program. You'll learn more about naming objects in Visual Basic .NET in subsequent chapters, especially Chapter 4.

The project name WindowsApplication1 isn't very informative. You can't tell much about the project from that name. Therefore, replace the name WindowsApplication1 with the name FirstProgram , which gives at least some idea of what the program is.

Below the project name in the New Project dialog box is a text box where you can specify where you wish to store the project on your computer. In Figure 2.6, Visual Basic .NET knows that I have been saving my recent programs in a disk directory named VBNetBook\Chapter02 on my D: disk drive. You should type in the name of the disk directory where you would like to have this project saved, as you will be using this project again later in the book.

Now click the OK button in the New Project dialog box. In a few moments, you should see a screen similar to that shown in Figure 2.7. This screen should look familiar to you because you saw it while you were testing the installation of Visual Basic .NET in Chapter 1. You need to spend some time with the Integrated Development Environment (IDE) now so you can become familiar with the many features that are available to you.

Figure 2.7. The Design window for Visual Basic .NET.

graphics/02fig07.jpg

At first blush, Figure 2.7 looks like a good candidate for information overload: There are a lot of things being presented to you all at once. But if you're going to eat an elephant, you need to do the task one bite at a time, until the job is done. And that's exactly what you're going to do here. Rather than flood your brain with an explanation of each and every button and icon on the screen, you will ease into the features of the IDE, learning about each one as you use it.

The Design Window

The Design window appears at the center of Figure 2.7. At the top of the Design window are two tabs that are labeled Start Page and Form1.vb [Design]. Because the Form1.vb [Design] tab is highlighted, you know that the IDE shown in Figure 2.7 is in Design mode for something called Form1.vb .

If you look below the two tabs, you should see a form displayed in the Design window with Form1 written on its title bar. You should also notice that eight small boxes surround the form, connected by a light-gray line. As you learned in Chapter 1, this means Form1 has the focus of the IDE.

The Properties Window

Near the right side of Figure 2.7 is a window labeled Properties. (If you do not see the Properties window, press F4.) Remember from earlier in this chapter that OOP is all about objects and that the state of each of those objects is determined by its properties. The form you see in the Design window is an object of type Form . At the present time, Visual Basic .NET has given this Form object the default name Form1 . In fact, Visual Basic .NET has supplied numerous default values to all the properties that describe the form, and that's the purpose of the Properties window: The Properties window allows you to see the current values of the form's properties and also allows you to change them.

Look closely at the Properties window in Figure 2.7. Notice that the Text property of Form1 is highlighted and that the box next to it shows the word Form1 . If you move your mouse cursor onto the word Form1 and double-click the mouse, you cause the word Form1 to be highlighted. Now you can simply type the words My First Program and press the Enter key. Your screen should now look similar to the one shown in Figure 2.8.

Figure 2.8. The Design window after you change the Text property.

graphics/02fig08.jpg

Notice that the title bar now shows the words you just typed. You have just changed the Text property of your first Form object! You're on a roll, so try changing another property of the form. Scroll the Properties window so that the top of the window is visible. You can do this by placing your mouse cursor on the scrollbar and, while holding the left mouse button down, dragging the scroll box to the top position. When you are done, your screen should look similar to Figure 2.9.

Figure 2.9. Changing the Name property.

graphics/02fig09.jpg

Notice in Figure 2.9 that the Name property is changed from Form1 to frmFirstProgram . You can see this new name spelled out just below the title bar for the Properties window. This change illustrates several things. First, it shows that every form has a Name property that you can change to just about any name you want. Second, even though you have the freedom to select any name for the form that you want, you should prefix the name with frm . This is a convention that is followed by almost every Visual Basic .NET programmer. Prefixing the form name with frm makes it easier to identify which object is being referred to if you need to reference the form in the program's code. Now any time you see frmFirstProgram in the program code, you know the code is referring to a form object named FirstProgram .

Programmer's Tip

graphics/tip_icon.gif

If you were eavesdropping on several programmers who were working on the frmFirstProgram program, you would probably hear them refer to the form as FirstProgram rather than frmFirstProgram . Experienced Visual Basic .NET programmers recognize that the frm element of the form's name is a naming convention, but the FirstProgram part of the form's name is what actually identifies the form.


You can scroll through the Properties window and examine the many properties that define the Form object. If you'd like a little additional information about the property you're examining, look at the gray area at the bottom of the Properties window, which gives additional information. For example, Figure 2.8 illustrates how the explanation window sheds a little more light on what the Text field is all about.

Expanding Properties

You might have noticed that in the Properties window, some properties have little boxes with plus signs in them at the left margin. This is Visual Basic .NET's way of telling you there is additional information associated with a property. For example, if you scroll down to the Location property, you see a box with a plus sign in it. If you click the plus sign, you see two new rows appear below the Location property row. These new rows are labeled X and Y , and they correspond to the x, y-coordinates of where the form is positioned on the screen. Also notice that what was the plus sign now appears as a minus sign inside the box. This is Visual Basic .NET's way of telling you that you are viewing the expanded definition of the Location property. If you click the minus sign, the X and Y property rows disappear and only the Location row is now visible. Also, the plus sign is now inside the box again.

Visual Basic .NET uses this plus box notation at several points in the IDE. We will explore the other plus boxes at the proper time. For the moment, remember that you can click the plus box to reveal additional information associated with the row where the plus box appears. Clicking the box a second time collapses that additional information back to its original, preexpanded, state.

Adding Objects to a Form

On the left side of the Design window shown in Figure 2.9 is another window, named Toolbox. The toolbox presents a scrollable list of predefined objects, or components , that you can add to a form. As you can see from the list, the toolbox provides a fairly large selection of labels, boxes, and other components that you can add to forms.

To add a label object to your form, you simply place the mouse cursor over the Label object in the toolbox and click. Then move the cursor over the form and click the mouse again. Your form should look like the one in Figure 2.10.

Figure 2.10. Adding a Label object to frmFirstProgram .

graphics/02fig10.jpg

Notice that the Label object has the focus of Visual Basic .NET. This means that the Properties window is displaying the properties that are available for the Label object.

Let's change a few of the Label control's properties. First, scroll down to the Text property and change the text to My name is Joyce . Next, right below the Text property is the TextAlign property, which simply determines where the text appears within the Label object. If you click this property, you are presented with nine boxes, which represent the positions where you can set the text. By default, the upper-left box is depressed, indicating that the text in the Label object will be positioned in the upper-left portion of the object. Clicking the box in the center causes the text to be centered in the Label object. If you press the Enter key (or click another part of the display screen), you see the TextAlign property filled in with the word MiddleCenter . You should also see that the Text property you just filled in is now centered within the Label object.

Now scroll the Properties window up to the top and change the Label object's Name property to lblName . Notice that the Visual Basic .NET naming convention of lbl is used to prefix the Name label for the Label object. While you're there, also change the BorderStyle property to Fixed3D . If you've made all these changes correctly, your display should look similar to that shown in Figure 2.11.

Figure 2.11. Setting the properties for the lblName object.

graphics/02fig11.jpg

Now, add two more objects to the form. To do so, you click the Button object in the toolbox list and then double-click the Form object to deposit the button on the form. Notice that double-clicking an object in the toolbox automatically places that object on the form. Now click the Button object on the form and, while holding down the left mouse button, drag the button toward the bottom-left side of the screen. Scroll the Properties window toward the bottom of the list and set the button's Text property to &Change . (You'll learn why the ampersand is there in a moment.) Now scroll the Properties window to the top of the list and change the Name property to btnChange . The Visual Basic .NET naming convention is to prefix all Button objects with btn .

Now that you're a pro at adding objects to a form, add a second Button object to the form and give it the name btnExit . You should also change its Text property to E&xit . If you've done this correctly, your form should look similar to the one shown in Figure 2.12.

Figure 2.12. The Form object with three component objects on it.

graphics/02fig12.jpg

Note the appearance of the text on each button. The letter C is underlined on the btnChange button object and the x is underlined on the btnExit button object. This happens because you placed an ampersand before these two letters on the buttons . These letters then become hotkeys for these buttons. That is, you could activate the btnChange object by clicking it with the mouse, or you could press the Alt+C key combination to activate it. Likewise, the btnExit object could be activated by using the Alt+X key combination. This is a standard Windows convention whereby the user can either use the mouse to click an object to activate it or use the object's hotkey ”the key with the underline ”to activate the object.



Visual Basic .NET. Primer Plus
Visual Basic .NET Primer Plus
ISBN: 0672324857
EAN: 2147483647
Year: 2003
Pages: 238
Authors: Jack Purdum

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