Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Chapter 2: Visual Basic Programming Concepts
Last change: February 13 2016 18:48:07.
<<PreviousNext>>

A Simple Program Walkthrough

This program displays “Welcome To Visual Basic!!!” in a label on a form when you click a button with the text, “Say Welcome.” The first step is to start the IDE and create a solution folder. Follow these steps:

  1. Start the IDE. Click the Start button on the lower left corner of the desktop. Highlight All Programs in the Start menu. Highlight Microsoft Visual Studio 2008 (folder) in the Programs menu; then click the Microsoft Visual Studio 2008 icon (program) in the submenu. (Note: The appearance and wording may differ depending on your operating system.) The Start Page should appear similar to Figure 2-2.
  2. Start a new project. Click the New Project button in the center of the screen.
    1. Make sure the Windows Form Application icon in the Templates pane is highlighted when the New Project dialog box appears (see Figure 2-3).
    2. Assign a proper name for the solution. Enter Welcome as the name in place of the name that automatically appears in the Name text box.
    3. Click the OK button. The IDE should show a form in the center and all the other elements discussed in the preceding section.

You are now are ready to work on the program. This project involves three VB objects: a form, a label, and a button. As you can see, the form is readily available in the IDE at this point. You will, however, need to bring the label and the button from the Toolbox that is located on the left side in the IDE. Follow these steps:

  1. Resize the form so that its width is about one-quarter of the screen width, and its height is also about one-quarter of the screen height. You can resize the form by dragging one of the sizing handles on the sides and corners of the form. You drag by holding down the mouse button and moving away from the original point. Figure 2-6 shows the sizing handles of the form.
  2. Draw a label on the form. Double-click the Label icon in the Toolbox as shown in Figure 2-6. You should see the label with the text Label1 at the upper left corner of the form. Drag the label to the center of the form. (Rest the cursor in the label then drag.)

Figure 2-6
Sizing handles

  1. Draw a button on the form.
    1. Click the Button icon on the Toolbox.
    2. Point the mouse on the form below the label control; then drag down and to the right corner for about ⅓” × 1”.
    3. Release the mouse. You will see the new shape and size of the button (Figure 2-7). You can resize the button by dragging one of the its sizing handles, the same way you resized the form and the label.

Figure 2-7
Drawing a control on the form
 

  1. Change the button’s Text property to “Say Welcome.” You change the property setting through the Properties window. If you do not see the window onscreen, click the Properties Window option in the VB View menu, or press F4.
    1. Click the button on the form so that the Properties window shows the properties of this button.
    2. Use the scrollbar of the window to find the Text property. Type “Say Welcome to replace Button1 in the text box (Figure 2-8).

Figure 2-8
Setting the Text property for Button1

  1. Add code to the code window.
    1. Double-click the button on the form (not the icon in the Toolbox nor the form itself). The code window will appear in place of the form. You should see some code lines in the code window, as shown in Figure 2-9.
    2. Add two lines of code as shown below:

Private Sub Button1_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' Make the label display the Welcome message Label1.Text = "Welcome to Visual Basic!!!" End Sub

Figure 2-9
The code window

Notice that a small window showing various choices will appear immediately after you type “Label1., including the dot (see Figure 2-10). This small window is recognized as the IntelliSense, which provides all the legitimate choices for Label1. As you enter additional letters in the code, you will see different items in the IntelliSense highlighted. When you see Text highlighted, you can continue to type the remaining letters, enter the equal sign (=), or press the spacebar to complete that portion of the code line.

Figure 2-10
The IntelliSense

  1. Run the program. Click the Start button in the toolbar, click the Start option in the Debug menu, or press F5.
  2. Click the Say Welcome button in the running form. You should see the label now displays, “Welcome to Visual Basic!!!,” instead of its original Label1 (Figure 2-11).

Figure 2-11
The Welcome project in action

Congratulations! You have successfully completed a VB project. Click the Close (X) button on the form’s title bar to end the project. To exit the IDE, click File in the menu bar and then click Exit. Alternatively, click the Close button of the IDE. When the Close Project dialog box asks whether to save or discard changes to the current project, click the Save button. Then when the Save Project dialog box shows up, click the Save button to save the project as named in the default folder.

Tip
When entering code, beware of the difference between the lowercase L and the number 1. To the human eyes, l (lowercase L) and 1 (numeral 1) look similar. To the computer, however, they are completely different. Some beginners tend to be careless in distinguishing the two. The consequence can range from an obvious syntax error with an immediate fix to days of searching for a mysterious bug in the program. A similar situation exists between the letter O (or its lowercase o) and the number 0.
Last change: February 13 2016 18:48:11.
<<PreviousNext>>