[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:
http://img524.imageshack.us/img524/8054/imagejk4.jpg
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:
Quote:
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
Re: Populate 3 Row Listview From Text File
What does this do:
vb.net Code:
lsvEmailed.Items.Add(strLine)
It adds a ListViewItem and returns it. What does this do:
vb.net Code:
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:
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:
With lsvEmailed.Items.Add(strLine).SubItems
.Add(strLine2)
.Add(strLine3)
End With
Re: Populate 3 Row Listview From Text File
Quote:
Subitems' is not a member of 'System.Windows.Forms.ListViewItem.ListViewSubItem
Which is exactly what the error mesage says :blush:
Thank you :)