PDA

Click to See Complete Forum and Search --> : how to update properties...


slyvette
Dec 17th, 2001, 04:25 PM
I'm somewhat new to designing active x controls so my question is probably somewhat simple for those of you out there. Anyway here goes, lets say you have the following code in your usercontrol code area....


Public List_Title as string
Public List_Number as integer


or whatever, so when you test the user control and place it on a form the properties "List_Title" and "List_Number" show up in the properties dialog box, but my question is that how do you know when the user changes them. For instance I placed a control of mine on a form in my project and changed the "List_Title" property to lets say "This is a test", but how do I make lets say a label in my control change it's caption at that very instance. I can run a sub like Public Sub Update() and have the code to change the label's caption but this is kind of tedious making the programmer have to call Mycontrol.update everytime they change a property. Anybody understand what I'm talking about? I appreciate anyone and everyones help!!!!:confused:

Ed Lampman
Dec 17th, 2001, 07:40 PM
This explains some of the properties you can use to persist property settings:

Persisting a Property
You can mark a property as persistable by implementing the PropertyChanged method in a Property Let or Property Set procedure, as in the following example:

Private mInterestRate As Single
Public Property Let InterestRate(newRate As Single)
mInterestRate = newRate
PropertyChanged "InterestRate"
End Sub

Calling the PropertyChanged method marks the InterestRate property as dirty. The WriteProperties event will fire when the class is terminated if any property in the class has called PropertyChanged.

The ReadProperties, WriteProperties and InitProperties Events
The WriteProperties event procedure is used when a class is terminating to write the current property values to a private storage known as a PropertyBag object. The following code is used to save a property to the built-in PropertyBag:

Private Sub Class_WriteProperties(PropBag As PropertyBag)
PropBag.WriteProperty "InterestRate", mInterestRate, conDefaultRate
End Sub

The Property Bag's WriteProperty method in the above code takes three arguments: the name of the property to save ("InterestRate"), the value to save (mInterestRate), and a default value (DefaultRate). If the new value matches the constant conDefaultRate, the WriteProperty method doesn't have to write out the value.

The ReadProperties event is fired when a class is initialized – but only if the PropertyBag has something in it. If the PropertyBag is empty, the InitProperties event will be fired instead. Code in the ReadProperties and InitProperties events is used to set the initial property values:

Private Sub Class_ReadProperties(PropBag As PropertyBag)
mInterestRate = PropBag.ReadProperty("InterestRate", conDefaultRate)
End Sub
Private Sub Class_InitProperties ()
mInterestRate = conDefaultRate
End Sub

Note that the constant conDefaultRate is used in both procedures to provide a default value. By using a constant to define the default values you eliminate the risk of accidentally defining different default values in different procedures.

When you first create an instance of the Loan class using the New keyword, the InitProperties event will be fired; once the class has been persisted and is then recreated, the ReadProperties event will be fired instead.