checkedListBox 选中的项如何保存。 checkedListBox 设置了红色,黄色,蓝色,绿色,4个选项,我选中任意几个项,保存后,以后软件重新打开,读取我前一次选中的几个项,不用数据库绑定。请问是否用文本保存,然后软件打开后,读取文本。这个代码怎么写 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 个人觉得可疑用xml文件存储信息,下次登陆的时候自动获取 记录一下选中的索引就行,或者某个唯一的属性,下次打开时读取这些信息用xml 恩,用xml就搞定了bdmh叫我玩delphi好不呵呵 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"); } 来上个测试通过的代码,我是冲着分数来的呵呵: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下测试通过。楼主可以结贴了 可以用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"])); }}}} 大哥们给下思路啊,在线等,谁顶都有分,先来先得,1小时后来的没分 帮我看看类似这样的背景图片要怎么样显示,没试过,请具体给个小例子的代码解答 要注意哪些? [小梦涂鸦]--一个DataGrid的设计及实现全过程(再不能发就算了,M的) 怎么编写C#控件呢,有没有这种专题链接 System.Windows名称空间引用有点怪问题 用C#怎么更改excel其中的一个表的名称 c# 弹出层 高分讨论:使用C#开发OLTP项目优缺点征集!(答者必得分) vs2017创建core2.0遇到两个问题 序列化和反序列化 web.config怎么配置?使用户打开的页面强行跳转到login.aspx
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");
}
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下测试通过。楼主可以结贴了
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"])); }
}
}
}