简体   繁体   中英

How to add listbox items to settings?

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();
        }

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 (ie string[] , take a look at this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM