How to save a listview as a .txt file?

howester77

Active member
Joined
Aug 5, 2005
Messages
38
Programming Experience
5-10
I have a listview control, how do you save the contents of a listview into a .txt file?
 
This is really two seperate issues.

1. How do you get the data out of a ListView.
2. How do you save text to a file.


For the first one you can use a loop to iterate over the Items of the ListView. The Text property of the ListViewItem is what's displayed in the first column. Each item has a Subitems collection. The Text property of each subitem is what's displayed in the other columns. Note that myListViewItem.Text and myListViewItem.Subitems(0).Text are both the string from the first column. I suggest that you get this part working before you even think about text files. If you read the help/MSDN topics from the ListView and the members I've mentioned you should get code examples.

For the second part, you use a StreamWriter to write to a text file. The member that you'll be most interested in will be WriteLine. Again, reading the help/MSDN topics for the class and its members will give you useful information and code examples.
 
VB.NET:
        For Each item As ListViewItem In Me.ListView1.Items
            For Each subitem As ListViewItem.ListViewSubItem In item.SubItems
                MessageBox.Show(subitem.Text)
            Next subitem
        Next item
 
The ListView is like a 2D array, with rows and columns. The outer loop iterates over the items, which are the rows, while the inner loop iterates of the subitems, which are the columns.
 
Last edited:
Back
Top