Writing All listView1 Items, Weird...
-
Hello All! :-) I am trying to write all items inside my listview control to a text file and I have the following code, I am
using System.IO;
File.AppendAllText("C:\\Users\\Jase\\Desktop\\File.txt", listView1.Items.ToString());
So, when I click on a button it will write each item to file but it doesn't work and I haven't been able to find any help on google or msdn. Does anybody have any pointers/links/samples they'd be willing to share? :-) Thanks for reading. Regards, j.t.
j.t.
-
Hello All! :-) I am trying to write all items inside my listview control to a text file and I have the following code, I am
using System.IO;
File.AppendAllText("C:\\Users\\Jase\\Desktop\\File.txt", listView1.Items.ToString());
So, when I click on a button it will write each item to file but it doesn't work and I haven't been able to find any help on google or msdn. Does anybody have any pointers/links/samples they'd be willing to share? :-) Thanks for reading. Regards, j.t.
j.t.
A quick look at the ListView class members states that Items is a property which returns ListViewItemCollection so no need to look further. Since you are executing the ToString() method on the ListViewItemCollection it is basically returning a string that represents the current object which is probaly something like System.Something.Something. There is nothing weird about that. If you have a class say: class Employee { public string ToString() { // Here is the implementation for the ToString() method. Some classes return the object name, some // return something meaningful but it all depends on the implementation. Some can event decide // to return "In your dreams this would work!" which is unlikely but as long it is a string, it is // valid. } } If you want all the objects then do this: // First save the ListViewItemCollection returned in a ListViewItemCollection reference ListViewItemCollection allItems = listView1.Items; Then iterate over all the items using foreach or some other loop. The other option you have is to extend the ListViewItemCollection class and override the ToString() method to return all the items instead of the base class implementation. Or you can do this: foreach(ListViewItem currentItem in listView1.Items) { // Here write each item which is currentItem to file }
-
Hello All! :-) I am trying to write all items inside my listview control to a text file and I have the following code, I am
using System.IO;
File.AppendAllText("C:\\Users\\Jase\\Desktop\\File.txt", listView1.Items.ToString());
So, when I click on a button it will write each item to file but it doesn't work and I haven't been able to find any help on google or msdn. Does anybody have any pointers/links/samples they'd be willing to share? :-) Thanks for reading. Regards, j.t.
j.t.
It's not weird at all. You assume that ToString will iterate over the collection, and find some sort of meaningful string value for each item. It plain isn't going to do that. I would imagine that you're getting ListViewItemCollection written to your text file. You need to write the code to work out a format for each item and to iterate over the items.
Christian Graus Driven to the arms of OSX by Vista.
-
It's not weird at all. You assume that ToString will iterate over the collection, and find some sort of meaningful string value for each item. It plain isn't going to do that. I would imagine that you're getting ListViewItemCollection written to your text file. You need to write the code to work out a format for each item and to iterate over the items.
Christian Graus Driven to the arms of OSX by Vista.