Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving values in a listbox

Status
Not open for further replies.

bb11

Programmer
Feb 23, 2005
5
AU
When a program is running and a value is added to a listbox. How do i make the value stay there even when the program is being closed and opened again
 
How are you adding the items to the list box?

Normally you would just click on the listbox in the designer and click on the "Items" property. Then type in your items there.
 
You can do that in many ways. One method is to use Serializable attribute.
Assume the listbox shows values of the object of MyClass. Then define an array list to store all the objects you add at run tinme in the listbox.
When the program is closed, use Serialize() to persist the list on the disk.
When the program opens then call Deserialize() to retreive the values and populate the list box.
Example:
Code:
[Serializable()]
public class MyObject
{
   // members to be dispalyed in the listbox
}
[Serializable()]
public class MyList
{
    ArrayList arr = new ArrayList(); // here will be stored the MyObject objects
}
//Create a MyList object:
MyList list = new MyList();
For each item added at run time in the listbox , create a MyObject object and add it in the list:
Code:
MyObject obj = new Myobject(...);
list.arr.Add(obj);
On application exit :
Code:
//Opens a file and serializes the object into it in binary format.
Stream stream = File.Open("listboxdata.xml", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, list);
stream.Close();
On application starting:
Code:
//Opens file "listboxdata.xml" and deserializes the object from it.
stream = File.Open("data.xml", FileMode.Open);
formatter = new BinaryFormatter();
MyList list = (MyList)formatter.Deserialize(stream);
stream.Close();
// Now use list object to populate the ListBox before dispalying it:
foreach (object obj in list.arr)
{
   MyObject myobj= (MyObject)object;
   // Add the myobj to the ListBox 
}
obislavu
 
Im sure there is a easier way to make the text in a listbox appear when starting and closing a program.What is the quickest way and simplest way?
 
You still have not sayed how you populate the listbox at all. Where do the values come from? From a textfile; Hardcoded; an Xml-File; a config-File,......?

I usually store values like these either in some xml File or in the config. On ProgramStart I Load all values from the file, and on ProgramClose i save all values back to the file.

Stephan
 
It is simply added from a text box.
There has to be a simple way to make it appear in the listbox when the program is closed and opened



private void button1_Click(object sender, EventArgs e)

{
listBox1.Items.Add(textBox1.Text);

}
 
Well if you want the values to be stored when the Program finishes or beginns you wil need to save them somewhere! Theres a couple of possibliliets, as mentioned in my previous post!

Store those values either in
- an ini File
- a plain text File
- a config File (very easy in .NET)
- a custom Xml File
- the registry

I dont think there is any other way. Sorry.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top