将复选框状态保存到设置文件 [英] save checkbox state to settings file
问题描述
我已经创建了设置文件 3 bool vars scoope用户名为skype_mute和skype_vol_down ....
i already have settings file create with 3 bool vars scoope user named skype_mute and skype_vol_down....
我需要将3个复选框状态保存到设置中提交
and i need to save the 3 checkbox states to the settings file the
我有一个lkistcheckbox有3个复选框,frist是独立的,第二个和第三个是如果2是maked 3rd没有标记,反之亦然如何将值保存到设置文件。
i have a lkistcheckbox with 3 checkboxes and the frist is independent and the second and 3rd is if 2 is maked 3rd is unmarked and vice versa how to save the values to the settings file.
之前我使用普通复选框并保存我只需使用:
previously i use normal checkbox and to save i just use:
//in preferences form 3
save button // should save all 3 checkboxes state however this is for normal checkbox and not list checkbox
private void btn_guardar_pref_Click(object sender, EventArgs e)
{
Settings1 set = new Settings1();
set.cb_MinTarefas = checkBox1.Checked;
set.Save();
this.Close();
}
// check checkbox state before opening form
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
Settings1 set = new Settings1();
checkBox1.Checked = set.cb_MinTarefas ;
// new code to checkbox list 3 checkboxes frist is independent and 2nd and 3rd is just allow select or 3rd or 2nd
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
CheckedListBox.CheckedIndexCollection checkedIndices = checkedListBox1.CheckedIndices;
if (checkedIndices.Contains(0) && checkedIndices.Count == 2 && (int)e.NewValue == 1)
{
checkedListBox1.SetItemChecked((e.Index == 1 ? 2 : 1), false);
}
if (!checkedIndices.Contains(0) && checkedIndices.Count > 0 && (int)e.NewValue == 1)
{
checkedListBox1.SetItemChecked((e.Index == 1 ? 2 : 1), false);
}
//in form1 main form
private void Form1_Load(object sender, EventArgs e)
{
VerificarActualizaçoesWebradio();
Settings1 set = new Settings1();
bool checkedValue = cb_MinTarefas ;
so the checkbox names are 1- cb_MinTarefas 2nd is cb_BaixarVolSkype 3rd is cb_SkypeMute
so how can i do this?
推荐答案
>我需要将3个复选框状态保存到设置文件
您可以使用数据绑定。
$以下b $ b是两个方法的示例:v1和v2。
you can use databinding.
below is an example with two approaches: v1 and v2.
using System.Data;
using System.IO;
using System.Windows.Forms;
using System.Xml.Linq;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
var xml = @"<root>
<data checked='true' />
</root>";
// v1: load xml and binding to xelement
var xe = XElement.Parse(xml);
var cb1 = new CheckBox { Parent = this, Dock = DockStyle.Top, Text = "XElement" };
cb1.DataBindings.Add("Checked", xe.Element("data").Attribute("checked"), "Value");
// v2: load xml and binding to dataset
var ds = new DataSet();
ds.ReadXml(new StringReader(xml));
var cb2 = new CheckBox { Parent = this, Dock = DockStyle.Top, Text = "DataSet" };
cb2.DataBindings.Add("Checked", ds, "data.checked");
// test
this.Menu = new MainMenu();
this.Menu.MenuItems.Add("trace", (s, e) =>
{
foreach(Control c in this.Controls)
foreach(Binding b in c.DataBindings)
b.WriteValue();
// todo: save to file
MessageBox.Show("DataSet: " + ds.GetXml() + "\n\n XElement: " + xe);
});
}
}
}
这篇关于将复选框状态保存到设置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!