【问题标题】:How to save a new item in a checkboxList after closing?关闭后如何在checkboxList中保存新项目?
【发布时间】:2016-06-15 15:24:59
【问题描述】:

我希望用户能够在复选框列表中动态添加一个项目,并且它将在 winform 关闭后保存。如果他稍后打开它,该项目仍然会在这里。

我是 C# 的新手,我过去常常在关闭后使用 Settings.settings 保存内容,但这样我只能保存新复选框列表的名称,而不是添加新复选框列表的操作。

private void additem_Click(object sender, EventArgs e)
        {
Form2 form2 = new Form2();
form2.ShowDialog(); //a new form to add the name of the new checkboxlist
string newitem = form2.textBox1.Text; // take the string from the form2
checklistBox1.Items.Add(newitem)
        }

我想在关闭后保存它,但我无法使用“设置”保存操作。有没有可能?

【问题讨论】:

    标签: c# winforms save


    【解决方案1】:

    可以在设置中保存:

    将名为“ItemNames”的StringCollection 添加到项目的Settings.settings。每次用户添加一个项目时,调用:

    if (Properties.Settings.Default.ItemNames == null)
        Properties.Settings.Default.ItemNames = 
            new System.Collections.Specialized.StringCollection();
    Properties.Settings.Default.ItemNames.Add(newitem);
    

    最后你必须保存你的设置:Properties.Settings.Default.Save();

    下次启动时初始化表单时,再次加载并根据值动态填写checkboxList

    foreach (string item in Properties.Settings.Default.Setting)
    {
        checklistBox1.Items.Add(item);
    }
    

    编辑:添加了 jsls 的建议

    【讨论】:

    • 工作正常,但您应该编辑并添加 if (Settings.Default.ItemNames == null) { Settings.Default.ItemNames = new System.Collections.Specialized.StringCollection(); Settings.Default.Save(); } Properties.Settings.Default.ItemNames.Add(nouvel);否则会出现错误(您需要初始化字符串集合)但谢谢您工作正常:)
    【解决方案2】:

    只需将项目保存到文件中即可。

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.ShowDialog();
        string newitem = form2.textBox1.Text;
        checklistBox1.Items.Add(newitem);
    }
    
    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        var saveItems = checkedListBox1.Items.OfType<string>().Select((item, index) =>
        {
            var itemChecked = checkedListBox1.GetItemChecked(index);
            return new SaveItem { Text = item, Checked = itemChecked };
        }).ToArray(); ;
    
        var serializer = new XmlSerializer(typeof(SaveItem[]));
        using (var writeFile = File.OpenWrite("items.xml"))
        {
            serializer.Serialize(writeFile, saveItems);
        }
    }
    public class SaveItem
    {
        public string Text { get; set; }
        public bool Checked { get; set; }
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        if (File.Exists("items.xml"))
        {
            var serializer = new XmlSerializer(typeof(SaveItem[]));
            using (var readFile = File.OpenRead("items.xml"))
            {
                var saveItems = (SaveItem[])serializer.Deserialize(readFile);
                foreach (var saveItem in saveItems)
                {
                    checkedListBox1.Items.Add(saveItem.Text, saveItem.Checked);
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      相关资源
      最近更新 更多