ActiveX Control Tutorial - Part 4, Page 3
Next up on my nerdy curriculum, we have the ReadProperties event. Excited yet? Not exactly an inspiring name, I grant you — but please bear with it.
Ironically, despite doing exactly the opposite of the WriteProperties method we just uncovered, ReadProperties is surprisingly similar in usage.
Hmm, I could cunningly ask you to reread the previous section backwards or something, but I don't think the Ed would like that. In fact, he'd have me off the site before you can say antidis...
<Fading 'Arghhh!' sound as author Karl Moore is thrown out the nearest window by an evil, hooded figure bearing a suspicious resemblance to the Ed. Can't be certain it was him though. Recognised nothing major, just silly little similarities. Like the thick cigar sticking out of the balaclava. Oh, and the one-legged limp. And the 'I am the Editor' tee shirt he wore. Strange coincidence, that. Ho-humm. Minutes later, a slightly-bruised Karlos sneaks back indoors to continue the tutorial>
Now, let's pretend one groovy son-of-a-developer used your Super Cool Text Box control yesterday to start developing his Parcel Tracking program. He's now returned to continue work and opens the Visual Basic project.
What happens? As the form bearing your creation opens, Visual Basic says "Hey diddly dandy, I'm loading you up here" and fires the control's ReadProperties event:
Private Sub UserControl_ReadProperties(PropBag _As PropertyBag)
Here, you're passed a property bag that potentially contains a handful of property values, such as those previously saved. It's at this stage you can read those properties. Let's peek at an example:
m_AcceptType = PropBag.ReadProperty("AcceptType", _m_def_AcceptType)
Here, we're passing ReadProperty the name of the item to find within the bag ('AcceptType') and a default value of m_def_AcceptType.
If, after a quick rummage around, the ReadProperty method manages to find a value for our item, it's passed back otherwise our default value is returned.
Either way our m_AcceptType variable, which ultimately handles the AcceptType property is set appropriately.
Let's look at another supercool example:
txtTextBox.Text = PropBag.ReadProperty("Text", "")
Here, we're directly setting the Text property of our Text Box equal to the "Text" value in the property bag. If no value exists in the bag, ReadProperty passes back our default of "" an empty string.
So you read values from the property bag in other words, retrieve your control properties - using this template:
Destination = PropBag.ReadProperty(PropertyName, Default)
In brief, the ReadProperties event fires when your control is being loaded. It passes you a possible bagful of properties and allows you to pick out the items you want and set variables or object properties accordingly.
Do you understand how both the reading and writing of properties fit together? Take a closer look at your project. Do you understand what the wizard has done?
Why don't you practice these techniques by adding a 'play around' property, such as Text2? You can access it via a property Let and Get routine, then use the WriteProperty and ReadProperty skills you've just learned to 'permanently' store the information alongside your control.
Top Tip: Here's a great way to chat up geekesses. Instead of saying you used the 'ReadProperty' and 'WriteProperty' methods to save stuff, simply mention how you 'persisted' the values. Trust me, you'll sound much more of an anorak and the grrls just love it!
But hold on one lil' minute, missy we've still not figured out the meaning of that mysterious 'PropertyChanged' statement our wizard threw inside every Let procedure - remember? Grab that cigar Sam; it's time to investigate...
Page 3 of 8