checkedListBox 设置了红色,黄色,蓝色,绿色,4个选项,我选中任意几个项,保存后,以后软件重新打开,读取我前一次选中的几个项,不用数据库绑定。
请问是否用文本保存,然后软件打开后,读取文本。这个代码怎么写

解决方案 »

  1.   

    个人觉得可疑用xml文件存储信息,下次登陆的时候自动获取
      

  2.   

    记录一下选中的索引就行,或者某个唯一的属性,下次打开时读取这些信息用xml
      

  3.   

    恩,用xml就搞定了bdmh叫我玩delphi好不呵呵
      

  4.   


            private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("color.xml");
                doc.DocumentElement.SelectSingleNode("checkedcolor").InnerText = checkedListBox1.SelectedItem.ToString()+",";
                doc.Save("color.xml");
            }
      

  5.   

    来上个测试通过的代码,我是冲着分数来的呵呵:using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    namespace 用完删除
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(Application.StartupPath+@"\color.xml");
                string[] getxmlcolor = doc.DocumentElement.SelectSingleNode("checkedcolor").InnerText.Split(',');
                foreach (string str in getxmlcolor)
                {
                    checkedListBox1.SetItemChecked(int.Parse(str), true);
                }
            }        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(Application.StartupPath+@"\color.xml");
                string xmlupdate = string.Empty;
                for (int i = 0; i < checkedListBox1.Items.Count; i++)
                {
                    if (checkedListBox1.GetItemChecked(i))
                    {
                        xmlupdate += i.ToString() + ",";
                    }
                }
                if (xmlupdate.Length > 0)
                {
                    xmlupdate = xmlupdate.Substring(0, xmlupdate.Length - 1);
                }
                else
                {
                    xmlupdate = "";
                }
                doc.DocumentElement.SelectSingleNode("checkedcolor").InnerText = xmlupdate;
                doc.Save(Application.StartupPath+@"\color.xml");
            }
        }
    }保存颜色的xml文件:<?xml version="1.0" encoding="utf-8"?>
    <colors>
      <checkedcolor>0,2,3</checkedcolor>
    </colors>在vs2010下测试通过。楼主可以结贴了
      

  6.   

    可以用setting来做
    namespace excusegen.Properties {
    sealed partial class Settings : ApplicationSettingsBase {
    static Settings defaultInstance =
    ((Settings)(ApplicationSettingsBase.Synchronized(new Settings( ))));
    public static Settings Default {
    get { return defaultInstance; }
    }
    [UserScopedSettingAttribute()]
    [DefaultSettingValueAttribute("")]
    public string LastExcuse {
    get { return ((string)(this["LastExcuse"])); }
    set { this["LastExcuse"] = value; }
    }
    [ApplicationScopedSettingAttribute()]
    [DefaultSettingValueAttribute("True")]
    public bool ExcludeAnimalExcuses {get { return ((bool)(this["ExcludeAnimalExcuses"])); }
    }
    }
    }