|
-
Oct 14th, 2005, 11:21 PM
#1
Classic VB - How do the ReadProperties and WriteProperties work? (PropertyBags)
PropertyBags are what are used to store the control properties. Each control has one propertybag and this includes UserControls. Contents of the propertybag are shown to the developer only in the development time: so those are what you see in the Properties Window which is often placed in the right side of the screen (as it is the default position for it).
ReadProperties
The "funny" thing is that you don't need to explicitly define the properties anywhere else: they are automagically detected by Visual Basic from ReadProperties and WriteProperties. The use is simple:
VB Code:
Private Const m_def_AutoRedraw As Boolean = False
Dim m_AutoRedraw As Boolean
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
m_AutoRedraw = PropBag.ReadProperty("AutoRedraw", m_def_AutoRedraw)
Set UserControl.Font = PropBag.ReadProperty("Font", UserControl.Extender.Container.Font)
UserControl.AutoRedraw = m_AutoRedraw
End Sub
As you can see, in this example we have two different kinds of properties: a variable and an object. The object is set directly to the control and the effect is immediate. The AutoRedraw however uses a temporary variable and a constant value. This is close to the default way things are done when creating an automated usercontrol and I see no reason not to do this in other way. Thus: constants in the code are "easy to change default settings". The font by default is copied from the container of the usercontrol. This is how VB controls behave by default.
Also noticeable is that the AutoRedraw setting is also set afterwards to effect: basically you need to do the redrawing of the control in ReadProperties, otherwise your control would appear odd-looking if you draw it with your own code.
WriteProperties
WriteProperties is more straightforward:
VB Code:
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
PropBag.WriteProperty "AutoRedraw", m_AutoRedraw, m_def_AutoRedraw
PropBag.WriteProperty "Font", UserControl.Font, UserControl.Extender.Container.Font
End Sub
It is only used to save the settings. First comes the property name, then the value to be saved and last the default value, which is optional.
Some notes about PropertyBags
Apparently strings are always stored in ANSI format, so you lose Unicode characters if you try to save those or use such as a default value. You can of course skip the problem by using StrConv, though it might cause problems. This far I've decided not to save Unicode at all. This isn't a critical issue though, often Unicode supported controls are filled afterwards.
Comments and feedback
If you're not adding any new information in to the reply, send me a private message instead
Last edited by si_the_geek; Oct 18th, 2005 at 12:10 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|