Results 1 to 6 of 6

Thread: Reading Text File & Displaying In A Listview

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    8

    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!

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    8

    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.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Reading Text File & Displaying In A Listview

    vb.net Code:
    1. OpenFileDialog_ResourceFile.InitialDirectory = ("C:\users\name\documents\studentProjects")
    2.         OpenFileDialog_ResourceFile.ShowDialog()
    3.         For Each ln In IO.File.ReadLines(OpenFileDialog_ResourceFile.FileName)
    4.             lsvProjects.Items.Add(ln)
    5.         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!

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    8

    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?

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Reading Text File & Displaying In A Listview

    vb.net Code:
    1. Dim reader As New System.IO.StreamReader(OpenFileDialog_ResourceFile.FileName)
    2.         Dim lines As String = reader.ReadToEnd ' this reads the whole file as a single string
    3.         For Each line As String In lines ' so this will give you each character in the string in turn
    4.             lisvProjects.Items.Add(line)
    5.         Next
    6.         reader.Close()
    7.  
    8.         ' Keep reading a line until we hit the end of the file
    9.         Do While Not reader.EndOfStream ' you've already read the entire file and closed the stream by this point
    10.             ' Read a line and add to lisvProjects
    11.             lisvProjects.Items.Add(reader.ReadLine()) ' so this won't read anything because it's never executed
    12.         Loop
    13.  
    14.         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
  •  



Click Here to Expand Forum to Full Width