|
-
Jul 18th, 2005, 07:02 AM
#1
Thread Starter
Member
Writing Listbox Contents To A File
Hey,
I have a listbox on a form, and want to print the entire contents, row by row, out to a file.
So for instance, a listbox could have the following values:
Apple
Banana
Pear
Plum
Orange
I want to write each line out to the file such as the following:
Write #1, Line1, Line2, Line3, Line4, Line5 (For the entire contents of the listbox).
I understand I would probably use a For Loop, For a = 1 to List1.Listcount or whatever, or could use an alternative loop.
PS... Sorry about the overt simplification of this post, I thought it would be better to paraphrase it for clarification.
Thanks,
Snowblind.
Last edited by Snowblind; Jul 18th, 2005 at 07:07 AM.
-
Jul 18th, 2005, 07:11 AM
#2
Re: Writing Listbox Contents To A File
yes loop through
VB Code:
Open "c:\path\filename.txt" for output as #1
For x = 0 to list1.listcount - 1
print #1, list1.list(x)
next
Close #1
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jul 18th, 2005, 07:14 AM
#3
Thread Starter
Member
Re: Writing Listbox Contents To A File
Many thanks for your quick reply,
Regards,
Snowblind.
-
Jul 18th, 2005, 07:20 AM
#4
Re: Writing Listbox Contents To A File
To take what [LGS]Static posted, one step further, you can use this to both save, and then retrieve your listbox contents.
VB Code:
Private Sub SaveLoadListbox(plstLB As ListBox, pstrFileName As String, _
pstrSaveOrLoad As String)
Dim strListItems As String
Dim i As Long
Select Case pstrSaveOrLoad
Case "save"
Open pstrFileName For Output As #1
For i = 0 To plstLB.ListCount - 1
Print #1, plstLB.List(plstLB.ListIndex)
Next
Close #1
Case "load"
plstLB.Clear
Open pstrFileName For Input As #1
While Not EOF(1)
Line Input #1, strListItems
plstLB.AddItem strListItems
Wend
Close #1
End Select
End Sub
Private Sub cmdLoad_Click()
Call SaveLoadListbox(List1, "c:\path\filename.txt", "load")
End Sub
Private Sub cmdSave_Click()
Call SaveLoadListbox(List1, "c:\path\filename.txt", "save")
End Sub
-
Jul 18th, 2005, 07:26 AM
#5
Re: Writing Listbox Contents To A File
had to get all fancy didn't ya Hack 
(good idea)
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Jul 18th, 2005, 11:10 AM
#6
Thread Starter
Member
Re: Writing Listbox Contents To A File
Hmmm, the code works well, but what I neglected to ask was if it was possible to print it all on one line?
So for instance, on my form there is a textbox and a listbox. In the textbox could be a word such as "fruit" and then in the list there could be lots of rows; "Banana", "Apple", "Pear" e.t.c, as they are all associated with that word.
Then, when the program writes the contents to a file, I would like the contents to look like this:
"Fruit", "Apple", "Pear", "Banana" all on one line
As opposed to:
Fruit
Apple
Pear
Banana
Is this easy enough to do?
Regards,
Snowblind.
-
Jul 18th, 2005, 11:12 AM
#7
Re: Writing Listbox Contents To A File
VB Code:
Dim StrBuff as string
For x = 0 to list1.listcount - 1
StrBuff = strbuff & list1.list(x) & ","
Next x
Open "c:\path\filename.txt" for output as #1
Print #1, strbuff
Close #1
easy enuff
-
Jul 18th, 2005, 11:13 AM
#8
Thread Starter
Member
Re: Writing Listbox Contents To A File
In fact, please ignore my previous post! I've found a way of doing it, I can have a string with all the listbox values in it, and then just write that string to a textfile.
Thanks for your help everyone,
Snowblind.
EDIT: After posting this, I realised I had a reply. Thanks for the reply, that's the same way I thought of doing it! Just took me a little longer to think it through!
-
Jul 18th, 2005, 01:50 PM
#9
Lively Member
Re: Writing Listbox Contents To A File
 Originally Posted by |2eM!x
VB Code:
Dim StrBuff as string
For x = 0 to list1.listcount - 1
StrBuff = strbuff & list1.list(x) & ","
Next x
Open "c:\path\filename.txt" for output as #1
Print #1, strbuff
Close #1
easy enuff 
I have a question if you don't mind
I edit the code to make it put the contents of the listbox in a text box:
For x = 0 to list1.listcount - 1
StrBuff = strbuff & list1.list(x) & vbcrlf
Next x
text1.text=strbuff
- The problem is that it will be always an extra empty line in the end of the Text Box
How can i do something like that:
text1.text=text1.text - vbcrlf ?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|