How are Objects Related to Each Other?

See Also

When you put two command buttons on a form, they are separate objects with distinct Name property settings (Command1 and Command2), but they share the same class — CommandButton.

They also share the characteristic that they're on the same form. You've seen earlier in this chapter that a control on a form is also contained by the form. This puts controls in a hierarchy. To reference a control you may have to reference the form first, in the same way you may have to dial a country code or area code before you can reach a particular phone number.

The two command buttons also share the characteristic that they're controls. All controls have common characteristics that make them different from forms and other objects in the Visual Basic environment. The following sections explain how Visual Basic uses collections to group objects that are related.

Object Hierarchies

An object hierarchy provides the organization that determines how objects are related to each other, and how you can access them. In most cases, you don't need to concern yourself with the Visual Basic object hierarchy. However:

There are some common cases in Visual Basic where one object contains others. These are described in the following sections.

Working with Collections of Objects

Collection objects have their own properties and methods. The objects in a collection object are referred to as members of the collection. Each member of the collection is numbered sequentially beginning at 0; this is the member's index number. For example, the Controls collection contains all the controls on a given form, as shown in Figure 5.10. You can use collections to simplify code if you need to perform the same operation on all the objects in a collection.

Figure 5.10   Controls collection

For example, the following code scrolls through the Controls collection and lists each member's name in a list box.

Dim MyControl as Control
For Each MyControl In Form1.Controls
   ' For each control, add its name to a list box.
   List1.AddItem MyControl.Name
Next MyControl

Applying Properties and Methods to Collection Members

There are two general techniques you can use to address a member of a collection object:

Once you're able to address all the members collectively, and single members individually, you can apply properties and methods using either approach:

' Set the Top property of the list box control to 200.
Controls!List1.Top = 200

–or–

Dim MyControl as Control
For Each MyControl In Form1.Controls()
   ' Set the Top property of each member to 200.
   MyControl.Top = 200 
Next MyControl

Objects That Contain Other Objects

Some objects in Visual Basic contain other objects. For example, a form usually contains one or more controls. The advantage of having objects as containers for other objects is that you can refer to the container in your code to make it clear which object you want to use. For example, Figure 5.11 illustrates two different forms you could have in an application — one for entering accounts payable transactions, and the other for entering accounts receivable transactions.

Figure 5.11   Two different forms can contain controls that have the same name

Both forms can have a list box named lstAcctNo. You can specify exactly which one you want to use by referring to the form containing the list box:

frmReceivable.lstAcctNo.AddItem 1201

–or–

frmPayable.lstAcctNo.AddItem 1201

Common Collections in Visual Basic

There are some common cases in Visual Basic where one object contains other objects. The following table briefly describes the most commonly used collections in Visual Basic.

Collection Description
Forms Contains loaded forms.
Controls Contains controls on a form.
Printers Contains the available Printer objects.

You can also implement object containment in Visual Basic.

For More Information   "For information about object containment, see "Using Collections" in "More About Programming." For information on the Printers collection, see "Working with Text and Graphics." For details on the forms and controls collections, see the Language Reference.

The Container Property

You can use the Container property to change an object's container within a form. The following controls can contain other controls:

This example demonstrates moving a command button around from container to container on a form. Open a new project, and draw a frame control, picture box control and a command button on the form.

The following code in the form's click event increments a counter variable, and uses a Select Case loop to rotate the command button from container to container.

Private Sub Form_Click()
   Static intX as Integer
   Select Case intX
      Case 0
      Set Command1.Container = Picture1
      Command1.Top= 0
      Command1.Left= 0

      Case 1
      Set Command1.Container = Frame1
      Command1.Top= 0
      Command1.Left= 0

      Case 2
      Set Command1.Container = Form1
      Command1.Top= 0
      Command1.Left= 0

   End Select
   intX = intX + 1
End Sub

For More Information   See "Container Property" in the Language Reference.

Communicating Between Objects

In addition to using and creating objects within Visual Basic, you can communicate with other applications and manipulate their objects from within your application. The ability to share data between applications is one of the key features of the Microsoft Windows operating system. With Visual Basic, you have great flexibility in how you can communicate with other applications.

For More Information   For details on using and communicating with other applications' objects, see "Programming with Components."