473,412 Members | 4,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

open txt file and add contents to listbox

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
7 8078
Killer42
8,435 Expert 8TB
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
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
Killer42
8,435 Expert 8TB
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
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
Killer42
8,435 Expert 8TB
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
they had the same text

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

just different locations
Sep 9 '07 #7
Killer42
8,435 Expert 8TB
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

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

Similar topics

6
by: Roy Riddex | last post by:
I have a text file which holds data for 30 cars in the following way: CarRegistration CarType CarClass Available I'm trying to display the full contents of this...
4
by: Andras Gilicz | last post by:
Hi VB fans I'm working on a relatively large project in VB6 with about a dozen forms, including graphs, labels, text boxes, etc. The software itself is actually a flow simulator with more or...
13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
4
by: redneon | last post by:
I've written a simple application which connects to an FTP server and recursively lists the contents of the directories in this server in a ListBox. What I'm wanting to do is change the ListBox and...
9
by: Suman | last post by:
Happy Friday everyone!!! I am working on a windows service and a C# application and needed some help with certain functionality. Please read through my issue below. Thanks! I have a windows...
1
by: servernet1997 | last post by:
I have a gridview with 4 columns. Two of the columns are read-only and two are editable. The two editable columns are listboxes leveraging an ObjectDataSource. The first listbox is populated from a...
1
by: Kevin | last post by:
The menu form has two options, one to create a new estimate, the second to open an existing estimate. Under option two is a list box containing all of the estimates. There are several different...
6
by: kimiraikkonen | last post by:
Hi, I can read from a text file and insert text file's contents into a listbox whose format is line by line using this code: Dim reader As String reader = My.Computer.FileSystem.ReadAllText("c:...
2
by: Plumebee | last post by:
Hi, I am very new to programming and have just started to use Visual Basic 2005 Express Edition. I am trying to read from a text file to draw a rectangles and lines. However to begin I'm trying to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.