Recalling UserControl Properties

Standard controls have properties you can set using the object browser.  The next time you bring up your VB program for editing you need to be able to recall the state of these settings.  Example #3 on this page  shows how to use a PropertyBag to remember these settings.

 

             

  BagMan PropertyBag Example

Storage to the Bag

Save to the Bag – Fill up the object to be saved to disk Stuff it in the Bag – The actual file operation to put on disk

 Private Sub cmdSave_Click()

  Dim objBag As New PropertyBag

    With objBag
        .WriteProperty "Str", "A string"
        .WriteProperty "Num", 666
        .WriteProperty "Bool", False
        .WriteProperty "Mascot", SuperBagMan.Picture

        SaveBagContents .Contents, App.Path & "\Things.bag"
    End With
   
    Set objBag = Nothing

End Sub  

                                                                                                               

Private Sub SaveBagContents(Contents As Variant, FilePath As String)

  Dim FileNum As Integer

    FileNum = FileSystem.FreeFile()

    Open FilePath For Binary As FileNum
        Put #FileNum, , Contents
    Close FileNum

End Sub

 

Retrieval from the Bag

Recall the Bag contents from disk Dump the Bag – The actual file operation to get contents from disk

 Private Sub cmdLoadBag_Click()

    Dim objBag As New PropertyBag
    Dim TestInt As Integer
   
    BagReadOut.Text = ""

    With objBag
        .Contents = LoadBagContents(App.Path & "\Things.bag")
              
        Display "Str = " & .ReadProperty("Str", "[No value]")
        Display "Num = " & .ReadProperty("Num", "[No value]")
        Display "Bool = " & .ReadProperty("Bool", "[No value]")
        Display "———————————————–"
        Display "The next value is purposely left off the storage list and thus it uses the default value specified in the .ReadProperty statement"
        Display "NotInBag = " & .ReadProperty("NotInBag", "[No value]")
       
        Set Image1.Picture = .ReadProperty("Mascot", "[No value]")
       
        TestInt = CInt(.ReadProperty("Num", "[No value]"))
       
    End With
   
    Set objBag = Nothing

End Sub

                                                                                                      

Private Function LoadBagContents(FilePath As String) As Variant

  Dim FileNum As Integer
  Dim tempContents As Variant

    FileNum = FileSystem.FreeFile()

    Open FilePath For Binary As FileNum
        Get #FileNum, , tempContents
    Close FileNum

    LoadBagContents = tempContents

 

Categories: CodingVisual-Basic

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *