C #: How to save a list of elements, such as Combobox, into a .NET settings file?

C #: How to save a list of elements, such as Combobox, into a .NET settings file?

+5
source share
3 answers

The only type of collection that allows you to use the settings constructor is System.Collections.ArrayList. If you use an ArrayList, all of its element types must be serializable (have the [Serializable] attribute or implement System.Runtime.Serialization.ISerializable.)

Here is some code to get data from an ArrayList (named cboCollection) in the settings in the combo box and vice versa.

    private void Form1_Load(object sender, EventArgs e)
    {
        if (Settings.Default.cboCollection != null)
            this.comboBox1.Items.AddRange(Settings.Default.cboCollection.ToArray());
    }


    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        ArrayList arraylist = new ArrayList(this.comboBox1.Items);
        Settings.Default.cboCollection = arraylist;
        Settings.Default.Save();
    }

    //A button to add items to the ComboBox
    private int i;
    private void button1_Click(object sender, EventArgs e)
    {
        this.comboBox1.Items.Add(i++);
    }
+9
source

If you are talking about user preferences for the application, I would skip the combo box and save the values ​​in a separation line:

StringBuilder sb = new StringBuilder();
foreach(var item in combo.Items){
  sb.Append(item.ToString() + ";");
}
Properties.Settings.MyListSetting = sb.ToString();

, , , .

, !

+4

Windows Forms objects cannot be serialized. Therefore, you cannot serialize and store them in a binary formatted file. You need to save the combobox values ​​manually in a file.

string comboboxFileName = @"c:\workDir\settings.settings";

private void saveComboboxInFile (String comboboxFileName )
{
   //--------------------------------------------------------
   //- Store the combobox values in a file. 1 value = 1 line
   //--------------------------------------------------------
   try
    {
        using (StreamWriter comboboxsw = new StreamWriter(comboboxFileName))
        {
            foreach (var cfgitem in comboBox.Items)
            {
                comboboxsw.WriteLine(cfgitem);
            }
        } // End Using`
    }
    catch (Exception e)
    {
       //process exception
    }
}



private void reloadCombboxFromFile (string  comboboxFileName )
   {
    //-------------------------------------------------
    //- Read the values back into the combobox
    //------------------------------------------------- 
        try
        {
            using (StreamReader comboboxsr = new StreamReader(comboboxFileName))
            {
                 while (!comboboxsr.EndOfStream)
                 {
                      string itemread = comboboxsr.ReadLine();
                      comboBox.Items.Add(itemread);
                 }
            } // End Using
      }
      catch (DirectoryNotFoundException dnf)
      {
         // Exception Processing
      }
      catch (FileNotFoundException fnf)
      {
         // Exception Processing
      }
      catch (Exception e)
      {
         // Exception Processing
      }
   }
+1
source

All Articles