CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2000
    Location
    Mississippi
    Posts
    1

    Random Files, LstBox, & TxtBox

    I have a procedure that creates and stores data describing "people"(name, ht, wt,...) in a random access file. I also have a procedure to "query" that data for certain "people" and then display those "people" in a listbox. I now need to click on a "person" in the listbox and have the program fill txtboxes on the same form with the appropriate "personal info"(name, ht, wt...textbox for each). Am I making this harder than it's suppose to be? I'm new at this so that could be the case, but I can't get the program to list the selected "person's" info. When I get the program to do anything, it seems to use data from the last "person" in the random access file. Thanks to whoever can help me with this problem!!


  2. #2
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: Random Files, LstBox, & TxtBox

    Is there some reason you aren't using a database to hold this info? If you can, you should try to at least keep this in an Access db, because things would be a lot easier for one. If you can't, then when the user selects a person to view, you are pretty much gonna have to start at line 1 and go through the entire file until you find the person you are looking for. eg:

    Open fn for input as #1

    While Not EOF(1)
    Line input #1, sTemp
    'obviously, you will either have to know how many characters the selected name is
    If mid(sTemp,1,10) = sNameUserSelected then
    'get the rest of the info from this line
    End If
    Wend

    Close #1



    Or, if the file is at least deliminated with something a "|" or "~" maybe, and the lastname was always in the second position you use the Split() function instead of trying the Mid function :

    'open the file
    Dim sInfo() as string

    While Not EOF(1)
    Line input #1, sTemp
    sInfo = Split(sTemp,"|")
    If sInfo(2) = sNameUserSelected then
    '...
    End If
    Wend




    The split function will return an array of the values seperated by the deliminator you tell it to look for.

    Hope this helps/applies,
    John

    John Pirkey
    MCSD
    www.ShallowWaterSystems.com
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

  3. #3
    Join Date
    Feb 2000
    Location
    Indiana
    Posts
    308

    Re: Random Files, LstBox, & TxtBox

    You could make a person class and fill a collection with objects of type "person". Use this collection to fill your list. As soon as the people or in the collection, your data file can be closed and left out of the rest of the operations. If you use "Name" as the key for the collection you could do something like this:

    private Sub lstNames_Click()
    Dim TempPerson as Person
    set TempPerson = m_colPeople(lstNames.Text)
    With TempPerson
    txtHeight = .Height
    txtWeight = .Weight
    txtDogsFavoriteFood = .DogsFavoriteFood
    'etc...
    End With
    set TempPerson = nothing
    End Sub





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