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>>

Coding the Revised Project

Now turn your attention to what code you need for this revised project. Because all the required properties of all controls have been set, your main focus is to make the label move across the form. Here is the list of questions you need to answer:

Placing the Label on the Right Margin of the Form

The label has a Left property, which locates the label’s left side on the form. The right margin of the form should have the value of the entire width of the form (see Figure 2-17). If you set the Left property of the label to the form’s width, the label will be placed on the right margin of the form. The code should be as follows:

lblWelcome.Left = Me.Width

Figure 2-17
Aligning the label on the right margin of the form

In which event procedure should you place the preceding statement? You want this line to be executed as soon as the project starts. The event that is raised when the project starts is the Form Load event that occurs when the form is being loaded into memory. This event is where you can place code to set initial values for various properties and data before the occurrence of any other events. To get the event code template from the code window, do the following:

  1. Select (frmWelcome Events) from the Object box.
  2. Select the Load event from the procedure box. The procedure template should appear in the code window.

Alternatively, double-click the form. The code will appear with the form load event procedure template.
The code should appear as follows:

Private Sub frmWelcome_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Place the label on the right margin of the form. lblWelcome.Left = Me.Width End Sub

Moving the Label Across the Form

To move the label across the form, you can use the SetBounds method discussed previously in this chapter; however, you will need to provide (code) the four required parameters. Alternatively, you can set the label’s Left property. Each time this property changes, the label repositions itself horizontally. For example, you can use the following code:

lblWelcome.Left = lblWelcome.Left – lblWelcome.Width / 10

This statement will subtract one-tenth of the label’s width from its current Left property value and assign the result to this property, causing the label to move to the left by one-tenth of the label’s width. Notice that the equal sign in the statement does not mean equal, but rather to move data from the right side to the left side of the equal sign.

Now, imagine that you have a button, and you place this statement in its Click event. Each time you click the button, the label will move to the new position. If you can click the button at an even tempo, the label will move smoothly across the form. It is, of course, hard for anybody to click the button evenly. And even if you can, you will get tired quickly! That’s why you use the timer.
The timer keeps track of time. At design time, you can see its appearance below the form and set its properties. At run time, it disappears completely. When enabled, all it does is keep track of time. The timer has one event—the Tick event—that is similar to a Click event; when the Tick event is raised (triggered), the code in the event procedure is executed. The Tick event, however, is not raised by a click, but rather by the time interval you set for its Interval property. An interval value of 1 is equivalent to one one-thousandth (1/1,000) of a second. If you set its interval property to 250, the Tick event will be triggered every quarter of a second. You can think that the timer ticks every quarter of a second. If you place the preceding statement to set the label’s Left property in this Tick event, the label will move left every quarter of a second by one-tenth of the label’s width. The code should appear as follows:

Private Sub tmrWelcome_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrWelcome.Tick lblWelcome.Left = lblWelcome.Left - lblWelcome.Width / 10 End Sub

To enter the code shown, do the following:

  1. Make sure that the timer’s name has been set to tmrWelcome. Click the timer below the form,and verify its name in the Properties window. Also verify that the Enabled property is set to True, and the Interval property is set to 250.
  2. Double-click the timer. The timer’s Tick event procedure should appear in the code window.
  3. Type in the preceding code line.

Determining Whether the Label Has Completely Disappeared

If you test the program now, the label will appear from the right margin of the form and then gradually move to the left until it disappears; however, it will not reappear. So how can your code tell if the label has completely disappeared from the form? The Left and Top properties of all the controls on the form are set relative to the upper left corner of the form, which has a coordinate value (0, 0). When the label’s Left property has a value zero, the label’s left margin is aligned with the left margin of the form (see Figure 2-17). At that time, the entire width of the label still appears on the form. This means when the label’s Left property plus its width is less than 0, the entire label has moved out of (and disappeared) from the form. At this point, it will be time to move the label to the right margin of the form again; an If block can be coded as follows:

If lblWelcome.Left + lblWelcome.Width <= 0 Then lblWelcome.Left = Me.Width ‘place the label on the right margin of the form Else End If

The If statement tests whether the Left property plus the Width property is less than 0. If so, the line below it (the assignment statement) will be executed. The assignment statement moves the label to the right margin of the form, as explained in the preceding subsection.
You now need to determine what code you should place between Else and End If. This will be the situation that the label can still be seen on the form. That’s when the label should move in the normal way. The complete Timer Tick event procedure should appear as follows:

Private Sub tmrWelcome_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrWelcome.Tick If lblWelcome.Left + lblWelcome.Width <= 0 Then ' Label has disappeared from the form. Place it on the right margin. lblWelcome.Left = Me.Width ‘Place the label on the right margin of the form Else ' Label can be seen. Keep moving to the left. lblWelcome.Left = lblWelcome.Left - lblWelcome.Width / 10 End If End Sub

Modify your timer procedure to match the preceding code.

Ending the Program

All the questions raised at the beginning of coding this revised project have been answered. All that is left now is to consider how the program should end. Like all Windows-based programs, your project unloads when the user clicks the Close button in its control box. Although many users will click the Close button to quit, it is a good practice to provide the user with a more formal way to exit. The Quit button is used for this purpose. You can use the form’s Close method to terminate the program. The code should appears as follows:

Private Sub btnQuit_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click Me.Close() End Sub

In the code Me is a special name for the current form. The Close method will close the form, ending the project in effect. Notice that all methods require a pair of parentheses even if no parameters are required.

Tip
Another way to quit a program is to use the End statement:
Private Sub btnQuit_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click End End Sub Question Mark ?  
There are some differences in the effects of the Close method and the End statements. This aspect is discussed in more detail in Chapter 11, “Menus and Multiple-Form Applications,” when you work with multiple forms. Suffice it to say that the Close method is considered less abrupt and a better way to exit.

Revised Project in Summary

The complete code should include three event procedures:

Test your project. You can also try to click the form’s Maximize button so that the form will cover the entire screen. Enjoy the Welcome banner as it moves across the form to say Welcome!!!
When you quit the IDE and are asked whether to save the project, click the Save button. Then create a new Chapter02 folder under Visual Studio 2008/Projects and save your program under this newly created folder.

Last change: February 13 2016 18:48:11.
<<PreviousNext>>