writing and reading vb6 controls to/from a file

ChrchMouse
ChrchMouse used Ask the Experts™
on
I have a vb6 application which allows the user to set various attributes for RichTextBoxes and Images (font, alignment, bounding box size, picture and scale, etc.)  All is working fine. Now I would like to save the attributes they have set, and read them back at a later time (File-Save, and File-Open).

I am at a loss as to how to accomplish this. Any ideas/sample code?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Sub LoadProperty(rtb As RichTextBox, ByVal FileName As String)
    Dim pb As PropertyBag
    Dim iFileNumber As Integer
    Dim Bytes() As Byte
   

    iFileNumber = FreeFile()
    Open FileName For Binary Access Read As #iFileNumber
    ReDim Bytes(LOF(iFileNumber)) As Byte
    Get iFileNumber, , Bytes
    Close #iFileNumber
   
    Set pb = New PropertyBag
    pb.Contents = Bytes
     
    rtb.AutoVerbMenu = pb.ReadProperty("AutoVerbMenu", False)
    rtb.BackColor = pb.ReadProperty("BackColor", vbWindowBackground)
    rtb.BulletIndent = pb.ReadProperty("BulletIndent", 0)
    rtb.Font = pb.ReadProperty("Font", Me.Font)
    rtb.HideSelection = pb.ReadProperty("HideSelection", True)
    '...
End Sub

Sub SaveProperty(rtb As RichTextBox, ByVal FileName As String)
    Dim pb As PropertyBag
    Dim iFileNumber As Integer
    Dim Bytes() As Byte
   
    Set pb = New PropertyBag
    pb.WriteProperty "AutoVerbMenu", rtb.AutoVerbMenu, False
    pb.WriteProperty "BackColor", rtb.BackColor, vbWindowBackground
    pb.WriteProperty "BulletIndent", rtb.BulletIndent, 0
    pb.WriteProperty "Font", rtb.Font, Me.Font
    pb.WriteProperty "HideSelection", rtb.HideSelection, True
    '...
   
    Bytes = pb.Contents
   
    iFileNumber = FreeFile()
    Open FileName For Binary Access Write As #iFileNumber
    Put iFileNumber, , Bytes
    Close #iFileNumber
End Sub

Private Sub Command1_Click()
    SaveProperty RichTextBox1, "C:\temp\x.dat"
    LoadProperty RichTextBox2, "C:\temp\x.dat"
End Sub

Author

Commented:
TigerZhao,

Thanks, one small problem with the code you posted. I have several RichTextBoxes, Images etc. Your code writes a single RichTextBox to a file, then uses LOF() to determine the size of Bytes to read in.  How is this accomplished with several RichTextBoxes with varying text content (.Text and/or .TextRTF)?

now you know how to get/put a RichTextBoxes property from/to a byte array
== Save ==
   'put every RichTextBoxes property to byte arrays
   pbMain.WriteProperty "RichText1", Bytes1
   pbMain.WriteProperty "RichText2", Bytes2
   ...
   'Write pbMan.Contents to file
== Load ==
    'Read pbMain.Contents from file
    Bytes1 = pbMain.ReadProperty("RichText1")
    Bytes2 = pbMain.ReadProperty("RichText2")
    ...
   'get every RichTextBoxes property from byte arrays

Author

Commented:
ok it works, I will try creating a dynamic Bytes array to handle an unlimited number of control writes/reads. Currently I am writing each control to its own file in a folder, then looping thru the folder to read them back in.  Not eloquent but it works.

Thanks for a very timely solution.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial