Results 1 to 3 of 3

Thread: [RESOLVED] Populate 3 Row Listview From Text File

  1. #1

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Resolved [RESOLVED] Populate 3 Row Listview From Text File

    Hi

    I amd trying to populate a listview control from a text file.

    Text File has data set as:

    Line 1 = Name
    Line 2 = Date
    Line 3 = email address

    eg:


    Iam trying to use this code
    Code:
            'POPULATE LISTVIEW WITH TEXTFILE
            'http://www.vbforums.com/showthread.php?t=242250&highlight=write+listview+textfile
            Dim sr As IO.StreamReader
            Dim strLine, strLine2, strLine3 As String
            Dim strFileName As String = Application.StartupPath & "\Emailed.txt"
            ' Open the file to read.
            Try
                sr = IO.File.OpenText(strFileName)
                Try
                    With Me.lsvEmailed
                        While sr.Peek <> -1
                            strLine = sr.ReadLine
                            strLine2 = sr.ReadLine
                            strLine3 = sr.ReadLine
                            lsvEmailed.Items.Add(strLine).SubItems.Add(strLine2).Subitems.Add(strLine3)
                        End While
                    End With
                Catch
                    MsgBox("Unexpected error reading character file - check the contents of " _
                    + strFileName + ".", MsgBoxStyle.Exclamation)
                End Try
                sr.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error opening file!")
            End Try
    The code works fine if iam only adding 1 subitem
    Code:
    lsvEmailed.Items.Add(strLine).SubItems.Add(strLine2)
    but if i try to add 2 subitems
    Code:
    lsvEmailed.Items.Add(strLine).SubItems.Add(strLine2).Subitems.Add(strLine3)
    When i build the file i get a build error:
    Error 2 'Subitems' is not a member of 'System.Windows.Forms.ListViewItem.ListViewSubItem'. C:\Documents and Settings\Toecutter\My Documents\Visual Studio 2005\Projects\Emailed\Emailed\Form1.vb 201 25 Emailed
    Any help much appreciated
    Toe

    ps i have attached this to this thread
    http://www.vbforums.com/showthread.php?t=242250

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Populate 3 Row Listview From Text File

    What does this do:
    vb.net Code:
    1. lsvEmailed.Items.Add(strLine)
    It adds a ListViewItem and returns it. What does this do:
    vb.net Code:
    1. lsvEmailed.Items.Add(strLine).SubItems.Add(strLine2)
    It adds a ListViewItem and returns it, then adds a ListViewSubItem to that and returns it. Now, what would this do:
    vb.net Code:
    1. lsvEmailed.Items.Add(strLine).SubItems.Add(strLine2).Subitems.Add(strLine3)
    It will take the ListViewSubItem that was last returned and try to add a ListViewSubItem to it. ListViewSubItems don't have subitems though, so of course that's going to fail. You have to add the second subitem to the original item, not the first subitem.
    vb.net Code:
    1. With lsvEmailed.Items.Add(strLine).SubItems
    2.     .Add(strLine2)
    3.     .Add(strLine3)
    4. End With
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Populate 3 Row Listview From Text File

    Subitems' is not a member of 'System.Windows.Forms.ListViewItem.ListViewSubItem
    Which is exactly what the error mesage says

    Thank you

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