Freetutes.com

VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering


You are here: Visual Basic > Advanced VB6 tutorial > Chapter 13

Getting Information About Your Control's Environment with the Ambient Object and the AmbientChanged Event

The Ambient object gives you information about selected properties (such as ForeColor, BackColor, and Font) of your custom control's container. The AmbientChanged event fires whenever one of these selected properties changes, thus giving your control the chance to react to changes in its container.

A common use of the Ambient object would be to synchronize the custom control's background color with its container's in the AmbientChanged event, as in Listing 13.1. Notice that the AmbientChanged event receives a single parameter indicating which container property has just changed. We check the parameter to see if a property we are interested in has changed and, if so, we synchronize our control with the value of the same property of the Ambient object.

LISTING 13.1
CHECKING TO SEE WHICH CONTAINER PROPERTY HAS CHANGED IN THE AMBIENTCHANGED EVENT PROCEDURE

Private Sub UserControl_AmbientChanged _
            (PropertyName As String)
   If UCase$(PropertyName) = "BACKCOLOR" Then
      BackColor = Ambient.BackColor
   End If
End Sub

The LocaleID Ambient property is a number representing the country or language of the developer. For example, 1033 represents U.S. English (these numbers are often referred to in hex notation in documentation).

The UserMode property (a Boolean property) is an important Ambient object property for the control author. It tells you whether the current control instance is in design or run mode. When UserMode is True, the control instance is in a running application. When it's False, the control instance is in a design-time instance of its container.

You can check UserMode in the UserControl's Paint and Resize events or in a custom property's Property procedures, for example, to vary the behavior of your control. You can give your control one behavior if it's in a running application (Ambient.UserMode = True) and another when it's in the VB design environment (Ambient.UserMode = False). In the example of Listing 13.2, we implement a property that is writeable at design time but not at runtime by checking Ambient.UserMode in the Property Let procedure.

LISTING 13.2
THIS PROPERTY IS ONLY WRITEABLE AT DESIGN TIME

Property Let MyProp (sValue as String)
   If Not Ambient.UserMode Then
      m_MyProp = sValue
   End If
End Property

Some Ambient properties don't represent states of the container per se but rather states of your control relative to the container. For instance, the DisplayName Ambient property represents the name by which your control is known to the container (you should use Ambient.Displayname in error messages that you display in your control's code). Likewise the DisplayAsDefault Ambient property is True if the developer has set your control to be the Default control on its container (Default=True).


  

<< Previous | Contents | Next >>

Home | Link to Us | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved