|
-
Oct 6th, 2006, 10:01 AM
#1
Thread Starter
Hyperactive Member
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.

-
Oct 6th, 2006, 10:04 AM
#2
Re: Text File & List Box
 Originally Posted by khanjan_a2k
~ Create a text file
VB Code:
Open "c:\machines.txt" For Append As # 1
 Originally Posted by khanjan_a2k
~ Write to a text file
 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
 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.
-
Oct 6th, 2006, 10:05 AM
#3
Re: Text File & List Box
For samples and tutroials visit our FAQ section (not to mention this forum with tons of samples).
-
Oct 6th, 2006, 10:09 AM
#4
Re: Text File & List Box
VB Code:
Dim ff As Integer, i As Integer
Dim ln As String
'to save it
ff = FreeFile 'that is your file number
Open "C:\test.txt" For Output As #ff 'opening the file (even if it does not exist yet) for writing - output
For i = 0 To List1.ListCount - 1 'write each list item with the loop
Print #ff, List1.List (i)
Next i
Close #ff 'dont forget to close the file
'to read it
ff = FreeFile
List1.Clear
Open "c:\test.txt" For Input As #ff 'open it for reading - input
Do Until EOF (ff) 'read until the end of file
Line Input #ff, ln 'input each line
List1.AddItem ln 'add it to the list
Loop
Close #ff
Also, you have a million of samples all over the forums on writing/reading text files.
-
Oct 6th, 2006, 10:21 AM
#5
Hyperactive Member
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!
-
Oct 6th, 2006, 10:30 AM
#6
Thread Starter
Hyperactive Member
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.

-
Oct 6th, 2006, 10:57 AM
#7
Re: Text File & List Box
 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.
-
Oct 6th, 2006, 11:02 AM
#8
Hyperactive Member
Re: Text File & List Box
 Originally Posted by gavio
VB Code:
Dim ff As Integer, i As Integer
Dim ln As String
'to save it
ff = FreeFile 'that is your file number
Open "C:\test.txt" For Output As #ff 'opening the file (even if it does not exist yet) for writing - output
For i = 0 To List1.ListCount - 1 'write each list item with the loop
Print #ff, List1.List (i)
Next i
Close #ff 'dont forget to close the file
'to read it
ff = FreeFile
List1.Clear
Open "c:\test.txt" For Input As #ff 'open it for reading - input
Do Until EOF (ff) 'read until the end of file
Line Input #ff, ln 'input each line
List1.AddItem ln 'add it to the list
Loop
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?)
-
Oct 6th, 2006, 11:03 AM
#9
Hyperactive Member
Re: Text File & List Box
 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?
-
Oct 6th, 2006, 11:17 AM
#10
Re: Text File & List Box
 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.
-
Oct 6th, 2006, 11:38 AM
#11
Re: Text File & List Box
 Originally Posted by BrendanDavis
Any way to increase this to add roughly 15,000 lines(ie. thewhole text file?)
Try with a ListView.
-
Oct 6th, 2006, 12:30 PM
#12
Hyperactive Member
Re: Text File & List Box
 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.
-
Oct 6th, 2006, 01:26 PM
#13
Re: Text File & List Box
 Originally Posted by gavio
VB Code:
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
-
Oct 6th, 2006, 10:24 PM
#14
Thread Starter
Hyperactive Member
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.

-
Oct 6th, 2006, 11:57 PM
#15
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.
-
Oct 7th, 2006, 06:31 AM
#16
Re: Text File & List Box
 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:
Private Sub List1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDelete And List1.ListIndex <> -1 Then
If MsgBox("Are you sure you want to delete " & Chr(34) & List1.List(List1.ListIndex) & Chr(34) & " from the list?", vbQuestion + vbYesNo) = vbYes Then
List1.RemoveItem (List1.ListIndex)
End If
End If
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|