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

private void btConfirm_Click(object sender, EventArgs e)

            String name;
            String address;
            String number;

            name = tbName.Text;
            address = tbAddress.Text;
            number = tbNumber.Text;

            StreamWriter SW = new StreamWriter("Listview.txt");
            ListViewItem Item = new ListViewItem();


            StreamReader SR = new StreamReader("Listview.txt");


            name = SR.ReadLine();
            address = SR.ReadLine();
            number = SR.ReadLine();

            Item.Text = name;




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.

This article has been dead for over six months. Start a new discussion instead.