Results 1 to 9 of 9

Thread: Writing Listbox Contents To A File

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2005
    Posts
    56

    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.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Writing Listbox Contents To A File

    yes loop through

    VB Code:
    1. Open "c:\path\filename.txt" for output as #1
    2. For x = 0 to list1.listcount - 1
    3. print #1, list1.list(x)
    4. next
    5. Close #1
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2005
    Posts
    56

    Re: Writing Listbox Contents To A File

    Many thanks for your quick reply,

    Regards,

    Snowblind.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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:
    1. Private Sub SaveLoadListbox(plstLB As ListBox, pstrFileName As String, _
    2. pstrSaveOrLoad As String)
    3.  
    4. Dim strListItems As String
    5. Dim i As Long
    6.  
    7. Select Case pstrSaveOrLoad
    8.    Case "save"
    9.     Open pstrFileName For Output As #1
    10.     For i = 0 To plstLB.ListCount - 1        
    11.         Print #1, plstLB.List(plstLB.ListIndex)
    12.     Next
    13.     Close #1
    14.  
    15.    Case "load"
    16.    plstLB.Clear
    17.     Open pstrFileName For Input As #1
    18.     While Not EOF(1)
    19.       Line Input #1, strListItems
    20.       plstLB.AddItem strListItems
    21.     Wend
    22.     Close #1
    23. End Select
    24.  
    25. End Sub
    26.  
    27. Private Sub cmdLoad_Click()
    28. Call SaveLoadListbox(List1, "c:\path\filename.txt", "load")
    29. End Sub
    30.  
    31. Private Sub cmdSave_Click()
    32. Call SaveLoadListbox(List1, "c:\path\filename.txt", "save")
    33. End Sub

  5. #5
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    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"

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2005
    Posts
    56

    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.

  7. #7
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Writing Listbox Contents To A File

    VB Code:
    1. Dim StrBuff as string
    2. For x = 0 to list1.listcount - 1
    3. StrBuff = strbuff & list1.list(x) & ","
    4. Next x
    5. Open "c:\path\filename.txt" for output as #1
    6. Print #1, strbuff
    7. Close #1
    easy enuff

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2005
    Posts
    56

    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!

  9. #9
    Lively Member
    Join Date
    Feb 2005
    Posts
    120

    Re: Writing Listbox Contents To A File

    Quote Originally Posted by |2eM!x
    VB Code:
    1. Dim StrBuff as string
    2. For x = 0 to list1.listcount - 1
    3. StrBuff = strbuff & list1.list(x) & ","
    4. Next x
    5. Open "c:\path\filename.txt" for output as #1
    6. Print #1, strbuff
    7. 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
  •  



Click Here to Expand Forum to Full Width