Click here to Skip to main content
15,666,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone.

I have a Listview with multiple columns - rows are progressively added.
At a given point a button is clicked and the contents of the Listview must be written to a text file.
Each row of the Listview is a line in the Text file and each column is delimited with | in the text file.

Using the code below I am getting :

ListViewSubItem: {03/09/2013}|ListViewSubItem: {USD/GBP}|......etc

instead of

03/09/2013|USD/GBP|.....etc

Why is "ListViewSubItem: {} being returned?

Can anyone please suggest what I am doing wrong?



VB
Private Sub UpdateJournal()
     Dim JournalFileName As String = "EtxJournal.dat"
     Dim JournalLine As String
     Dim i As Integer
     Dim aryText(4) As String


     '---Create the Journal
     Dim objWriter As New System.IO.StreamWriter(JournalFileName, False)


     '---Write to Journal

     For x = 0 To lsv_Journal.Items.Count - 1

         JournalLine = lsv_Journal.Items(x).SubItems(0).ToString & "|" & _
                       lsv_Journal.Items(x).SubItems(1).ToString & "|" & _
                       lsv_Journal.Items(x).SubItems(2).ToString & "|" & _
                       lsv_Journal.Items(x).SubItems(3).ToString & "|" & _
                       lsv_Journal.Items(x).SubItems(4).ToString & "|" & _
                       lsv_Journal.Items(x).SubItems(5).ToString & "|" & _
                       lsv_Journal.Items(x).SubItems(6).ToString & "|" & _
                       lsv_Journal.Items(x).SubItems(7).ToString & "|" & _
                       lsv_Journal.Items(x).SubItems(8).ToString & "|" & _
                       lsv_Journal.Items(x).SubItems(9).ToString

         objWriter.Write(JournalLine)

     Next x

     objWriter.Close()

 End Sub
Posted
Updated 3-Sep-13 0:11am
v2

1 solution

You need to use the Text property.
Note that I used StreamWriter method WriteLine instead of Write.

VB
Private Sub UpdateJournal()
     Dim JournalFileName As String = "EtxJournal.dat"
     Dim JournalLine As String
     Dim x As Integer
     Dim aryText(4) As String
     '---Create the Journal
     Dim objWriter As New System.IO.StreamWriter(JournalFileName, False)
     '---Write to Journal
     For x = 0 To lsv_Journal.Items.Count - 1
         JournalLine = lsv_Journal.Items(x).SubItems(0).Text & "|" & _
                       lsv_Journal.Items(x).SubItems(1).Text & "|" & _
                       lsv_Journal.Items(x).SubItems(2).Text & "|" & _
                       lsv_Journal.Items(x).SubItems(3).Text & "|" & _
                       lsv_Journal.Items(x).SubItems(4).Text & "|" & _
                       lsv_Journal.Items(x).SubItems(5).Text & "|" & _
                       lsv_Journal.Items(x).SubItems(6).Text & "|" & _
                       lsv_Journal.Items(x).SubItems(7).Text & "|" & _
                       lsv_Journal.Items(x).SubItems(8).Text & "|" & _
                       lsv_Journal.Items(x).SubItems(9).Text
         objWriter.WriteLine(JournalLine)
     Next x
     objWriter.Close()
 End Sub


Here is an alternative using StringBuilder:
VB
Imports System.Text

...


Private Sub UpdateJournal()
    Dim JournalFileName As String = "EtxJournal.dat"
    Dim JournalLine As New StringBuilder(4096)
    Dim x As Integer
    Dim y As Integer
    Dim aryText(4) As String
    '---Create the Journal
    Dim objWriter As New System.IO.StreamWriter(JournalFileName, False)
    '---Write to Journal
    For x = 0 To lsv_journal.Items.Count - 1
        For y = 0 To 9
            JournalLine.Append(lsv_journal.Items(x).SubItems(y).Text)
            If y <> 9 Then JournalLine.Append("|")
        Next
        objWriter.WriteLine(JournalLine.ToString)
    Next x
    objWriter.Close()
End Sub
 
Share this answer
 
v6
Comments
Darrell de Wet 4-Sep-13 1:41am    
That's brilliant, thanks very much. I tried your alternative suggestion and it worked like a dream. Thank you for your effort.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900