繁体   English   中英

保存复选框状态

[英]Save the CheckBox state

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

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

可能看一下设置文件以进行简单的启动。

为此,您需要自己记录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);

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

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

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

我会使用这样的设置:

假设已经创建了一个名为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;
}

如果您使用启用了 Web 应用程序 cookie 并将信息存储在 cookie 中,那么这是可能的。

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

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

在 C# 中,您可以使用设置文件。 可以在此处找到如何使用它的信息: http : //msdn.microsoft.com/en-us/library/aa730869%28v=vs.80%29.aspx

如果您想将其保存到注册表中,您可以执行以下操作

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);
        }
    }   
}           

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM