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

Interfaces of VB Objects

You have worked with a form and two controls. Forms and controls are collectively recognized as objects. Each type of object provides different functionality. For example, forms serve as containers for other objects and as windows for the visual interface. Labels are used to display text, which is usually a simple message or instruction for the user. Buttons are used to initiate specific actions when the user clicks them. All objects provide interfaces for your code. These interfaces include properties, events, and methods. The following sections take a closer look at each.

Properties

Objects have properties—special types of data associated with the objects. Most properties relate to the appearance of the objects. Other properties determine how an object behaves. Many properties can be set at either design time or run time, while other properties can be set only at design time.  Still others can be set or available only at run time. In code, you refer to a property of an object by the following syntax:

Object.Property

The object name and the property name are separated by a dot (.). There should be no spaces in between. For example, you can refer to the background color property of the label named Label1 by coding:

Label1.BackColor

To set its background color to red, you will code:

Label1.BackColor = Color.Red

where Color.Red is a constant name recognized in VB and represents the red color. Many controls have common properties that will be explored in the next section.

Events

In addition to properties, objects also have events that are user or system actions recognized by the object. As you can see from the Welcome project, the button has a Click event. When the user clicks the button, the Click event is raised (triggered). Different objects recognize different events. As discussed in Chapter 1, “Introduction,” VB is an event-driven language. When a VB project starts to run, it waits for events to occur and then responds by executing the code written to handle these events accordingly. You write code for each of the events that you want the program to handle. The procedure written to handle an event is called an event procedureor event handler. Event procedures have the following syntax structure:

Private Sub ObjectName_Event(parameter list) Handles Object.Event <Code to handle the event> End Sub

The first line of the procedure starts with Private Sub, which is used to indicate the beginning of a Sub procedure. The object name identifies the object of interest, and the object and event are separated by an underscore. A pair of parentheses that enclose a list of parameters follows the event name. Typically, the parameter list looks similar to the following:

ByVal Sender As System.Object, ByVal e As System.EventArgs

As you can see, the list has two parameters. The first parameter identifies the message sender, or the object that makes the call. The second parameter gives the event arguments. Its contents will vary depending on the event. These parameters will be explored further in other chapters where applicable.
The End Sub line physically defines the end of the procedure; that is, any lines beyond this point have nothing to do with this procedure. When execution control drops down to this line, it will return to the point where this procedure was triggered; that is, it will return to the message sender. In most cases, this means the computer will be waiting for another event to invoke another event procedure.

Tip
You might have discovered some confusing terminology in the previous discussion. The term, control has been used in two different contexts. Initially, those objects dragged into the form from the Toolbox were introduced as controls. The flow of execution in a program, however, is also referred to as control in the previous paragraph. For clear differentiation, this book uses execution control to refer to execution flow. Unless the context is clear, the term VB control will be used to refer to VB objects.

Exploring Available Events of an Object

You can find what events an object can recognize and what parameters each event procedure has by exploring the code window. The Object box at the upper left corner has a list of all the objects on the form. You can see the names of these objects by clicking the drop-down button. The Procedure box on the right shows the list of events the object on the left recognizes (see Figure 2.12). If you click an event in this list, an event procedure template will appear in the code window. This is the standard way to start writing code to handle a particular event.

 

Figure 2-12
The Object box and the Procedure box

Tip
Always use the procedure template provided by VB to code an event procedure. When in the code window, first select the object name from the object box; then select the event from the procedure box. The event procedure template will appear in the code window.
A project can have many event procedures. The relative position of these procedures is not important. Procedures get invoked by the events, and are not executed in the order they are placed in the code window. In contrast, the order of the code lines within the procedure is very important. In general, these lines are executed top down, line by line—or more accurately, statement by statement. The result of executing a line may depend on the results from the previous lines. Any lines that are placed out of order can cause an erroneous result.

Try This
In the code window of your current project, select (Form1 Events) from the Object box on the left; then select KeyPress from the Procedure box on the right. You should see the following event procedure template in the code window:
Private Sub Form1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress End Sub
You may want to try a few more, just for fun and familiarity.

Methods

Finally, objects have methods—actions that objects are capable of performing. For example, SetBounds is a method that many objects have. The following code will set bounds for (move) the label you have created to a random location on the form. For the time being, however, do not worry about how the code works.

Label1.SetBounds(Rnd() * Width, Rnd() * Height, Label1.Width, Label1.Height)

The syntax to use a method is as follows:

Object.Method(Parameter List)

Notice that there is a dot between the object and the method, as is the case in the code for the property. Some methods have required parameters; some have optional parameters; other methods require no parameters. Notice also that there is a pair of parentheses enclosing the parameter list. This pair is required, even when the method requires no parameter.

These parameters provide information for the method to perform proper actions. For example, the SetBounds method just shown requires four parameters. Its syntax appears as follows:

Object.SetBounds(x, y, Width, Height)

where Object = a control or form,
x, y = a pair of numbers indicating the coordinate at which the left side and top edge of the object  should be aligned.
Width, Height = a pair of numbers giving the width and height of the object.

The preceding statement that starts with Label1tells the label to set its boundary at (move to) a position in which its upper left corner is at the point as defined by the first two parameters, keeping its width and height the same as its previous size.

Try This
Insert the Label1.SetBounds code line to the Button1_Click event procedure you created in the previous section. Run the project and then click the Say Welcome button as many times as you want. You should see the Welcome label move all over the form. Remove this line of code to continue this chapter.
Last change: February 13 2016 18:48:07.
<<PreviousNext>>