so, i want to save my listView Items as i save Location or Size of window in the Project settings.

for example:

private void Form1_Load(object sender, EventArgs e)
        {
            this.Location = Properties.Settings.Default.MyLoc;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.MyLoc = this.Location;
        }

how can i do that? if you can write me a code its will be grate, if not can you just explain how? thanks!

someone?!~!?!

Here you go. I did an example code for you. I save all listView items into a List<T>.
Each row of List is consisted of a string array.

This is how you save the data from listView:

List<string[]> list;
        private void PopulatingListView()
        {
            ListViewItem item1 = new ListViewItem("item1", 0);
            // Place a check mark next to the item.
            item1.Checked = true;
            item1.SubItems.Add("1");
            item1.SubItems.Add("2");
            item1.SubItems.Add("3");
            ListViewItem item2 = new ListViewItem("item2", 1);
            item2.SubItems.Add("4");
            item2.SubItems.Add("5");
            item2.SubItems.Add("6");
            ListViewItem item3 = new ListViewItem("item3", 0);
            // Place a check mark next to the item.
            item3.Checked = true;
            item3.SubItems.Add("7");
            item3.SubItems.Add("8");
            item3.SubItems.Add("9");

            // Create columns for the items and subitems.
            // Width of -2 indicates auto-size.
            listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
            listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
            listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
            listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center);

            listView1.View = View.Details;
            //Add the items to the ListView.
            listView1.Items.AddRange(new ListViewItem[] { item1, item2, item3 });
        }

        private void buttonSaveFromListView_Click(object sender, EventArgs e)
        {
            //getting all the values into a list
            list = new List<string[]>();
            foreach (ListViewItem lvi in listView1.Items)
            {
                string[] values = new string[] { lvi.Text, lvi.SubItems[1].Text, lvi.SubItems[2].Text, lvi.SubItems[3].Text };
                list.Add(values);
            }
        }

And this is how you populate the listView again from the List<T>:

private void buttonPopulateListView_Click(object sender, EventArgs e)
        {
            if (listView1.Items.Count > 0)
            {
                listView1.Items.Clear();
                foreach (string[] item in list)
                {
                    ListViewItem lvi = new ListViewItem(item[0]);
                    lvi.SubItems.Add(item[1]);
                    lvi.SubItems.Add(item[2]);
                    lvi.SubItems.Add(item[3]);
                    listView1.Items.Add(lvi);
                }
            }

Take some time toi study. The code works well.
I hope this is what you have been looking for..

bye,
Mitja

it's not what i ment,
i have a program with button that add items.
now i want that:

when i close(Form1_Closing) the program its will save that items as they are after i added them.
and when i open the program again(Form1_Load) i want it will show those items.

It will save the item to where, and load from from where?
Anyway, you can still use my code from above, you only need to modify it a bit.
If you are not sure how, let me know, I`ll show you, but you have to tell me where to save the data (or to database, or into some file (text one)).

Mitja

ohh you got me all wrong. hh i dont want to save it on a text File or something like that i just want to save it as string in the program that all, in the list view.

like this, when the program is running:

1) i add item named "abcd"
2) i close the program
3) i open the program and i see the item "abcd" in the list view.

Do you mean to close the project (completey)? If so, you have to save the data from listView somewhere, or into some file (xml, text), or into database. Otherwise, how will you retrive the data? When program is closed, all data gets lost. So the only way to retreive them back is to get them from somewhere were they were saved before - you get the point now?

ohh yhee.. ok i didnt know that.. so can you help me do that with XML file/?

i made something like that, can you help me with that?

String itemXml = "items.xml";
        List<String> listViewItems = new List<String>();
        XmlDocument items = new XmlDocument();
        CultureInfo currentCulture;
        public Form1()
        {
            InitializeComponent();
            currentCulture = CultureInfo.CurrentCulture;
        }

        private void setVisibility()
        {
            if (!File.Exists(settingsXml))
            {
                XmlElement r = items.CreateElement("items");
                items.AppendChild(r);
                XmlElement el;

                el = items.CreateElement(listView1.Items.ToString());
                el.InnerText = listView1.Items.ToString();
                r.AppendChild(el);
            }

            else
            {
                items.Load(settingsXml);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            setVisibility();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            settings.Save(settingsXml);
        }

I would better suggest you to start with using text files, its way simplier and as efficient as xml (if not even more in your case, becuase you dont have millions of data).

You can use streamWriter to write into txt filen and streamReader to read out of it.

// *** Read from file ***
--------------------------

// Specify file, instructions, and privelegdes
file = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Read);

// Create a new stream to read from a file
StreamReader sr = new StreamReader(file);

// Read contents of file into a string
string s = sr.ReadToEnd();

// Close StreamReader
sr.Close();

// Close file
file.Close();



// *** Write to file ***
--------------------------

// Specify file, instructions, and privelegdes
FileStream file = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Write);
            
// Create a new stream to write to the file
StreamWriter sw = new StreamWriter(file);

// Write a string to the file
sw.Write("Hello file system world!");

// Close StreamWriter
sw.Close();

// Close file
file.Close();

Well no matter, I can not do it, thanks anyway for your help!

I think its the concept your are struggling with, its just as important to know how code works, as it is to know how to use it. Before you jump into something complicated it helps to write some pseudo code showing the steps involved in a process before you ever write any real code.

Also, for small controls, like a textbox, its fine to use the control to store the data like a string. and not save it anywhere else. But when you are working with a good amount of data that you need to manipulate. It helps to create a List of objects and just use the listview control to display that data. not keep track of it.

basic pseudo:
onLoad
- Create a List<myObject> to hold your working data.
- check for a saved file, and populate that List with data from it.
- populate a listview with the data from your List.

onItemAdded
- add the item to the List.
- add the item to the listview.

onClose
- save the List to a file

It's a good practice to write support methods, like a method that takes an List<t> and populate a listview the way you like it. and of course methods that load and save the data, like earlier ones in this thread. I personally like XMLSerialization, but plain text is faster. Its just more complicated and it doesn't save your data as exact.

Start by making a little app that saves the value of a textbox and then reopens it. if you have a particular question, post back, if we see your code, we can explain why it doesn't work, and what you need to do to make it.

Don't give up. Coding is fun!

this is my original code. http://www.mediafire.com/?0wa3s8wynomjzfv
i wrote some stuff there. you will see.
i realy need that help from you because i need to finished that program until next week
thanks very much!

OK, This is definitely homework. No doubt about it. If you are really serious about programming then something this simple should really be figured out on your own through research on the web.

if you studied the XML classes in the .net framework you wouldn't have any problem with this. But personally I feel that manually creating a xml document is ridiculous. So I use XML serialization.

After all the help that the rest of the community here as provided, you should have been able to at lest hack up something that worked. But i have edited your project making minimal changes as possible, commenting all the changes, but still making the application work well. hopefully you will read through it and take the time to understand all the changes that I made. With programming there is NEVER only one way to do something. 100 programmers are likely to come up with 150 different ways to do the same thing.

If you have any questions about how or why just post back.

wooow!! thank you sooooooooo much!! i realy appreciate it!!! you the best!!!

i don't know why it do that:

i make the code that i gave just to be more clean. but my real project it other project. so i put all those stuff in my code and there is no problem except from one.

in the XMLSaver and in the AddForm it's make problem with 'myObject'

did you make sure to create a myObject class in your other project.

They are based on a class that represents the myObject object. It is defined at the end of the main forms .cs file

yes i did.
in the Errors the Description is: are you missing a using directive or an assembly reference?

and i think its with the reference so what should i add?

At the top of the class file that contains the XMLSaver there should be using directives for the serialization of xml using System.Xml.Serialization; but if you just copied the entire file, then the namespace declaration just below that probably still has the old project namespace.

just change namespace WinFormsApplicatin1.Utilities //or whatever it is

to namespace Utilities should work then

i already have them.. :\ never mind i will just copy the project to new project and then its might be work.

why don't you just upload the code to daniweb, click advanced editor and click manage attachements. anyway, I'll haev a look

i tried.. it was wrote me that the file is unable to download or something like that..

Ok, the only problem was that you created the myObject class as an internal class of one of your forms. This made it not available to all the other classes in the application. I simple cut it and pasted it into a new class file, in the root of the namespace and it compiles fine.

thankssssssssssssss!!!!!!!!!!!!!!!!!!!!!!!!!! you are the best!!!!!!!!!!!!!!!!!!!!!!!!!
i don't know how to thank you!! thank you thank you thank you!!!!!!!!!!!!

you could mark this thread as solved using the mark as solved link above the quick reply box. That way I actually get credit for helping you.

there is an option to delete item from the XML FILE?

because i have 'Delete item button from the listView' and i want that will be delete it also from the XML file..

and if there's no way to do that. maybe there is possible way? like to delete the XML file and then to make it again? like in the program:

1) i write something
2) i close the program
3) i open the program and i see the items in the listView
4) i delete items as i want.
5) i close the program
6) i open the program again and i see the changes// (if i delete 3/6 items i will see the rest items(3 items)

you could mark this thread as solved using the mark as solved link above the quick reply box. That way I actually get credit for helping you.

Its about time :)

i marked it as solved but i change it to unsolved because i have one more question

there is an option to delete item from the XML FILE?

because i have 'Delete item button from the listView' and i want that will be delete it also from the XML file..

and if there's no way to do that. maybe there is possible way? like to delete the XML file and then to make it again? like in the program:

1) i write something
2) i close the program
3) i open the program and i see the items in the listView
4) i delete items as i want.
5) i close the program
6) i open the program again and i see the changes// (if i delete 3/6 items i will see the rest items(3 items)

SORRY, but PLEASE.... START ANOTHER THREAD!
You question has been answered plenty of times. Even from my side.
Hope you understand the issue.

Mitja

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.