繁体   English   中英

C#:如何将组合框等项目列表保存到 .NET 设置文件?

[英]C#: How do you save a list of items like a Combobox to the .NET Settings file?

C#:如何将组合框等项目列表保存到 .NET 设置文件?

设置设计器允许您使用的唯一集合类型是 System.Collections.ArrayList。 如果您确实使用 ArrayList,则其所有元素的类型都必须是可序列化的(具有 [Serializable] 属性或实现 System.Runtime.Serialization.ISerializable。)

下面是一些代码,用于从设置中的 ArrayList(名为 cboCollection)获取数据到组合框并返回。

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

如果您在谈论应用程序用户设置,我会遍历组合框并将值保存在分隔字符串中:

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

如果上面的代码不完美,请原谅,这只是一个例子。

希望有帮助!

Windows 窗体对象不可序列化。 因此,您无法使用 binaryformatter 将它们序列化并存储在文件中。 您需要将组合框值手动存储在文件中。

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
      }
   }

您可以使用System.Collections.Specialized.StringCollection类型来保存 ComboBox 项目。 首先在设置设计器中创建这种类型的变量,例如 CboItems。

阅读项目:

if (Properties.Settings.Default.CboItems != null)
    comboBox1.Items.AddRange(Properties.Settings.Default.CboItems.Cast<string>().ToArray());

要保存项目:

var items = new System.Collections.Specialized.StringCollection();
items.AddRange(comboBox1.Items.Cast<string>().ToArray());
Properties.Settings.Default.CboItems = items;

Properties.Settings.Default.Save();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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