Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Writing All listView1 Items, Weird...

Writing All listView1 Items, Weird...

Scheduled Pinned Locked Moved C#
helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jas0n23
    wrote on last edited by
    #1

    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.

    C C 2 Replies Last reply
    0
    • J jas0n23

      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.

      C Offline
      C Offline
      CodingYoshi
      wrote on last edited by
      #2

      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 }

      1 Reply Last reply
      0
      • J jas0n23

        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.

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        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.

        J 1 Reply Last reply
        0
        • C Christian Graus

          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.

          J Offline
          J Offline
          jas0n23
          wrote on last edited by
          #4

          Aaaaahhhh beautiful, now I understand :-) Thank you so much CodingYoshi and Christian Graus! Your help is much appreciated.

          j.t.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups