3 Replies - 11804 Views - Last Post: 06 November 2011 - 07:03 AM Rate Topic: -----

#1 sharpy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 394
  • Joined: 02-June 07

Stream read text and split into listview

Posted 22 December 2008 - 10:41 PM

Hi All

What I am trying to do is stream write to a text file information from 5 columns I have in my list view and then read the data back into those columns as required. I have manage to write the data to the text file in this format

1,Facemill,2290 ,2545 ,0.622

I have Split the info with a comma

I can read the info back into my Listview only as one line of information. What I dont know how to do is split the line at every comma and place the info back in its correct column and then split the next line ETC. The code I have up to now is this.

Try
			Dim sr As StreamReader
			Dim fs As FileStream
			'clearing listview data toavoid confusion 
			ListView1.Items.Clear()
			'filestream object to open and read file 

			 fs = New FileStream(("c:\listview.txt"), FileMode.OpenOrCreate)
			'stream reader object to read streamed input 
			sr = New StreamReader(fs)
			Dim itm As Object
			'reading line by line 
			itm = sr.ReadLine
			While Not itm = Nothing

				ListView1.Items.Add(itm)
				'readline from stream
				sr.ReadLine()


			End While
			'close stream reader and filestrema object 
			sr.Close()
			fs.Close()
		Catch ex As System.Exception

			System.Windows.Forms.MessageBox.Show(ex.Message, "Load Tool Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
		End Try



If anybody can help and show me how to this it would be much appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Stream read text and split into listview

#2 sharpy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 394
  • Joined: 02-June 07

Re: Stream read text and split into listview

Posted 23 December 2008 - 06:11 AM

Hi All

Been working on this a bit more and this is what I have now. The code will populate the columns in my listview, however to get it to work I have to limit the loop with a count as the code just loops around and around. Also I have added a second line in my text doc but it will not read this line in, only the first line which it will do over and over again.

Text Doc data

1,Facemill,2290 ,2545 ,0.622
2,Facemill2,3000 ,2000 ,0.700



This is my code

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		
		Dim arrayList As New ArrayList
		Dim line As String
		Dim Tool As String
		Dim Description As String
		Dim Feed As String
		Dim Speed As String
		Dim Time As String
		'Dim count As Integer

		Try
			Dim sr As StreamReader = New StreamReader("C:\listview.txt")

			While sr.Peek() <> -1
				arrayList.Add(sr.ReadLine())

			End While
			sr.Close()

		Catch Ex As Exception
			MessageBox.Show(Ex.Message, "Read Error")
		End Try

		For Each line In arrayList
			Dim tmp() As String = Split(line)
			'While count = 0
			While (line.IndexOf(",") >= 0)
				Dim split As String() = line.Split(New [Char]() {","})
				Tool = split(0)
				Description = split(1)
				Feed = split(2)
				Speed = split(3)
				Time = split(4)

				ListView1.Items.Add(Tool)
				ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Description)
				ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Feed)
				ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Speed)
				ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Time)

				'count = count + 1
			End While

		Next

	End Sub



Im sure its something simple but I just cannot see where the problem is so if you can help it would be appreciated :crazy:
Was This Post Helpful? 0
  • +
  • -

#6 sharpy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 394
  • Joined: 02-June 07

Re: Stream read text and split into listview

Posted 23 December 2008 - 06:57 AM

Hi all

Feel like im having a one way conversation here anyway for those who are interested and I always like to finish off a thread just in case somebody has been following, I have solve my problem with this code.

Dim Tool As String
		Dim Description As String
		Dim Feed As String
		Dim Speed As String
		Dim Time As String

		Try

			Dim sr As StreamReader
			'declaration of filestream to open file 
			Dim fs As FileStream
			'clearing listbox data toavoid confusion 
			'filestream object to open and read file 
			fs = New FileStream(("C:\listview.txt"), FileMode.OpenOrCreate)
			'stream reader object to read streamed input 
			sr = New StreamReader(fs)
			Dim itm As Object
			'reading line by line 
			itm = sr.ReadLine
			While Not itm = Nothing
				Dim split As String() = itm.Split(New [Char]() {","})
				Tool = split(0)
				Description = split(1)
				Feed = split(2)
				Speed = split(3)
				Time = split(4)

				ListView1.Items.Add(Tool)
				ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Description)
				ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Feed)
				ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Speed)
				ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Time)


				itm = sr.ReadLine
			End While
			'close stream reader and filestrema object 
			sr.Close()
			fs.Close()
		Catch ex As System.Exception

			System.Windows.Forms.MessageBox.Show(ex.Message, "Load Tool Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
		End Try


Feliz Navida a todos :D

This post has been edited by sharpy: 23 December 2008 - 06:59 AM

Was This Post Helpful? 2

#7 MisterBoolean  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-November 11

Re: Stream read text and split into listview

Posted 06 November 2011 - 07:03 AM

hey, i just got your code and you helped me a lot! Thx you! =)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1