1

How to add all items from listbox to settings? I tried by arraylist but I had error:

Object reference not set to an instance of an object.

I have no idea how to do this. Settings:

Name - listboxItems, Type - System.Collections.ArrayList, Scope - Application

private void button9_Click(object sender, EventArgs e)
        {
            //Save listBox Items
            foreach (object item in listBox1.Items)
            {
                Properties.Settings.Default.listboxItems.Add(item);
            }

            Properties.Settings.Default.Save();
        }
denied
  • 311
  • 1
  • 5
  • 18

1 Answers1

3

Maybe the listboxItems entry in the Settings is null. Try:

var newList = new ArrayList();
foreach (object item in listBox1.Items)
{
    newList.Add(item);
}

Properties.Settings.Default.listboxItems = newList
Properties.Settings.Default.Save();

If you want something typed in your settings (i.e. string[], take a look at this answer.

Community
  • 1
  • 1
Matt
  • 2,682
  • 1
  • 17
  • 24