|
-
Apr 25th, 2013, 04:57 PM
#1
Thread Starter
New Member
Reading Text File & Displaying In A Listview
Hi, I am having some trouble displaying text in a list view control. I have several columns in my list view,
and I am not sure how to read each line from a text file and 'place' it in the column.
Code:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
OpenFileDialog_ResourceFile.InitialDirectory = ("C:\users\name\documents\studentProjects")
End Sub
Private Sub Update_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
lisvProjects.BeginUpdate()
Dim reader As New System.IO.StreamReader(OpenFileDialog_ResourceFile.FileName)
Dim lines As String = reader.ReadToEnd
For Each line As String In lines
lisvProjects.Items.Add(line)
Next
reader.Close()
' Keep reading a line until we hit the end of the file
Do While Not reader.EndOfStream
' Read a line and add to lisvProjects
lisvProjects.Items.Add(reader.ReadLine())
Loop
reader.Close()
End Sub
Private Sub btnSelectFile_Click(sender As System.Object, e As System.EventArgs) Handles btnSelectFile.Click
lisvProjects.Items.Clear()
If OpenFileDialog_ResourceFile.ShowDialog() = DialogResult.OK Then
txtProjectMainFile.Text = OpenFileDialog_ResourceFile.FileName
Else
Exit Sub
End If
End Sub
End Class
I know my code is a bit rough, but I am only a beginner. Thanks in advance!
-
Apr 25th, 2013, 05:06 PM
#2
Re: Reading Text File & Displaying In A Listview
Give us an example line from the file and an indication of how you want to divide it and we'll see what we can do.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Apr 25th, 2013, 05:13 PM
#3
Thread Starter
New Member
Re: Reading Text File & Displaying In A Listview
As this program is just a practice for myself, the line from the text could be anything really. I have three different lines though:
TriviaGame
FastTimes
LaterWeaken
I want to divide it by line. For example, I want one row in the column to contain "TriviaGame" and the row directly below to
contain "FastTimes" and so on.
-
Apr 25th, 2013, 05:48 PM
#4
Re: Reading Text File & Displaying In A Listview
vb.net Code:
OpenFileDialog_ResourceFile.InitialDirectory = ("C:\users\name\documents\studentProjects")
OpenFileDialog_ResourceFile.ShowDialog()
For Each ln In IO.File.ReadLines(OpenFileDialog_ResourceFile.FileName)
lsvProjects.Items.Add(ln)
Next
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Apr 25th, 2013, 06:02 PM
#5
Thread Starter
New Member
Re: Reading Text File & Displaying In A Listview
Thank you! Your method works much better than the streamreader I was trying to use. Would you mind explaining why streamreader didn't work in this case?
-
Apr 25th, 2013, 06:14 PM
#6
Re: Reading Text File & Displaying In A Listview
vb.net Code:
Dim reader As New System.IO.StreamReader(OpenFileDialog_ResourceFile.FileName)
Dim lines As String = reader.ReadToEnd ' this reads the whole file as a single string
For Each line As String In lines ' so this will give you each character in the string in turn
lisvProjects.Items.Add(line)
Next
reader.Close()
' Keep reading a line until we hit the end of the file
Do While Not reader.EndOfStream ' you've already read the entire file and closed the stream by this point
' Read a line and add to lisvProjects
lisvProjects.Items.Add(reader.ReadLine()) ' so this won't read anything because it's never executed
Loop
reader.Close()
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
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
|