Creating Custom Interface Members

In this window you add the properties events and methods that are unique to your custom control. Follow these steps:

1. Click the New button to display the Add Custom Member dialog box.

 

2. In the Name box, enter Caption (which is the name of a property)and select the Property radio button.
3. Click OK. The name of the first custom property is now displayed in the My Custom Members box of the Create Custom Interface Members window.
4. Repeat steps 1 through 3 to add the Text Alignment and Effect properties.

If you have misspelled any of the property names or if you want to delete one and re-enter it use the Edit and Delete buttons. When you are done click the Next button.

Setting Member Mapping

You use this window to map certain properties of your custom control to properties of the so-called constituent controls. A constituent control is a regular VB control that your custom control uses. Suppose your custom control contains a Label control and you want the label’s background color to become your control’s background color (or foreground color fer that matter). In this window you map the properties of the controls used by your custom control to the properties of any of the controls it uses. When calling one of the mapped members the user thinks he or she is setting a control’s properties but in reality is setting a constituent control’s member.

You must map all the members (properties methods and events) to the User- Control’ object (except for the custom members of course). When the user clicks the control for instance the Click event is passed to the host application as if it were generated by the custom control. Any properties methods or events that you don’t· want to handle with your own. code must be mapped to ‘the User Control object. The Click event is a typical example. There’s no action you want to take from within your control’s code when it’s clicked with the mouse. The event must be reported to the host application to handle it accordingly

To map properties follow these steps:

1. From the Public Name list select a property or an event. .
2. Click the Control drop-down list’s down arrow and select User Control The Wizard immediately selects the User Control member with the same name.
3. Map all members of the new control (except for Custom members) to the equivalent members of the User Control object.
4. Click the Next button

Setting Attributes

In this window you set the attributes of the new members (or change the attributes of some default members if you want but this isn’t recommended). Members . that have already been mapped will not appear in this list.The Wizard has declared . all new properties as variants because it can’t decipher their types from their names. Our Text Alignment and Effect properties however are integers and the Caption property is a string.

To set attributes follow these steps:

1. In the Public Name box select Text Alignment.
2. Click the Data Type drop-down arrow and select Integer.

Notice that you can set an initial value too. Set the Text Alignment property’s initial value to 4 (which displays the caption in the center of the control).

3. In the Default Value box, enter the value 4. This is the value that is displayed by default in the Properties window for the FLEX Label control. You will see  later In the chapter how to assign custom data types to your properties.
4. Repeat steps 1 through 3 for the Effect property. Set its data type to Integer and its initial value to 2 (carved).

TIP

Don’t forget to supply a short description of each property in the Description box. These descriptions are displayed below the Properties window when the user selects a property. The standard members have a description already but you must supply descriptions for your custom properties

In the Arguments box you supply the arguments of the events. This project doesn’t have any custom events but you’ll see examples of custom events later in the chapter.
Notice that the properties can have different design-time and run time properties which is the first really unique characteristic of a user control. If you think that the developer has no reason to change the alignment or the effect at run time make these properties Read-Only at run time.

Finishing

In this window you are prompted to click Finish to generate the control. Leave View Summary Report checked.
Your hard disk will spin for a few seconds and you will see the summary report. Read the summary report (which basically tells you how to proceed) and close or save the editor window.

You have created the FLEX Label control. Not that it does much (you haven’t entered the code for displaying the caption yet), but you are ready to test its behavior as a control. At this point, your control has its own Properties page and you can place it on your Forms.
There’s nothing on the screen except a Form. This is the Use Control object which for all intents and purposes is a Form. There’s also a new icon in the toolbox. This is your new control’s icon. The icon of a matrix with a pen en top is the default icon for all ActiveX controls. To change the icon design a new bitmap and assign it to the Use Control object’s Toolbox Bitmap property Let’s first make sure the control works and then we’ll customize it. The ActiveX control’s icon is disabled because you are still designing the control. The control won’t be activated while it’s being designed.

Testing Your New ActiveX Control

To test your new control you would normally have to create an OCX file start a new project and then place the . custom control on a Form. To simplify development Visual Basic lets you add a new project to the existing project. In this way. you can test

and modify the control without loading a new project each time. The next steps are standard and you will follow them every time you design an ActiveX control:

1. Choose File> Add Project, to open the New Project window.
2. Select Standard EXE A new project is added in the New Project window, with its default Form named Form 1 Change the name of the Form to Test Form and change the name of the new project to Test Project
3.Close the User Control Design window by clicking its Close button. Close the controls Code window if it’s open.

When the Test Form is selected the custom control’s icon in the toolbox is enabled. From this point on. you can use the FLEX Label control just as you use any other VB controls.

To place an instance of the Flx Label control on the test Form follow these steps:

1.Select the icon of the custom ActiveX control in the toolbox and draw the control as usual. The control is automatically named Label 3Dl following the Visual Basic convention for naming controls.
2.With the control selected on the Form locate the Effect property in the Properties window and set it to another value (it must be an integer and in the valid range of 0 to 6).

You can, however, set both the Text alignment and Effect property to any value. As long as you specify an integer value, Visual Basic accepts it. You’ll see what can be done about that shortly.

As you may have noticed already ActiveX controls have an Appearance property that by default is 3-D which gives the controls a 3-D type edge. But the Appearance .property doesn’t take effect unless the Border Style property is also set to 1-Fixed . Single. The default value of the Border Style property is 0 flat (a rather peculiar
choice considering the effect it has on the Appearance property). Open the Border- Style property and set it to 1-Fixed Single.

Now double-click the FLEX Label control on the Form to see its events. You ‘new control has its own events and you can program them just as you would program the events of any other VB control Enter the following code in the control’s Click event

Private Sub Label 3Dl_Click()
MsgBox ‘My properties are ‘ &vbCrLf &_
Caption – ‘ & Labe13Dl.Caption & Chr$(13) & _
Text Alignment – ‘ & labe13D1.TextAlignment & ChrS(13) &_
‘Effect – ‘ & Label 3Dl.Effect
End Sub

To run the control press F5 and then click the control. You will see the control’s properties displayed in a message box, as shown in Figure 16.5. ‘(At this point no caption is displayed on the “Control because we have not yet implemented the Caption custom property.)

Some of the custom
control’s properties as
they are displayed by
the control itself

You can program other events too. There’s nothing peculiar about programming the custom control. For the developer it’s a regular control. VB has done quite a lot for us. It generated a working ActiveX control. Of course we must still add the code that makes the control tick. Creating something that looks and feels like a control with point-and-click operations was quite a feat but our final goal is to design something practical and useful.

Scroll to Top