Building a Generic Control VB Help

In the first section of this chapter, you learned how to b Wilde ActivX controls with the ActiveX User Interface Wizard. Eve though the Wizard to  care of man. details for you and constructed a functional control you had to  ow a  about a control’s modes of operation in order to attach the desired type of fl nationality. The Wizard creates the skeleton of the control but you have to flesh it out There are a few importer t topics I didn’t cover earlier and we will look .it item  in this section including how to initialize an terminate controls and  ow to use unique properties method and ‘events. To simplify the discussion are not going to build another control Instead we’ll use a generic control. This tit. we will not use the Wizard but will implement all e Properties  and methods manually so that you’ll get some experience in editing controls.

Creating a Generic Control

Let’s start by creating a generic control. Follow the these step.

1. Choose File» New Project to open the New Project window.
2. Click the.Activex Control icon. Visual Basic creates a new project that contains User Control named User Control. This is the control’s Form on which the visible interface will be built.
3. Choose File > Add Project to add a Standard EXE project. Visual Basic creates a new project with a single . You’ll use Form to test the control. (Form is frequently called the “test Form” and the project it belongs to is called the “test project.”) What you have on your screen now is a generic control. .
4. This is a good point at which to rename and save your project’s files. For this generic project, use the default names.
5. Close the control’s design window to enable the control’s in the toolbox.
6. Place an instance of the new control on the Form. The newly created Form doesn’t have a background color or a border that will distinguish it from the Form on which it lies
7. With the control selected open the Properties window.

By default a User Control object has the following properties:

These properties are actually provided by the container. The Left property is determined by the container and has meaning only in the context of a container. Likewise the Tab Index and Tab Stop properties aren’t managed by the control itself because the control doesn’t know what other controls exist on the Form: Only the Form does, and therefore the Form must maintain these properties for its controls. rest a few of these properties. Assign the value ”My generic control” to the tool Tip text property. Run the application and then rest the pointer for a second over the control. The string you entered is displayed in a Tool lip box. In a similar manner you can test the Tag property by assigning a string to it or you can test the Index property by creating multiple instances of the same control on the Form with the same name and a different Index value. There’s not a single line of code you should add to the control to implement these properties.

Adding a Property

Let’s add.a property to our generic control. We’ll call it Title and we’ll store its .value internally to the m_Title private property. Select User Control in the Project window and in the Code window insert the following declaration:

Private m_title As String
and then the procedures:
Public Property Get Title  As String
title – ~Title ‘
End Property
Public property Let Title (By Val vNew Value As String)
m_Title – vNew value ‘
End Property

Close the User Control design window and the Code window switch to the test Form select the new control and look up its new property In the Properties window. The mere presence of the Let and Get procedures is all that Visual Basic needs to add a property to the control. Enter a new value in the Title property’s box (for  example, Control Title).As expected, the title won’t appear and the control. We must also write a few lines of code to display the title. Switch back to the User Control window double-click it to open the Code window and in the Paint event enter the following:

Private Sub Use rControl_Paint()
User Control.CurrentX – 0
User Control.CurrentY – 0
User Corntrol.Print m_Title
End Sub

TIP

The first two statements aren’t really needed to display something at the control’s upper left corner but you must set them accordingly if you want to display something elsewhere on the control

Switch back to the test Form. If you have followed any of the previous suggestion and experimented with the custom control on the Form, delete all controls on the test Form. ‘ Add an instance of the custom control (it will automatically be named User Control unless you have changed the name of the User Control object) and then assign a value to the The Property. Set the title to My Generic Control for instance. The title won’t the moment you enter it because the Paint event isn’t triggered” when a property changes values. you must re size the control to force a Paint event and display the title. If you don’t like the font change the User Control’s Font property (our control doesn’t have a Font property yet) Every time a new property is set the Property Let procedure is invoked. You  must therefore call the Paint method from within the Property Let procedure so that the title is displayed as soon as it’s entered. Switch back to the User Control and add the following line to the Property Let Title procedure: User Control Paint Your VB window should like the one shown in Figure 16.1’4. Now assign a value to the Title property and watch the string appear on the control.

The VB window during the
first steps to the design of
the generic control

Now, press FS-to run the location. The title won’t appear on the control It was there at design time, but, it disappeared at run time. You probably want to stop the application and look up the value of the Title property to make sure it still contains its value But you’re in for a surprise. The Title property’s value is a blank string. It wasn’t your fault, so don’t repeat the process. Any properties set at design time lose their value at run time. It’s a strange behavior but this is how

controls work Your experience with Visual Basic tells you that any properties set at design time i keep their values at run time. To make this happen, you must first save the property values to the Property Bag. What’s not so easy to guess is when an fiction must take place. Let’s explore the control’s life cycle.

The life of a Control

Let’s experiment a little with the control’s key events. Follow these steps:

1. Switch to the User Control window and double-click it to open the Code window.
2. Locate the Initialize Init Properties and Terminate events in the drop-down list on the right. In each enter the Debug.Print statement followed by the event’s name as shown here

Private Sub User Control Initialize()
Debug.Print “initializing control”
End Sub
Private Sub User Control Init Properties()
Debug.Print initializing properties
End Sub  Private Sub User Control_Terminate()
Debug.Print “terminating control”
End Sub

3.Close the User Control window and return to the test Form. Place an instance of the new control on the Form and watch the Immediate Execution Window. The following messages are displayed:

initializing control
initializing properties

When you place a-control on a Form it is initialized and then its properties are initialized.

NOTE

The Print statements executed when you switched from .the User Control to the test Form. Even though you are not running·the application the code is running To understand this behavior you must put on your ActiveX designer’S hat. When You place a regular ActiveX control, such as a Text Box control on a Form some code is executed. This is what you just witnessed. ActiveX controls are always in. run mode regardless of whether the Form they belong to is running. How else would the control’s appearance change every time you set a different font or a background color?

4. Now set the Title property and run the test application. Two new messages appear in the Immediate Execution window (clear the current contents of the Immediate Execution window first):

terminating control
initaliznig control

The control that was on the From at design time was terminated and a new one was initialized. All its properties were initialized to their default values and the default value for the Title property was a blank string. That’s why it disappeared: If you stop the application now the following message appears once again in the Debug window:

initializing control

This time the run time instance of the control is terminated and another design time instance of the control is initialized. Each time you switch from design-time mode to run time mode the instance(s) of the control is(are) terminated and a new one is created.

During this transition the properties must be saved somehow. To do so follow these steps:

1. Switch back to the User Control Code window and enter a Print statement in the Read Properties and Write Properties events.
2. Switch back to the test project set the Title property run the application again and you will see the following sequence of events:

writing properties
terminating control
initializing control
reading properties
Visual Basic saves the values of the properties in the Properties window and terminates the design-time instance of the control. It then initializes the run time instance of the control and reads the same properties. This is the life cycle of an ActiveX control

NOTE 

But if Visual Basic knows which values to save why can’t it remember them until the new instance of the control is created? Why does Visual Basic save the property values and then read them again? It seems so simple but notice the Terminate event between the writing and reading of the property values. In between these two instances the control ceases to exist Even if this behavior doesn’t make sense right now this is how controls behave.

Let’s summarize the events that mark the life of a control. To execute some code at each of these events place it in the corresponding event handler.

Key Events in a Controls Life time

When you place an instance of a control on the Form the following events take place:
Initialize initializes the design-time instance of the control.

Ini Properties assigns initial values to the properties

When you switch.from design time to run time the following.events take place:

Write Properties saves the properties listed in the Properties window.

Terminate terminates the design-time instance of the control.

Initialize initializes a new run time instance of the control.

Read Properties reads the saved properties.

When you switch from run time to design time the following events take place:

Initialize initializes the design-time instance of the control.

Read Properties reads the values from the Properties window and assigns them to the corresponding properties.

TIP

When you switch from run time to design-time mode no Write Properties servent takes place Expected Visual Basic doesn’t save the properties that changed at run time the ActiveX control to the properties set in the Properties window at design time In other words, changes made to the control’s properties at design time are valid at run time too. The opposite isn’t true changes made at run time are re set when you switch back to design mode.

To maintain the values of the properties when the control switches from design to run time you must add a few lines of code in the Read Properties and Write Properties events. We have looked at how property values are written to and read from the Property Bag object so here’s the code for the Title property:

Private Sub User Control Write Properties (Prop Bag As Property Bag)
Debug.Print “writing properties”
Prop Bag Write Property ‘Title” m_Title. “Control Title’
End Sub

Private Sub User Control’_Read Properties (Prop Bag As Property Bag)
Debug. Print “reading properties”
Title – Prop Bag. Read Property “Title”  ‘Control Title’)
End Sub

Posted on November 6, 2015 in Building AdiveX Controls

Share the Story

Back to Top