943,106 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 3149
  • C# RSS
Feb 8th, 2010
0

Listview and saving data to a text file

Expand Post »
Hi ,

My program at the moment has 3 textboxes , 1 button and 1 listview.

I've added 3 columns named "Name" "Address" and "Number" to the Listview.

What I have done so far is I can enter text into the 3 textboxes , I click the button and the text I enter appears in the Listview.

But I can't seem to be able to save the text I enter into a text file , what I would also like to do is to be able to enter text into the Listview , re run the program and the text I entered before to be still visible in the Listview , I just can't seem to make the data stick to it at the moment.

If you could steer me in the right direction I would really appreciate it
C# Syntax (Toggle Plain Text)
  1. private void btConfirm_Click(object sender, EventArgs e)
  2. {
  3.  
  4.  
  5. String name;
  6. String address;
  7. String number;
  8.  
  9. name = tbName.Text;
  10. address = tbAddress.Text;
  11. number = tbNumber.Text;
  12.  
  13. StreamWriter SW = new StreamWriter("Listview.txt");
  14. ListViewItem Item = new ListViewItem();
  15.  
  16. SW.WriteLine(name);
  17. SW.WriteLine(address);
  18. SW.WriteLine(number);
  19. SW.Flush();
  20. SW.Close();
  21.  
  22. StreamReader SR = new StreamReader("Listview.txt");
  23.  
  24.  
  25.  
  26. name = SR.ReadLine();
  27. address = SR.ReadLine();
  28. number = SR.ReadLine();
  29.  
  30. Item.Text = name;
  31. Item.SubItems.Add(address);
  32. Item.SubItems.Add(number);
  33.  
  34. listView1.Items.Add(Item);
  35.  
  36.  
  37. SR.Close();
  38.  
  39. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
KeenLearner2010 is offline Offline
1 posts
since Feb 2010
Feb 9th, 2010
1
Re: Listview and saving data to a text file
Firstly, you are not specifying a path for your text file, just a file name, this is bad and can lead to an error. you should always specify a full path.

this write the data to a text file, then loads it back and uses its contents to fill the listview, if this code update the listview then you ARE saving it to the file.

You need to separate this, on form close loop through the listview items and write them to a file. on form load loop through the text file and load it to the listview.

but there is a more OOP way to go about it. Create an object that represents a contact, with 3 strings name, number, and address, as a property. Then create a list of those objects and use that as a source for your listview and serialize it to a XML file on save, deserialize it on open.
Reputation Points: 442
Solved Threads: 89
Master Poster
Diamonddrake is offline Offline
721 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Control characters in a file.
Next Thread in C# Forum Timeline: Determine easter for a given year





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC