jsls
jsls

Reputation: 261

How to save a new item in a checkboxList after closing?

I want the user to be able to dynamically add an item in a checkboxlist and it ll be saved after the winform is closed. If he opens it later the item will still be here.

I'm quite new on c# and i used to use Settings.settings to save things after closing but that way I only manage to save the name of the new checkboxlist and not the action that a new checkboxlist is added.

private void additem_Click(object sender, EventArgs e)
        {
Form2 form2 = new Form2();
form2.ShowDialog(); //a new form to add the name of the new checkboxlist
string newitem = form2.textBox1.Text; // take the string from the form2
checklistBox1.Items.Add(newitem)
        }

I want to save it after closing but I can't save an action with Settings. Is it possible otherwise?

Upvotes: 1

Views: 84

Answers (2)

RomCoo
RomCoo

Reputation: 1893

It is possibleto save it in Settings:

Add a StringCollection with the name "ItemNames" to the Settings.settings of your project. Each time the user adds an item, call:

if (Properties.Settings.Default.ItemNames == null)
    Properties.Settings.Default.ItemNames = 
        new System.Collections.Specialized.StringCollection();
Properties.Settings.Default.ItemNames.Add(newitem);

At the end you have to save your settings: Properties.Settings.Default.Save();

When initializing the form on the next start, load it again and fill the checkboxList dynamically depending on the value:

foreach (string item in Properties.Settings.Default.Setting)
{
    checklistBox1.Items.Add(item);
}

Edit: Added suggestion of jsls

Upvotes: 1

Tommy
Tommy

Reputation: 3124

Just save the items into a file.

private void button1_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2();
    form2.ShowDialog();
    string newitem = form2.textBox1.Text;
    checklistBox1.Items.Add(newitem);
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    var saveItems = checkedListBox1.Items.OfType<string>().Select((item, index) =>
    {
        var itemChecked = checkedListBox1.GetItemChecked(index);
        return new SaveItem { Text = item, Checked = itemChecked };
    }).ToArray(); ;

    var serializer = new XmlSerializer(typeof(SaveItem[]));
    using (var writeFile = File.OpenWrite("items.xml"))
    {
        serializer.Serialize(writeFile, saveItems);
    }
}
public class SaveItem
{
    public string Text { get; set; }
    public bool Checked { get; set; }
}
private void Form1_Load(object sender, EventArgs e)
{
    if (File.Exists("items.xml"))
    {
        var serializer = new XmlSerializer(typeof(SaveItem[]));
        using (var readFile = File.OpenRead("items.xml"))
        {
            var saveItems = (SaveItem[])serializer.Deserialize(readFile);
            foreach (var saveItem in saveItems)
            {
                checkedListBox1.Items.Add(saveItem.Text, saveItem.Checked);
            }
        }
    }
}

Upvotes: 1

Related Questions