Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How can you save a ListView items and SubItems side by side?
ListView1 contains the purchased items and the Subitems contains the price. Below is the example:
mid1
Purchases          Price
 Apple              5
 Orange             2


When I try to save the information, I get the following:
MIDL
ListViewItem: {Apple 1 @ 5}
ListViewItem: {Orange 1 @ 2}


Here is the Code I used:
VB
Dim W As IO.StreamWriter
        Dim i As Integer
        W = New IO.StreamWriter("C:\Daily Purchases" & ".txt")
        For i = 0 To ListView1.Items.Count - 1
            W.WriteLine(ListView1.Items.Item(i))
        Next
        W.Close()
Posted
Updated 14-Nov-10 12:27pm
v3

1 solution

Try:
VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim W As IO.StreamWriter
      Dim i As Integer
      W = New IO.StreamWriter("F:\TEMP\Daily Purchases" & ".txt")
      For i = 0 To ListView1.Items.Count - 1
          W.WriteLine(ListView1.Items.Item(i).Text + ":" + ListView1.Items.Item(i).SubItems(1).Text)
      Next
      W.Close()
  End Sub

and you will have

Apple:5
Banana:3

in your file.
 
Share this answer
 
Comments
Naa Nana 14-Nov-10 20:24pm    
Thank you very much body! You rock!!!
Naa Nana 14-Nov-10 21:37pm    
One more question. Is there a way to leave about two tabs space between the apple and 5 in your code:
W.WriteLine(ListView1.Items.Item(i).Text + ":" + ListView1.Items.Item(i).SubItems(1).Text)
Dr.Walt Fair, PE 15-Nov-10 0:36am    
Just insert as many vbTab characters as you want, like + vbTab + vbTab + ":"

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