By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,438 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

open txt file and add contents to listbox

P: 26
i need some help opening a text file and having the contents in that file add to a list.

for example a user types '/addlist C:/text.txt'. the program takes the c:/text.txt and adds the items to a listbox

how would i code this
Sep 8 '07 #1
Share this Question
Share on Google+
7 Replies


Expert 5K+
P: 8,434
i need some help opening a text file and having the contents in that file add to a list.

for example a user types '/addlist C:/text.txt'. the program takes the c:/text.txt and adds the items to a listbox

how would i code this
Expand|Select|Wrap|Line Numbers
  1. Open strFileName For Input Access Read Shared As #1
  2. Do Until Eof(1)
  3.   Line Input #1, strText
  4.   List1.AddItem strText
  5. Loop
  6. Close #1
Sep 8 '07 #2

waynespangler
P: 8
Try this:

Expand|Select|Wrap|Line Numbers
  1.          Dim str As String = ""
  2.         Dim ary() As String
  3.         str = My.Computer.FileSystem.ReadAllText("c:\test.txt")
  4.         ary = Split(str, vbNewLine)
  5.         Dim x As Integer
  6.         For x = 0 To UBound(ary)
  7.             If ary(x) <> "" Then
  8.                 ListBox1.Items.Add(ary(x))
  9.             End If
  10.         Next
  11.  
Sep 8 '07 #3

Expert 5K+
P: 8,434
i need some help opening a text file and having the contents in that file add to a list...
It would help if we knew what version of VB we're dealing with. My code should work (assuming I got that AddItem right) in just about any version, but I'm pretty sure My.Computer.FileSystem.ReadAllText is specific to the .Net world.

Anyway, you should certainly have enough to get you started by now.
Sep 8 '07 #4

P: 26
im working in VB6

your code works for small destinations like c:/file.txt but when i try

C:\Documents and Settings\Jacob Evans\Desktop\Grades\Semester1\sem1grades.txt

the program adds around 22k of blank spaces to the listbox and the program locks up
Sep 8 '07 #5

Expert 5K+
P: 8,434
your code works for small destinations ...
The difference would almost certainly be due to the contents of the file, rather than the name or location. Try opening the file in Notepad, and see what's in it.

Also, you might try using the Trim() function on each string before adding it to the listbox.
Sep 9 '07 #6

P: 26
they had the same text

1:1
2:2
3:3
4:4

just different locations
Sep 9 '07 #7

Expert 5K+
P: 8,434
they had the same text ...
Which code did you use, the ReadAllText or the old-style Line Input? I'm not familiar with ReadAllText (or any VB.Net stuff) - maybe it got confused by the spaces and read everything in the directory or something.

Ideas, anyone?
Sep 10 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.