保存CheckBox状态

时间:2012-01-23 16:09:58

标签: c# checkbox

我需要知道是否可以在C#中保存CheckBox的状态?我的意思是,如果我检查CheckBox并关闭程序,一旦我重新启动程序,CheckBox仍然会保持检查状态。有可能吗?

7 个答案:

答案 0 :(得分:3)

这是一个普遍的问题。你需要以某种方式自己序列化状态,但是如何以及在哪里取决于很多事情。

可能需要查看Settings file才能获得简单的开始。

答案 1 :(得分:1)

为此,您需要自己记录CheckBox的状态。例如,您可以将值存储在包含应用程序UI状态的XML文档中。例如,在非常简单形式中,您可以执行以下操作:

// ... as the application is closing ...
// Store the state of the check box
System.IO.File.WriteAllText(@"C:\AppFile.txt", this.CheckBox1.IsChecked.ToString());

// ...

// ... as the application is being initialized ...
// Read the state of the check box
string value = System.IO.File.ReadAllText(@"C:\AppFile.txt");
this.CheckBox1.IsChecked = bool.Parse(value);

如您所见,这只是将值存储在文件中,并在初始化期间将其读回。这不是一个很好的方法,但它展示了一个可能的过程。

答案 2 :(得分:1)

最简单的方法是使用配置XML文件。您可以通过visual studio轻松添加它,无需使用注册表,如果应用程序是可移植的,则可以使用它,因为设置随程序一起保存。有关如何设置的教程如下:

http://www.sorrowman.org/c-sharp-programmer/save-user-settings.html

答案 3 :(得分:0)

如果您正在使用Web应用程序cookie并将信息存储在cookie中,则可以。

您可以结帐http://www.daniweb.com/web-development/aspnet/threads/30505

http://asp.net-tutorials.com/state/cookies/

答案 4 :(得分:0)

在C#中,您可以使用“设置”文件。有关如何使用它的信息,请访问:http://msdn.microsoft.com/en-us/library/aa730869%28v=vs.80%29.aspx

答案 5 :(得分:0)

如果您想将此保存到注册表中,您可以执行类似这样的操作

RegistryKey Regkey = "HKEY_CURRENT_USER\\Software\\MyApplication";
RegKey.SetValue("Checkbox", Checkbox.Checked);

但我个人将其保存到.Config文件

以下是如果您愿意,可以使用配置文件进行操作的示例

private static string getConfigFilePath()
{
    return Assembly.GetExecutingAssembly().Location + ".config";
}
private static XmlDocument loadConfigDocument()
{
    XmlDocument docx = null;
    try
    {
        docx = new XmlDocument();
        docx.Load(getConfigFilePath());
        return docx;
    }
    catch (System.IO.FileNotFoundException e)
    {
        throw new Exception("No configuration file found.", e);
    }
}
private void rem_CheckedChanged(object sender, EventArgs e)
{
    if (rem.Checked == true)
    {
        rem.CheckState = CheckState.Checked;
        System.Xml.XmlDocument docx = new System.Xml.XmlDocument();
        docx = loadConfigDocument();
        System.Xml.XmlNode node;
        node = docx.SelectSingleNode("//appsettings");
        try
        {
            string key = "rem.checked";
            string value = "true";
            XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
            if (elem != null)
            {
                elem.SetAttribute("value", value);
            }
            else
            {
               elem = docx.CreateElement("add");
                elem.SetAttribute("key", key);
                elem.SetAttribute("value", value);
                node.AppendChild(elem);
            }
            docx.Save(getConfigFilePath());
        }
        catch (Exception e2)
        {
            MessageBox.Show(e2.Message);
        }
    }   
}           

答案 6 :(得分:0)

我会这样使用设置:

假设已创建名为boxChecked的布尔设置。

//if user checks box
Properties.Settings.Default.boxChecked = true;
Properties.Settings.Default.Save();

//...

//when the program loads
if(Properties.Settings.Default.boxChecked)
{
    checkBox1.Checked = true;
}
else
{
    checkBox1.Checked = false;
}

相关问题