CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817

    Get every item from listbox and write it to a file

    i have "test1" and "test2" in a Listbox, how can I write them into a file so later on I can read from that file and use list1.additem to add strings "test1" and "test2" back into listbox

    Thank You


  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: Get every item from listbox and write it to a file

    to write the listbox contents to a file:

    Dim yourfile as string
    Dim i as Integer
    yourfile = "c:\test.txt"
    Dim hfile as Integer
    hfile = FreeFile
    Open yourfile for Output as #hfile
    for i = 0 to lbx.ListCount - 1
    print #hfile, lbx.List(i)
    next i
    Close hfile






  3. #3
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817

    Re: Get every item from listbox and write it to a file

    Ok, now how do I read data from that file and reverse the process by putting what I have saved back into listbox

    Thank You


  4. #4
    Join Date
    May 1999
    Posts
    3,332

    Re: Get every item from listbox and write it to a file


    private Sub Command2_Click()
    Dim yourfile as string
    Dim i as Integer
    yourfile = "c:\test.txt"
    Dim hfile as Integer
    hfile = FreeFile
    Open yourfile for input as #hfile
    Dim strline as string
    lbx.Clear
    Do While Not EOF(hfile)
    Line input #hfile, strline
    lbx.AddItem strline
    Loop
    Close hfile

    End Sub






  5. #5
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817

    Re: Get every item from listbox and write it to a file

    Thanks bud.


  6. #6
    Join Date
    Jul 1999
    Location
    Athens, Hellas
    Posts
    769

    Re: Get every item from listbox and write it to a file

    It is faster enough instead of 'for' loop:

    for i = 0 to lbx.ListCount - 1
    print #hfile, lbx.List(i)
    next i



    to use 'for each' loop:

    dim li as listitem
    for Each li In YourListView.ListItems
    print #hfile, li
    next




    Michael Vlastos
    Automation Engineer
    Company SouthGate Hellas SA
    Development Department
    Athens, Greece

  7. #7
    Join Date
    May 1999
    Posts
    3,332

    Re: Get every item from listbox and write it to a file

    may be you are right, but AndyK asked for a listbox solution. How would a for each loop look on a list box?


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

Featured