Interacting with the Container

Activex controls are meant to be used as building blocks in application development. As such they are sited on Forms or other controls that can act as containers. As an Activex control designer you should be able to access the properties of a control’s container and adjust the control’s appearance according to the properties of the container. You will find two objects useful in designing custom controls: Extender and Ambient

The EX tender Object

The Extender object provides some of your control’s basic properties such as its Name Width and Height These are the properties that are maintained by Windows itself and they don’t trigger a Property Let procedure when they are set. As . ‘you know the control’s Name property can be changed at any time while the control is used In design mode but there are no Property procedures for this property. To find out the name  to the control by the user you must call the Extender object’s Name property. The Extender object is also your gateway to certain properties of the parent control the control on which the custom control is sited.

The name Property

You can find out the Name of the container control and its dimensions. To access the Extender object use the following expression

UserControl.Extender.extProperty

The eXf Property entry is an Extender property name. The name of the custom control is returned by the following expression:

User Control.Extender.Name

NOTE

do I really have to invoke the Extender object to find out-the custom control’s name from within the custom control? Isn’t this overkill?’ If you think about it the control doesn’t know its own name! The user can set the control’s Name property at any time during the control’s design and to read this name from within the control’s code, you must indeed call upon the services of the Extender object. There are no Property Let and Property Get procedures for the Name property.

The Width and Height Properties

These properties return the control’s dimensions as specified by the user. To find out the control’s dimensions use the expressions:

UserContro1.Extender.Width

and

UserControl.Extender.Height

The Tag and Index Properties

These are two more properties that Visual Basic maintains for you. Tag and Index aren’t properties the Extender object (although the syntax indicates that they are). They are properties of your custom control that can’t be accessed directly. I didn’t include any code for maintaining these properties, but they appear in the control’s Properties window anyway. Because their values are maintained by Visual Basic you can’t access them directly you must go through the Extender object

The Parent property

This property returns the object on which your control is sited. The UserContrql.Extender.Parent object is one way of accessing the container control’s properties. To find out the container control’s dimensions you can use the following statements:

PWidth – UserControl.Extender.Parent.Width
PHeight – UserControl.Extender.Parent.Height

You can use similar statements.to read the container control’s name (User Control.Extender.Parent.Name) it’s background (UserControl.Extender.Parent.BackColor) and so on

To experiment with a few of the Extender object’s dimensions, insert the following lines in the Flx Label User Control’s Click event run the test from and click the FLEX Label control.

Accessing the Extender Object

Private Sub UserControl_Click()
Dim ExtProp As String
ExtProp – ‘I’m a custom control. My name is
& UserControl.Extender.Name
ExtProp – ExtProp &I I m located at C- & UserControl .Extender: Left
& ‘t ‘ & UserControl.Extender.left & ‘)’
ExtProp – ExtProp &, vbCrLf & ‘My dimensions are ‘ _
&, UserControl.Extender.Width & ‘by  _
&, UserControl.Extender.Height
ExtProp – ExtProp & vbCrL f & ‘I’m tagged as ” _
& UserControl.Extender.Tag
ExtProp  ExtProp & vbCrLf & ‘I’m sited on a controt named
& UserControl.Extender.Parent.Name
ExtProp  ExtProp & vbCrLf & ‘whose dimensions are ” _
& UserControl.Extender.Parent.Width
&  by & UserControl.Extender.Parent’.Height
MsgBox ExtProp
RaiseEvent Click
End Sub

You will see the message box shown in Figure 16.7 (you must assign a value to the Tag property which is by default empty).

NOTE

This message is displayed from within the custom control’s Click event. from the test application, Only after the message is displayed does the test application receive the Click event. If you have programmed the control’s Click event in the test application as discussed earlier in the chapter you will see two message boxes. The first one is displayed from the User Control’s code the second is displayed from the application’s code

Use the Extender object to
access certain properties of
the custom control and its
container.

FIGURE 16.7:
FIGURE 16.7:

The Default and Cancel Properties

These two properties determine whether the custom control can be used as the Default (Extender.Default is True) or the Cancel (Extender.Cancel is True) control on the Form. These are two more properties you can’t set from within your code You can read them only while the control is used in the design of another application.

NOTE

The Default Cancel property of the User Control object determines whether one of the Default and Cancel properties of the control can be set. and not whether the control will be the Default or Cancel control on the Form. If Default Cancel is True, properties Default or Cancel will appear in the control’s Properties window. and you can find out the values of these properties through the Extender object.

The Ambient Object

The Ambient object is similar to the Extender object, in that it provides information about the custom control’s environment and the two actually overlap in some ways. The Ambient object gives your control’s code hints about the control’s environment, such as the container’s background color, its font, and so on. The single most important property of the Ambient object is User Mode which
indicates whether the control is operating in design or run time  mode. As you know from working with regular controls, all VB controls operate in design and run time modes. Because our control behaves identically in both modes there’s no need to between the two. In designing custom ActiveX controIs however you frequently need to differentiate between the two modes from  within your code and react to certain events differently depending on whether the control.. is being used in design-time or run time mode

The User Mode Property

This property is True when, the control is operating in run time mode and False whet) the control is operating in design mode. To see how this property works let’s display the text “Design Mode” while the control is in design mode. Open the Paint event’s subroutine and insert the following lines at the end of the Paint event’s handler

Using the User Mode Property

Private Sub User Control_Paint()
Draw Caption
Old Font Size – User Control.Fpnt.Size
User Control.Font.Size – 10
If Not Ambient.User Mode Then
User Control.CurrentX a 0
User Control.CurrentY – 0
UserControl.Print ‘Design Mode’
End If
UserControl.Font.Size – OldFontSize
End Sub

These statements determine whether the control is being used in design or run time mode. In design mode, the string “Design Mode” is displayed in its upper left comer as shown in Figure 16.8. The statements that save and restore the font size are needed, because without them either the “Design Mode” string will be displayed in a large font (same as the caption) or the caption will be printed in the same (small) size as the string. The code sets the font size to a small value displays the “Design Mode” string at the upper left corner of the control and then restores the original font size.

The ·Design Mode” string
is displayed in the control’s
upper left comer when It’s
open in design mode.

FIGURE 16.8:
FIGURE 16.8:

The For Color Back Color Properties

These two properties report the foreground and back ground colors of the container. Use them to initialize the equivalent properties of your custom control.

The Front property

This property reports the font used by the control’s controls container. All controls inherit the font setting of the container on which they are

Introduction

This is an introductory window which you can skip in the future by checking the Skip This Screen in the Future check box.

 

Scroll to Top