CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2001
    Location
    PA, U.S.
    Posts
    3

    Loading and saving items to and from a listbox?

    I am making a program for my High School senior project and it includes a Log In form. When a user logs in, their name is added to a listbox. Is there a way to save the contents of this listbox to a file of some sort when the user logs in, and also be loaded from this file to the listbox when the form is loaded again? If this is possible, could someone please help me?

    Thanks


  2. #2
    Join Date
    Feb 2001
    Posts
    54

    Re: Loading and saving items to and from a listbox?

    Hello:
    Use list1.AddItem. I would recommend that you save listbox to a database
    here code using ADO and Listbox to save information to Oracle database

    Private Sub Command4_Click()
    List1.AddItem (Text1.Text)
    Adodc1.Recordset.AddNew
    Adodc1.Recordset.Fields(1) = Val(List1.Text)
    Adodc1.Refresh
    End Sub

    Good Luck


  3. #3
    Join Date
    Feb 2001
    Posts
    54

    Re: Loading and saving items to and from a listbox?

    Hello:

    I forgot to mention to retreive the data. You need to do this:

    List2.AddItem (Adodc1.Recordset.Fields(0))
    Adodc1.Recordset.MoveNext

    Good Luck

    It can use DAO and Access to do this. Read more about DAO.
    You find ADO by using Project and then Component and then find Microsoft ADO then follow steps to link your Database to VB.



  4. #4
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Loading and saving items to and from a listbox?

    Below is two simple routines. The first will save your data to disk and the second will reload it to the Listbox. These routine assume your listbox is named "List1". Also you might want to change the fielname in the Open statements to suit your needs.


    private Sub cmdSave_Click()
    Dim X
    Open "C:\MyFile.txt" for Output as #1
    for X = 0 to List1.ListCount - 1
    print #1, List1.List(X)
    next X
    Close #1
    End Sub
    '
    '
    private Sub cmdLoad_Click()
    Dim Temp as string
    List1.Clear ' clear current entries
    Open "C:\MyFile.txt" for input as #1
    Do Until EOF(1)
    Line input #1, Temp
    List1.AddItem Temp
    Loop
    Close #1
    End Sub





    John G

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