Results 1 to 16 of 16

Thread: Text File & List Box

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Text File & List Box

    Hi all,
    I am supposed to create a program that has the lists of computers for each and every room in the building. This is how it goes:

    There is a list box with all rooms in it:

    When the user chooses one of them, the list of computers is displayed: I want this list to be stored in a text file. So could someone tell me how can I:
    ~ Create a text file
    ~ Write to a text file
    ~ I want each line from the text file to be an item: how do i do that
    ~ I want to edit the text file

    You could just give me a brief code that allows me to do the above. I will be able to refurb it to use for my methods

    Thank you for your help in advance,
    I appreciate it
    Khanjan
    Hey... If you found this post helpful please rate it.

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

    Re: Text File & List Box

    Quote Originally Posted by khanjan_a2k
    ~ Create a text file
    VB Code:
    1. Open "c:\machines.txt" For Append As # 1
    Quote Originally Posted by khanjan_a2k
    ~ Write to a text file
    VB Code:
    1. Print #1, "MachineName"
    Quote Originally Posted by khanjan_a2k
    ~ I want each line from the text file to be an item: how do i do that
    Print #1 will place each item on a separate line
    Quote Originally Posted by khanjan_a2k
    ~ I want to edit the text file
    Open the file and read the whole thing into a multiline textbox. Do you editing, save the contents back.

  3. #3

  4. #4
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Text File & List Box

    VB Code:
    1. Dim ff As Integer, i As Integer
    2. Dim ln As String
    3.  
    4.     'to save it
    5.  
    6.     ff = FreeFile 'that is your file number
    7.         Open "C:\test.txt" For Output As #ff 'opening the file (even if it does not exist yet) for writing - output
    8.             For i = 0 To List1.ListCount - 1 'write each list item with the loop
    9.                 Print #ff, List1.List (i)
    10.             Next i
    11.         Close #ff 'dont forget to close the file
    12.  
    13.     'to read it
    14.  
    15.     ff = FreeFile
    16.     List1.Clear
    17.  
    18.         Open "c:\test.txt" For Input As #ff 'open it for reading - input
    19.             Do Until EOF (ff) 'read until the end of file
    20.                 Line Input #ff, ln 'input each line
    21.                     List1.AddItem ln 'add it to the list
    22.             Loop
    23.         Close #ff
    Also, you have a million of samples all over the forums on writing/reading text files.

  5. #5
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Text File & List Box

    If you want to get a line from a text box and add it to a list(other than the ways listed with the open functions), like from a textbox within your program for example, you can always check for Chr$(13) / Chr$(10) and do a Mid$() to get it. It's the long way around, but useful sometimes. I wouldn't recommend using it for a very large list, though. It will eat up your memory!

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Text File & List Box

    I want to take each line from the text file and put it each line as a seperate item in the listbox... I think you understood me wrong there Hack


    Khanjan
    Last edited by khanjan_a2k; Oct 6th, 2006 at 10:34 AM.
    Hey... If you found this post helpful please rate it.

  7. #7
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Text File & List Box

    Quote Originally Posted by khanjan_a2k
    I want to take each line from the text file and put it each line as a seperate item in the listbox...
    That's what my code from post #4 does.

  8. #8
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Text File & List Box

    Quote Originally Posted by gavio
    VB Code:
    1. Dim ff As Integer, i As Integer
    2. Dim ln As String
    3.  
    4.     'to save it
    5.  
    6.     ff = FreeFile 'that is your file number
    7.         Open "C:\test.txt" For Output As #ff 'opening the file (even if it does not exist yet) for writing - output
    8.             For i = 0 To List1.ListCount - 1 'write each list item with the loop
    9.                 Print #ff, List1.List (i)
    10.             Next i
    11.         Close #ff 'dont forget to close the file
    12.  
    13.     'to read it
    14.  
    15.     ff = FreeFile
    16.     List1.Clear
    17.  
    18.         Open "c:\test.txt" For Input As #ff 'open it for reading - input
    19.             Do Until EOF (ff) 'read until the end of file
    20.                 Line Input #ff, ln 'input each line
    21.                     List1.AddItem ln 'add it to the list
    22.             Loop
    23.         Close #ff
    Also, you have a million of samples all over the forums on writing/reading text files.

    I just tested this, and came up with a problem: it only lists 3000 or so lines in the listbox. I am interested in this code because it seems to be a quick way to add a lot of lines from a text file to a listbox(where as I have been using another method to pull the lines straight from a text box, which results in the app freezing if there are more than a couple thousand lines).

    Any way to increase this to add roughly 15,000 lines(ie. thewhole text file?)

  9. #9
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Text File & List Box

    Quote Originally Posted by gavio
    That's what my code from post #4 does.
    You seem to be fairly knowledgable... think you could help me out with my thread in the API forum by any chance?

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

    Re: Text File & List Box

    Quote Originally Posted by BrendanDavis
    I just tested this, and came up with a problem: it only lists 3000 or so lines in the listbox. I am interested in this code because it seems to be a quick way to add a lot of lines from a text file to a listbox(where as I have been using another method to pull the lines straight from a text box, which results in the app freezing if there are more than a couple thousand lines).

    Any way to increase this to add roughly 15,000 lines(ie. thewhole text file?)
    This probably is not a number of lines issue as much as it is a memory issue. The listbox control is limited to 32K - you may be exceeding the limit.

  11. #11
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Text File & List Box

    Quote Originally Posted by BrendanDavis
    Any way to increase this to add roughly 15,000 lines(ie. thewhole text file?)
    Try with a ListView.

  12. #12
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Text File & List Box

    Quote Originally Posted by Hack
    This probably is not a number of lines issue as much as it is a memory issue. The listbox control is limited to 32K - you may be exceeding the limit.
    That makes sense. I knew it wasn't a line issue, because the last line it added wasn't even a full line.. it was a partial. And also, loading the huge text file in a textbox worked fine, so I figured it was the listbox, not the code.

  13. #13
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Text File & List Box

    Quote Originally Posted by gavio
    VB Code:
    1. Open "C:\test.txt" For Output As #ff 'opening the file (even if it does not exist yet) for writing - output
    Append will do the same thing, without destroying the file if it does exist. Depends on whether he wants to add everything at once, or be able to add things later.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Text File & List Box

    Well... what I want is to create a text file. Then add the list of computers in the particular room of the bulding. So the user can add the list of computers at the beginning. But I also want to be able to remove or add more computers to the list. So I will be adding a whole lot on the first time. But... in the near future... I will be replacing computers on the text files.

    Also I will be storing the history of changes made to each computer... and the history of problems with each computer.

    So I guess I will use Append since it will not destroy the file.

    Also... lets say I want to remove an item from the middle of the list... How would I do that?

    Khanjan
    Hey... If you found this post helpful please rate it.

  15. #15
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Text File & List Box

    if you need to use the text file to store values quite often, why dont you consider database for this...
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  16. #16
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Text File & List Box

    Quote Originally Posted by khanjan_a2k
    Also... lets say I want to remove an item from the middle of the list... How would I do that?
    Try the following:
    VB Code:
    1. Private Sub List1_KeyDown(KeyCode As Integer, Shift As Integer)
    2.     If KeyCode = vbKeyDelete And List1.ListIndex <> -1 Then
    3.         If MsgBox("Are you sure you want to delete " & Chr(34) & List1.List(List1.ListIndex) & Chr(34) & " from the list?", vbQuestion + vbYesNo) = vbYes Then
    4.             List1.RemoveItem (List1.ListIndex)
    5.         End If
    6.     End If
    7. 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