Saving Properties to the PropertyBag

See Also

When the end user of an ActiveX document views a document, there may be several text boxes that she or he will fill out as part of your application. If it's possible that the user will return repeatedly to the document, you may wish to save some time by saving the data to the PropertyBag.

In the following example, the PropertyChanged statement is inserted into the Change event of the TextBox (txtFirstDoc) control. This causes Internet Explorer to prompt the user to save changes. If the user responds positively, the code writes the property value to the PropertyBag.

For More Information   To learn more about the PropertyBag, see "Saving the Properties of Your Control" in "Building ActiveX Controls."

Note   This topic is part of a series that walks you through creating a sample ActiveX control. It begins with the topic Creating an ActiveX Document.

To write and read a property to the PropertyBag

  1. In the Project Explorer window, double-click the FirstDoc icon to bring its designer forward.

  2. Double-click the TextBox (txtFirstDoc) control to view its code.

  3. In the Code window, from the drop-down list of events for txtFirstDoc, click Change. Add the PropertyChanged statement, as shown:
    Private Sub txtFirstDoc_Change()
       PropertyChanged
    End Sub
    
  4. Add the following code to the UserDocument object's WriteProperties event:
    Private Sub UserDocument_WriteProperties _
    (PropBag As VB.PropertyBag)
       PropBag.WriteProperty "StrDocProp", _
       txtFirstDoc.Text, "Hello"
       Debug.Print "WriteProperties"
    End Sub
    
  5. The code in step 4 saves the value of the Text property of the TextBox control to the PropertyBag. To retrieve the value, add the following code to the ReadProperties event:
    Private Sub UserDocument_ReadProperties _
    (PropBag As VB.PropertyBag)
       txtFirstDoc.Text = _
       PropBag.ReadProperty("StrDocProp", _ 
       "Hello")
       Debug.Print "ReadProperties"
    End Sub
    

Running the Project

Now that the property can be written to and read from the PropertyBag, run the project to test it.

  1. Press f5 to run the project.

  2. Click the Address box and type the path of the FirstDoc.vbd.

  3. Type some distinctive text, such as your name, into the blank TextBox control.

  4. On the File menu, click Close. Internet Explorer will present a dialog box asking if you would like to save the your changes. Click Yes.

  5. Run Internet Explorer again.

  6. Click the Address box and type the path of the FirstDoc.vbd. The text you typed will be present in the TextBox control.

Using the PropBag in conjunction with the WriteProperties and ReadProperties events, you can easily persist data for your object.

Step by Step

This topic is part of a series that walks you through creating a sample ActiveX document.

To See
Go to the next step Adding a Menu to the ActXDoc Project
Start from the beginning Creating an ActiveX Document