Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to save the contents of more (RTF)textboxes in one file? 1

Status
Not open for further replies.

IgnaceD

Technical User
Apr 1, 2004
6
NL
Hi,

is it possible to save the contents of more textboxes, normal and RTF, in one file?
E.g. save the contents of 2 normal textboxes and 3 RTF textboxes in one file, so this one file contains the contents of all 5 textboxes.
If this can be done, how can I retrieve the contents of the 5 textboxes separately?

Thanks in advance.
 
strongm: would this work?

a) save the text of the 1st txtbox to a string
b) Concatenate the 2nd txtbx to the string adding a "|" before you add the text
c) repeat that for each txtbox
d) save it to a file
e) parse/split the string to each txtbox the same way it was concatenated.
 
A forum member from Xtreme Visual Basic Talk (Passel) came up with this, works OK:
Assign the textboxes to variants and put the variants in a binary file.
Code:
Private Sub Save_Click()
  Dim a As Variant

  If Len(Dir$("c:\test.bin")) > 0 Then Kill "c:\test.bin"
  Open "c:\test.bin" For Binary As #1
    a = Text1.Text
    Put #1, , a
    a = Text2.Text
    Put #1, , a
    a = RichTextBox1.TextRTF
    Put #1, , a
    a = RichTextBox2.TextRTF
    Put #1, , a
    a = RichTextBox3.TextRTF
    Put #1, , a
  Close #1
End Sub

Private Sub Load_Click()
  Dim a As Variant
  Open "c:\test.bin" For Binary As #1
    Get #1, , a
    Text1.Text = a
    Get #1, , a
    Text2.Text = a
    Get #1, , a
    RichTextBox1.TextRTF = a
    Get #1, , a
    RichTextBox2.TextRTF = a
    Get #1, , a
    RichTextBox3.TextRTF = a
  Close #1

End Sub
 
I think this is a good example to demonstrate the use of VB's PropertyBag (which most people are only vaguely aware exists, and even then only for use with classes).

Here's a rewrite of the 'Xtreme' (hah!) solution. It may look a little more code, but a) it is a more complete example and b) it is much more flexible
Code:
[blue]Option Explicit
Private pbText As New PropertyBag

Private Sub Command1_Click()
    PutData
    
    ' Clear contents to show we are retrieving the data
    Text1.Text = ""
    Text2.Text = ""
    RichTextBox1.Text = ""
    RichTextBox2.Text = ""
    RichTextBox3.Text = ""
    
    GetDataFromFile
    ' Now demonstrate we can retrieve any item we like whenever we want
    MsgBox pbText.ReadProperty("text2.text")
    
End Sub

Private Sub PutData()
    Dim hFile As Long
   
    pbText.WriteProperty "text1.text", Text1.Text
    pbText.WriteProperty "text2.text", Text2.Text
    pbText.WriteProperty "RichTextBox1.textrtf", RichTextBox1.TextRTF
    pbText.WriteProperty "RichTextBox2.textrtf", RichTextBox2.TextRTF
    pbText.WriteProperty "RichTextBox3.textrtf", RichTextBox3.TextRTF
    
    hFile = freefile
    Open "c:\test.bin" For Binary Access Write As hFile
    Put hFile, , pbText.Contents
    Close hFile
End Sub

Private Sub GetDataFromFile()
    Dim hFile As Long
    Dim dummy As Variant
        
    hFile = freefile
    Open "c:\test.bin" For Binary Access Read As hFile
    Get hFile, , dummy
    Close hFile
    
    pbText.Contents = dummy
    ' Note that now we have the propertybag loaded we can retrieve contents in any order we like
    RichTextBox1.TextRTF = pbText.ReadProperty("richtextbox1.TextRTF")
    Text1.Text = pbText.ReadProperty("text1.Text")
    RichTextBox3.TextRTF = pbText.ReadProperty("richtextbox3.TextRTF")
    Text2.Text = pbText.ReadProperty("text2.Text")
    RichTextBox2.TextRTF = pbText.ReadProperty("richtextbox2.TextRTF")

End Sub
[/blue]
 
Very cool, strongm. I've only used the PropertyBag object to persist properties in ActiveX Controls. That use makes the PropertyBag object itself look a lot less elegant than it appears here. Thanks, I'll have uses for this. :)
 
>I've only used the PropertyBag object to persist properties in ActiveX Controls

I blame the limited examples available.

Propertybags are VERY useful things.

For example, they are one of the easiest ways of cloning objects (if the object is persistable)... e.g. stdpicture

I also use them as part of a method for passing data from one application to another

You can also use them to derive a new recordset from a filtered recordset in ADO, although, snce their introduction, this is better done through ADO streams.
 
Very interesting, strongm. I'll keep them higher on the radar in future.

Would you mind elaborating a bit on "passing data from one application to another"?

 
>Would you mind elaborating a bit on "passing data from one application to another"?

Probably best to start a new thread ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top