locked
Combobox saving RRS feed

  • Question

  • So I've seen some tutorials and responds on this with a code like this to save Listbox items in a C# program:

                Properties.Settings.Default.Themes.Clear();

                foreach (object item in cbxTheme.Items)

                {
                    Properties.Settings.Default.Themes.Add(item);
                }
                Properties.Settings.Default.Save();

    only the problem is that is somehow gives an error at the word "item" wich is at the line between the {}'s.

    I want to store and restore items of a combobox but the word item isn't working. I changed it to a string once and that worked but somehow the combobox didn't work anymore. Can you help me with how I am supposed to fix the problem of 1 word?

    Sunday, April 29, 2018 1:32 PM

All replies

  • Btw, I forgot to tell but the error is that it can't convert an object to string.
    Sunday, April 29, 2018 1:33 PM
  • Not sure what's going on with your code but when I creating a setting as System.Collections.Specialized.StringCollection with the following code all works as expected.

    using System;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                FormClosing += Form1_FormClosing;
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                Properties.Settings.Default.ComboBox1Items.Clear();
                Properties.Settings.Default.ComboBox1Items
                    .AddRange(comboBox1.Items.Cast<string>().ToArray());
    
                Properties.Settings.Default.Save();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
                if (Properties.Settings.Default.ComboBox1Items.Count >0)
                {
                    comboBox1.Items.Clear();
    
                    comboBox1.Items.AddRange(
                        Properties.Settings
                            .Default.ComboBox1Items.Cast<string>().ToArray());
    
                    Properties.Settings.Default.ComboBox1Items.Clear();
                    Properties.Settings.Default.Save();
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                comboBox1.Items.Add(textBox1.Text);
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, April 29, 2018 9:37 PM