10 Replies - 1667 Views - Last Post: 06 August 2009 - 04:28 PM Rate Topic: -----

#1 unlimited24   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 28-August 08

reading lines from a .txt file and displaying it in a list box

Posted 19 July 2009 - 02:09 PM

hi. I would like to ask for help in determining how to make the listbox display each line from a text file as an item.

what I have so far:

If ListBox2.SelectedIndex = 0 Then
			Dim mystream As New IO.StreamReader(path:="test.txt")

			For Each line In mystream.ReadToEnd
				ListBox1.Items.Add(line)
				mystream.Close()
			Next

		End If


this solution, as it is, it displays every individual character as an item witch is not what i intend. I want the full line from the text file to be displayed on the listbox as an item, till the end of the document.

thank you :)

Is This A Good Question/Topic? 0
  • +

Replies To: reading lines from a .txt file and displaying it in a list box

#2 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: reading lines from a .txt file and displaying it in a list box

Posted 19 July 2009 - 03:25 PM

I suggest you System.IO (Part 1) Tutorial.
It has some useful information for you.
Was This Post Helpful? 0
  • +
  • -

#3 LoveIsNull   User is offline

  • Recovering D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 646
  • Joined: 10-March 09

Re: reading lines from a .txt file and displaying it in a list box

Posted 19 July 2009 - 03:37 PM

The StreamReader class has a method called ReadLine() which will return the next line in the stream. The method you used, ReadToEnd(), returns all content in one string. So when you do:
For Each line In mystream.ReadToEnd
you are actually inferring the variable line as type Char because ReadToEnd returns a string, thus you are going through each character.
There are also other classes like IO.File with methods such as ReadAllLines which will return a string array of the contents.
Was This Post Helpful? 0
  • +
  • -

#4 mark.bottomley   User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: reading lines from a .txt file and displaying it in a list box

Posted 19 July 2009 - 05:22 PM

Also - don't close the file on each loop - move the file close to after the "Next" statement
Was This Post Helpful? 0
  • +
  • -

#5 unlimited24   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 28-August 08

Re: reading lines from a .txt file and displaying it in a list box

Posted 20 July 2009 - 07:01 AM

thank you guys. I have solved my problem.

 If ListBox2.SelectedIndex = 0 Then
			ListBox1.Items.Clear()
			Dim path As String = "test.txt"
			Dim readText() As String = File.ReadAllLines(path)
			Dim s As String
			For Each s In readText
				ListBox1.Items.Add(s)
			Next



thank you again for you're help guys :)
Was This Post Helpful? 0
  • +
  • -

#6 DavisCreation   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-July 09

Re: reading lines from a .txt file and displaying it in a list box

Posted 25 July 2009 - 11:34 PM

View Postunlimited24, on 20 Jul, 2009 - 06:01 AM, said:

thank you guys. I have solved my problem.

 If ListBox2.SelectedIndex = 0 Then
			ListBox1.Items.Clear()
			Dim path As String = "test.txt"
			Dim readText() As String = File.ReadAllLines(path)
			Dim s As String
			For Each s In readText
				ListBox1.Items.Add(s)
			Next



thank you again for you're help guys :)

I was looking for this exact thing, but to work with an online text file. Can you help me please? I've tried everything I know to do, and it just doesn't work.
Was This Post Helpful? 0
  • +
  • -

#7 LoveIsNull   User is offline

  • Recovering D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 646
  • Joined: 10-March 09

Re: reading lines from a .txt file and displaying it in a list box

Posted 25 July 2009 - 11:40 PM

Define online text file, as in a text file that is online right?
But, where is this text file exactly, and how are you trying to access it?
You know what they say:
When you've tried everything that you know to do and it just doesn't work, go and learn some more!
Or read the rules and start a new topic providing us with the solution(s) you have attempted.
Was This Post Helpful? 0
  • +
  • -

#8 DavisCreation   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-July 09

Re: reading lines from a .txt file and displaying it in a list box

Posted 25 July 2009 - 11:48 PM

I would rather not make a new topic, since this is right in the lines of what I'm needing.

By online txt file, I mean a text file that is on my server.

I tried using the above script and it works fine for local txt files. I'm just not sure how to tell it to do the same thing, but for the text file on my server. http://www.example.com/sample.txt
Was This Post Helpful? 0
  • +
  • -

#9 LoveIsNull   User is offline

  • Recovering D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 646
  • Joined: 10-March 09

Re: reading lines from a .txt file and displaying it in a list box

Posted 26 July 2009 - 12:08 AM

If you're just looking to download the file you might want to check out the WebClient.
Was This Post Helpful? 0
  • +
  • -

#10 DavisCreation   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-July 09

Re: reading lines from a .txt file and displaying it in a list box

Posted 26 July 2009 - 07:16 AM

Maybe I should explain the situation.

I have a program that has a combobox. It always needs to be updated w/o redownloading the program, so what I figured was best is to keep a text file on my server that lists all items that should be in the combobox. Then, using a method very similar to the above, it would add those lines into the combobox.

What would be the most effective and clean way of achieving this?

EDIT: This is sorta veering away from the original posts. I may make a new topic. ( http://www.dreaminco...topic116873.htm )

This post has been edited by DavisCreation: 26 July 2009 - 07:42 AM

Was This Post Helpful? 0
  • +
  • -

#11 unlimited24   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 28-August 08

Re: reading lines from a .txt file and displaying it in a list box

Posted 06 August 2009 - 04:28 PM

ok guys thanks for the posts and i take it you solved your problem?! if not i can help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1