7 Replies - 730 Views - Last Post: 21 February 2013 - 11:54 AM Rate Topic: -----

#1 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

How to save listview into a text file

Posted 21 February 2013 - 10:21 AM

I used to be able to save the listbox below to a textfile, but I don't know how to add the value of the items in the list view to the object.

For the file below. I am able to put each items into the StockPrice x, which is working well
Now I try to learn about the list view, and I want to put the item into Y, but I don't know how to declare that the right way.
If I 'm able to get the stockprice into y, then I will remove the listbox, and use the listview only.

I hope I write enough detail.
                try
                {
                    var items = stockListView.Items;
                    System.IO.StreamWriter writer = new System.IO.StreamWriter(currentFile);  // open stream for writing
                    for (int i = 0; i < stockListBox.Items.Count; i++)
                    {
                        StockPrice x = (StockPrice)stockListBox.Items[i]; // working great
                        StockPrice y = (StockPrice)stockListView.Items[i]; // this doesn't work
                        writer.Write(x.CompanyName);
                        writer.Write("|");
                        writer.Write(x.Stock);
                        writer.Write("|");
                        writer.Write(x.OpeningPrice);
                        writer.Write("|");
                        writer.Write(x.HighPrice);
                        writer.Write("|");
                        writer.Write(x.LowPrice);
                        writer.Write("|");
                        writer.Write(x.ClosingPrice);
                        writer.Write("|");
                        writer.WriteLine(x.Date);
                    }
                    writer.Close();
                }


Is This A Good Question/Topic? 0
  • +

Replies To: How to save listview into a text file

#2 Julyuary  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 106
  • Joined: 20-December 11

Re: How to save listview into a text file

Posted 21 February 2013 - 10:28 AM

If you're saving an object, you might want to try using serialization? Try looking here on msdn http://msdn.microsof...b(v=vs.71).aspx
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5475
  • View blog
  • Posts: 11,761
  • Joined: 02-June 10

Re: How to save listview into a text file

Posted 21 February 2013 - 10:28 AM

Quote

08        StockPrice y = (StockPrice)stockListView.Items[i]; // this doesn't work


"Doesn't work" doesn't tell us much.
Do you get an error message?
Have you put in a breakpoint to make sure there even is an Item[i] in the ListView.Items collection?

Have you used breakpoints before to see the run-time values of variables?
What does this error message mean?[/
Was This Post Helpful? 0
  • +
  • -

#4 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

Re: How to save listview into a text file

Posted 21 February 2013 - 10:33 AM

Thank you both

View PosttlhIn`toq, on 21 February 2013 - 10:28 AM, said:

Quote

08        StockPrice y = (StockPrice)stockListView.Items[i]; // this doesn't work


"Doesn't work" doesn't tell us much.
Do you get an error message?
Have you put in a breakpoint to make sure there even is an Item[i] in the ListView.Items collection?

Have you used breakpoints before to see the run-time values of variables?
What does this error message mean?[/


Error: Can not convert form.listview to stockprice.

I am new to c# class, and we didn't go over the debug stuff. I am watching your breakpoint tutorial right now, so hang with me :)
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5475
  • View blog
  • Posts: 11,761
  • Joined: 02-June 10

Re: How to save listview into a text file

Posted 21 February 2013 - 10:42 AM

I would put this line between your 07 and 08

var tempitem = stockListView.Items[i];


Then put a breakpoint on the next line.
This would let you look at tempitem and see what the actual type is and the information it holds. From there you can plan your next move on how to cast it to a new StockPrice.

A ListBox can take objects of any type. But i *think* a Listview has to have a type of ListViewItem. So the compiler doesn't know how to convert that ListViewItem back to a StockPrice. You may have to make a converter for that. Or just make a new StockPrice and set its values from the ListViewItem, if that's simpler.
Was This Post Helpful? 0
  • +
  • -

#6 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

Re: How to save listview into a text file

Posted 21 February 2013 - 10:46 AM

It won't let me debug the current build, because I have an error on StockPrice y = (StockPrice)stockListView.Items[i];. So it only run the last build.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5475
  • View blog
  • Posts: 11,761
  • Joined: 02-June 10

Re: How to save listview into a text file

Posted 21 February 2013 - 11:10 AM

Then comment out the line with the error so the compiler is happy.
The point here is just to discover the type. Then you can work from there.
Was This Post Helpful? 1
  • +
  • -

#8 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

Re: How to save listview into a text file

Posted 21 February 2013 - 11:54 AM

Thank for the help. I learn something new about debugging today. It was very useful video. Anyway it wasn't easy to implement like the listbox, so I'll skip it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1