setting and Reading PrOperty Values VB Help

Next you see the number of subroutines two for each property.

The caption property Procedures

Public Property Get Caption() As String
Caption – m_Caption
End Property

Public Property let Caption(ByVal New_Caption As String)
nLCaption – New_Caption
PropertyChanged ‘Caption’
End Property

Each property is defined by two Public procedures:
• Property Let
• Property Get

The Property Let procedure is invoked every time the property is changed, either via the Properties window (at design time) or via code (at run time). The code that’s executed when C) property changes value consists of two lines. The first line gets the value supplied by the procedure’s argument (which is the new value of the
property) and assigns it to the private variable that represents the property in the control. There it of the code sees only the m_Caption local property not the actual property. The second line notifies Visual Basic that the property has changed value. The Property Changed method is important and must be included in the Property
Let procedure because this is how Visual Basic saves any-changes made to the property at design time so that it will take effect at run time.

The Property Get procedure is invoked every time the program recalls the value of the property. This procedure reads the value of the m_Caption private variable and assigns it to the Caption property. There must be a Property Let and a Property Get procedure for each property and they must include the lines shown here  They represent the minimum functionality of the mechanism for setting and reading property values.

Of course you can add validation code here too. The Text Alignment property’s value must be in the range 0 through 9. As is the custom control allows the user to user any value in the Properties window for this property.Let’s add some validation code in the Property Let procedure of the Text Alignment property. The validation code is simple: It rejects any values that are smaller than 0 or larger than 8

validation for the Property Let produce

Public Property Let Text Alignment (ByVaJ New_Text Alignment,As Integer) If mL Text Alignment >-0 And m_Text Alignment<-S Then
m_Text Alignment –New_Text Alignment
Property Changed”Text Alignment”
MsgBox “Invalid value for this property’
End If
End Property

The If statement tests the validity of the supplied value and if the new value is outside the valid range the attempt to set the property is rejected. Modify the Property Let procedure according to the previous listing and then switch to the test Form of the test project. Select the FLEX Label control on the Form open the Properties window and set the Text Alignment property to an -invalid value (13 or 1000 for example). As soon as you attempt to change the property’s value to an invalid setting, the control displays the warning and rejects the changes. You may be wondering now How can make this property display its valid settings only in a drop-down List Box control like other Visual Basic controls? It is possible of course but it takes a bit of code and you’ll see how shortly.

After the Property Let and Property Get procedures for all properties of the control comes some initialization code.

The initialization Code

Initialize Properties for User Control
Private Sub UserControl_InitProperties()
Set Font – Ambient.Font
m_Caption – m_def_Caption
m_Effect – m_def_Effect
m.TextAlignment. ~_def_TextAlignment
UserControl.BorderStyle – 1
UserControl.BackStyle – 1
End Sub

The statements of the in it properties () subroutine assign initial values to the private variables that represent ion  the con properties. The constants m_def Caption m_”-Text alignment  and 111 _def .Effect wen> defined earlier in the program. When . this control .Form, Visual Basic looks up the values cf the m_Caption, m_Text Alignment, and l: variables and uses them to assign the proper values to entries of the Properties window.

saving and retrieving properties values

Now come two interesting subroutines:
• Read Properties
•Write Properties

When you set come properties through the Properties window the new values must be saver) some where. The reason? So that the ontrol won’t forget them. An application may (and usually does) change the values of certain properties at run time. But when the application stops and you’re back in design mode the properties changed at run time must be reset to their values before the application is started-not to their default values but to whatever values you assigned to them at design time.

Visual Basic provides a special object for storing all property values: the Property- Bag. The Property Bag object exposes two methods one for saving a property’s value and one for reading a property’s value. You the control developer need not know anything about how the values are stored Visual Basic stores them and when you request their values it furnishes them. The two methods are properly named. Write- Property and Read Property.

Posted on November 6, 2015 in Building AdiveX Controls

Share the Story

Back to Top