Initializing the Control al’d Its Properties VB Help

You can use two events to maintain the control  and Init Properties. The Init Properties event is the place to assign  ital values to the various properties.

The ActiveX User Interface Wizard does it so well for us. The Initialize event can be used to enter initialization code that doesn’t involve properties. If you attempt to set a property value or do something on the control (for instance, printing the . Title on the control with the statement User Control.Print “Control”) you’ll get the following error message:

Object Required

The User Control object is being initialized It doesn’t exist yet. That’s why the following statement

User Control Print Control

works where executed from within other events but not from with in the Initialize event.

So what can you do from within this event? Very tittle You can assign initial values to the private variables of the control but you can’t access the control’s properties not ‘even the Ambient object.

The event of interest is the Init Properties event which takes place after the ‘control is created. This behavior may strike you as strange: The Initialize event takes place every time you switch between design and run time mode but the Init Properties event doesn’t follow.

TIP

The Init Properties event takes place the first time a control is created and placed on a container. After that, the role of the Init Properties event in the control’s life  is taken by the Read Properties event. If you changed the values of certain properties on the control it wouldn’t make much sense for Visual Basic to reset them to their initial values. Instead it reads them from the Property Bag when the Read Properties event is triggered.

‘In the Init Properties event you can  initialization code that controls the appearance of a “newborn” control. For instance you can date nine what happens if the user places the control on a Form by double-clicking on its Icon instead of actually drawing the control on the Form. Visual Basic places ‘an instance of the . control on the Form and it will have a certain size (which is the same for all controls). if your control contains a long default title a shape or any element you want to be entirely visible you can adjust the initial size of the control with a couple of statements such as the following:

User Control.Width – 2400
User Control.Height – 1200

When your control is placed on a Form with a double-click of its icon its initial size is 2400 by 1200 twips.

The Extender and Ambient objects are also available from within the Init Properties control because the control has been sited. You can display a title on the control in the same font as the container’s font as follows:

Set Font – Ambient.Font
UserControl.Print ‘FabControl

These two lines display the string “Fab Control” in the control’s upper left comer in the font of the container. In addition your control’s font will also be initially set to the Form’s font.

NOTE

The title “Fab Control” appears on the new instance of the control only if its Auto- Redraw property is set to True. The control is created behind the scenes and is actually displayed after all the initialization code has been executed. If the control’s Auto Redraw property is False the string will be printed on the control initially but when the control is displayed the string is not part of the persistent bitmap (discussed in Chapter 6) and will not be refreshed.

A Controls Key Properties

As you have learned the User Control object is basically a Form on which you can place other controls draw shapes display text and detect events. It even has properties such as Auto Redraw and Sale mode which make it suitable for drawing at run time. But it’s not called Form it’s called User Control. In addition it has a few properties that are unique to ActiveX controls and we are going to look at them in this section.

Can Get Focus

Set this property to True if the control can receive the focus either with the mouse or with the Tab key. A user control can get the focus if the User Control object gets the focus or if one of its constituent controls can get the focus. If the control can get the focus the Enter Focus and Exit Focus events are triggered every time the focus is moved in or out of the control. Set the Can Get Focus property of the generic control to True and then enter the following lines in the control’s Enter focus and Exit Focus events:

Private Sub User Control_EnterFocus()
User Control Back Color = vbRed
End Sub
Private Sub User Control_Exit Focus()
User Control Back Color – vb Green
End Sub

Then switch to the test Form and place instances of the new control on it’ (or one instance of the control and a couple of regular controls). Run the application and move the focus from. one control to the other. The.generic control that has the focus is filled with red, and the other control is filled with green. Custom controls with a visible user inter face should be able to receive and handle the focus. If the control contains multiple constituent controls you should also decide which one takes the focus. By default, the constituent control that was placed first-on the user control takes the focus. To set the focus to another. constituent control use the Focus method and follow these steps:

1. Go back to the generic control, and place two Command Buttons on the User Control. Don’t change their names. .
2. Add the following lines to move the focus to the Command2 button in the Enter Focus event

Private Sub User Control_Enter Focus()
UserControl.BacK Color – vb Red
Command2.Set Focus
End Sub

3. Switch to the test From delete all controls on the Form, and place an instance of the new control (large enough to display both buttons) and another Command Button on the Form.
4. Run the project and check out how the focus is moved from one control to the other. Notice that when the user control takes the focus it passes it to the Command 2 button. You can’t move the focus to the Command 1 button with  the Tab key. The user control is a single entity and it gets the focus once

Control Container

If this property is set to True the user control can become a container for other controls. Normally the control splaced on a container are grouped with the container and they all move together. When you re-position the container control on the Form all the controls contained in it are moved along.

By default a user control is not a container. In other words it is possible to draw a Command Button that lies half on the user control and half outside. To change this behavior set the ControI Container property to True.

Alienable

If the Alignable property is set to True the user control has an Align
property at design time. The Align property determines whether and how the control is aligned on the Form. The Align property’s settings are shown in Table 16.3.

TIP 

The Align property is not available at- design time if the user control’s Alignable property is False. Set the Alignable property to True for tool bar like controls which must always be aligned with the edges of the container even when the container is re sized .

VALUE DESCRIPTION

Invisible At Run time

Some controls the Tuner being the most typical example are.invisible at run time.If your user control does not have a user interface and need not appear on the Form set its Invisible All centime property to True.

Tool box Bitmap

Use this property to display a BMP file in the toolbox in place .of the ActiveX Control generic icon. The Tool box Bitmap property’s value is a BMP file’s path name but the bitmap is stored in the control and distributed with it

AccessKeys

You use the Access Keys property to specify which keys will act as
hot keys for the control. If you want the user to move the focus instantly to your control by pressing a hot-key combination (Alt+Key) assign the key value to the Access Keys property. Follow these steps:

1. Assign the-value “A” to the user control’s Access Keys property (without the quotes).
2. Switch to the test Form and run it. Notice that you can switch the focus to the user control by pressing Alt+A,
3. Now stop the application, return to the user control, and open the Access- Key Press event. This event is invoked every time the access key is pressed. Enter the following lines to display the key’s ASCII value

Private Sub User Control Access Key Press (Key Ascii As Integer)
Debug.Print ·Access key pressed & KeyAscii
End Sub

To move the focus to a specific constituent control or to perform some action when the focus is moved to the user control enter the appropriate code in the Access Key Press event. Let’s add. access keys to the two Command buttons of the last example. Follow these steps:

L Switch to the User control object and add two Command buttons, Command 1 and Command 2  as shown in Figure 16.15. (If you have followed the steps of the examples in the section I Can Get Focus” the two Command buttons already on the Form.)

When the focus is moved to
a custom control, it is actually
moved to one of the
constituent controls.

FIGURE 16.15:

FIGURE 16.15:

2. Switch to the test Form, and place an instance of the new control, another button or another VB control on the Form.
3. Run the test project and move the focus back and forth Notice the following

When you move the focus to the custom control with the mouse, the
first Command button takes the focus.

When you move the focus to the custom control with the Tab key, the ASCII value of the shortcut key is displayed in the Immediate Execution window

4. Now switch back to the User Control object and assign shortcut keys to the two Command buttons. Change their caption properties to Command&l and Command 2 so that the keys 1 and 2 will become their access keys.
5. Run the test project and experiment with moving the focus among the various controls on the Form

You can use a hot-key combination to access not only the user control but also the individual constituent components on it. To activate a specific constituent control every time the focus is moved to a custom control, insert the Set Focus method in the User Control_Access Key Press subroutine. If the custom control contains a text box as a constituent control, you can set the focus to this control each time the user control receives the focus with the following subroutine:

Private Sub User Control Access Key Press (Key Ascii As Integer)
Text. Set Focus
End Sub

VB6 at Work: The Alarm ActiveX Control

The next example demonstrates an ActiveX control that contains all three types of members-properties methods and events. It’s a simple alarm that can be set to go off at a certain time and when it times out it triggers a Time Out event. Moreover while the timer is ticking the control updates a display showing the time elapsed since the timer started (the property Count Down must be False) or the time left before the alarm goes off (the property Count Down must be True). Figure 16.16 shows the test Form for the Alarm control. The first instance of the Alarm control (Process A) counts the time since the Tuner started and the second instance (process B) counts down the time left before the alarm goes off.

The test Form for the Alarm
custom control

FIGURE 16.16:

FIGURE 16.16:

TIP

When you load the Alarm project for the first time Visual Basic will issuea warning to the effect that it couldn’t load the Alarm control This simply means that the Alarm control hasn’t been Installe don your system yet, and the test Form that uses it can’t be loaded. Continue loading the project and then open the test Form. The two instances of the Alarm control will be replaced by two Picture Box controls. Delete these two controls and place two instances of the Alarm control the test Form,as shown in Figure16.16.

Posted on November 6, 2015 in Building AdiveX Controls

Share the Story

Back to Top