Results 1 to 6 of 6

Thread: ReadProperties / WriteProperties / InitProperties - - - - HELP!

  1. #1
    Guest
    Can anyone please explain how to use these methods to set the properties of my usercontrol.

    I have tried coding it in several ways, but i cannot figure out how to maintain the same value in the properties pane between design-time test runs, the values always revert to the defaults!

    And if I add more than 1 of these controls on 1 form, the second and third.... instances dont even have the default properties!!!

    this is driving my nuts.

    Please help.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    The InitProperties event is only raised once; When the control is added to a form.
    You must store the property values in the PropertyBag that is passed to the ReadProperties and WriteProperties events.
    When a property changes call the PropertyChanged method and pass the name (string) of the property.
    Here's a simple example of a user control that has a Caption property:
    Code:
    Dim m_Caption As String
    
    Public Property Get Caption() As String
        Caption = m_Caption
    End Property
    
    Public Property Let Caption(ByVal New_Caption As String)
        m_Caption = New_Caption
        UserControl.Cls
        UserControl.Print New_Caption
        PropertyChanged "Caption"
    End Property
    
    'Initialize Properties for User Control
    Private Sub UserControl_InitProperties()
        'set the Caption property to the extender name
        Me.Caption = Extender.Name
    End Sub
    
    'Load property values from storage
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
        Me.Caption = PropBag.ReadProperty("Caption", "")
    End Sub
    
    'Write property values to storage
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
        Call PropBag.WriteProperty("Caption", m_Caption, "")
    End Sub
    Good luck!

  3. #3
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Canada
    Posts
    264
    Sorry, didn't really understand you ... lost you there somewhere, can you try explaining it again (maybe with an example) ?
    In the beginning the universe was created. This has made a lot of people very angry and is generally regarded as a bad idea.

    - Douglas Adams
    The Hitchhiker's Guide to the Galaxy

  4. #4
    Guest
    oh dear, I think I am drowning here!

    What is the Extender? Do I need it?

    I dont want to change default values at design-test time, I want to type 200 into the MAX property in the properties pane...I want the control to know this is what I typed...no matter what happens to this MAX value during execution I want the same value (200) to still be there when I get back to the properties pane.

    Its times like this that I really hate Microsoft for making things unnecessarily complicated!

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    The Extender object has some properties that are given to the control from it's extender (usually the Form if you're only using the control in VB).
    These properties are forexample the Top, Left, Width and Height properties.
    These properties are not determent by the control itself but rather from the form that host the control.
    The Name property is also an Extender property. The sample code I submitted in my the previous post simply sets the Caption property to the Control (or Extender) name.
    Just like when you add a Label to a Form the default Caption is Label1 (or Label2 and so on).

    For a MAX property you can do the same as I did with the Caption property but you should have an other default value.
    This default value can be stored in a constant.
    Just like a scrollbar sets the Max property to 32767 as default.

    The changes you make to the property during design time is saved in the PropertyBag provided by the WriteProperties event.

    The following code sample assumes that you store the Max property value in an Integer. You must change that if you store it as any other data type.
    Code:
    Private m_Max As Integer
    Private Const Def_Max = 32767
    
    Public Property Get Max() As Integer
        Max = m_Max
    End Property
    
    Public Property Let Max(iNewValue As Integer)
        m_Max = Max
        'do whatever
        'and notify the UserControl that the Property
        'value has changed
        PropertyChanged "Max"
    End Property
    
    Private Sub UserControl_InitProperties()
        'init the Max property
        'This is only called once; when the control
        'is added to a Form
        m_Max = Def_Max
    End Sub
    
    'Load property values from storage
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
        m_Max = PropBag.ReadProperty("Max", Def_Max)
    End Sub
    
    'Write property values to storage
    Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
        Call PropBag.WriteProperty("Max", m_Max, Def_Max)
    End Sub
    The last argument in the ReadProperty and WriteProperty methods of the PropertyBag object is the default value.
    This must be added because if no changes are made to the property the property is never stored in the PropertyBag and the ReadProperty method will fail.

    Best regards


  6. #6
    Guest


    Thanks Joacim, it was the PropertyChanged thing that I had missed.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width