This method has the following syntax:
Write Property property Name value default Value
The property Name variable is the name of the property (Effect for instance) value can be a literal (such as 1 or “some sizzling effect”) but is nearly always the name of the private variable that holds the property value and default Value is the property’s default value.
Why specify both a value and a default value in the Write Property method? Visual Basic compares the value with the default setting and If that value and the default value are the same Visual Basic doesn’t save it (to speed up the process of saving and restoring property values). When you later request the property’s value with the Read Property method Visual Basic provides the same-default value
The Read Property Method
This method has the following syntax
Read Property property Name default Value’
The property Name variable is the name of the property and default Value is the value stored earlier in the Property Bag object for this property. In the User Control’s Write Properties event’s code you must call the Write Property method once for each property. Like wises in-the control’s Read Properties subroutine you must call
the Read Property method once for each property. Here are the listings for the Write Properties and Read Properties events of the FLEX Label control as generated by the Wizard.
The Controls Read Properties Procedure
Lead property values from storage
Private sub User control_ReadProperties(PropBag As PropertyBag)
Sp.~ Fo~t” Prop3ag.ReadPropertyC”Font”, Ambient.fo~t)
UserControl.BorderStyle – PropBag.ReadPropertyC”uorderStyle” 0)
UserCohtrol.BackStyle” PropEag.R ad ropertyC’BackStyle’ 1)
UserControl.Enabled – PropBag.ReadProperty(‘Enabled’ True)
UserControl.ForeColor – PropBag.ReadProperty(” ForeColor’ _
UserControl.MousePointer -PropBag.Read Property(‘MousePointer’ 0)
UserControl.OLEDropMode- PropBag.ReadProperty(OLEDropMode’ 0)
Set Picture – PropBag.ReadProperty(‘Picture’ Nothing)
m_Caption c PropBag.ReadProperty(‘Caption’ m_def_Caption)
m_Effect – PropBag.ReadProperty(‘Effect’ m_def_Effect)
m_TextAl;gnment – PropBag.ReadProperty(‘TextAlignment’ _
User Control.BackColor – PropBag.ReadProperty(‘BackColor’ _
The Control’s Write Properties Procedure
Write property values to storage
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
PropBag.WriteProperty(‘Font’, Font, Ambient.Font)
PropBag.WriteProperty(‘BorderStyle’, UserControl.BorderStyle, 0)
PropBag.Wr;tePropertY(‘~ackstyle’ UserControl.BackStyle, 1)
PropBag. Wri teProperty(‘ Ertab1ed ‘UserContro 1 .Enabl end True)
PropBag.WriteProperty(‘Picture’ Picture Nothing)
PropBag:WriteProperty(‘Caption” m_Caption ~
PropBag.WriteProperty(‘Effect’ m_Effect _
The last section of the code maps the various controls’ events to the equivalent events of the User Control object. When the user clicks on the ActiveX control Windows reports the Click event to the User Control object. As a control developer you can process the event from within the control (in which case the application that uses the control doesn’t see the Click event) you can pass it to the host application (in which case the application receives Click events for the control and the application programmer can program them), or you can do both (do something within your code and then pass them to the host application).
You pass an event to the application via the Raise Event method. The User control
object’s Click event is coded as follows:
Private Sub User Control_Click()
Raise Event Click
The code for the remaining events is nearly identical. These events call the Raise- Event statement to pass the event to the host application. If the event reports any arguments such as the Key Press event these arguments are listed in parentheses next to the event’s name. The Wizard hasn’t done anything terribly special; It simply inserted some straightforward code. With the exception of the Read property and Write Property methods, everything else should be more or less familiar to most VB programmers. The \ ActiveX control therefore is slightly more complicated than a standard project. Now it’s time to enter a few lines of code. After all we must tell our control how to align the caption and render it with a 3-D effect.
Drawing on the User Control
We now have a functional control, and it wasn’t difficult to develop. Visual Basic created the skeleton of a working control. It hooked it into the environment its icon appears in the toolbox, and we can use it in our projects just as we use any other control: it even manages its own Properties window. Now, it’s time to make this control “tick.” We must add the code that’s unique to this control: the code
that draws the caption.
the code for darwing the custom control’s visible interface is usually placed in . event. this event every time :I control m t be redrawn and you mu supply the code to redraw it. What we are going to do now is copy the Draw Caption subroutine we developed earlier in the chapter anti paste it into the custom control. Open the User Control object’s Code window and paste the Draw Caption(J sun to tine In the User Control’s Paint event handler insert Draw Caption O subroutine:
This statement in the Paint event ensures that every time the developer re sizes the control on the Form or the Form needs to be refreshed at run time the entire User Control object will be redrawn.
The Draw Caption subroutine draws on a Form object. The User Control object is practically identical to the Form object and it supports the same drawing methods. Open the User Control’s Code window locate the Draw caption() subroutine’s code and replace all instance of “User Control” s that it will draw the caption on the User Control object. The Draw Caption subroutine must also be called every time a custom property is changed to redraw the caption. The properties that affect the appearance of the caption on the User Control object are the custom properties Caption Text- Alignment, and Effect, as well as the standard properties Picture and Back Color. Locate the Property Let procedures for these properties and insert a line to invoke the User Control_Paint event. The revised Property Let procedure for the Effect property is shown next (with the new statement underlined):
Public Property let Effect(By Val New_Effect As Effects)
m_E~fect – New_Effect
Drawing on the User Control object is identical to drawing on the Form object. Custom controls that draw their visible interface and don’t rely on the standard controls, such as the FLEX Label control are called user-drawn. Later in the chapter you’ll see how to build custom controls based on constituent controls.
You should also fry to set a color property in the FLEX Label control’s Properties window. You will see the familiar color box where you select and specify hew colors. You didn’t do anything special about this property. If you look at the control’s . code you’ll see that the Wizard declared the Back Color and Fore Color properties as OLE_COLOR type. When Visual Basic sees the-OLE_COLOR type it knows how to handle the corresponding property in the Properties window.
OLE_COLOR is not a general data type you can use in normal variable declarations in Visual Basic, and it has special meaning here. It places a Color Selection dialog box that pops up every time the user tries to set a new value for a color-related property. If you set the Back Color or Fore Color property’s data type to long the user of the control would have to type a long value (for example &HOOFFOOfor green) in the Properties window.