How do I set a C# checkbox property to "unchecked" for the purpose of saving a setting?
How do I set a C# checkbox property to "unchecked" for the purpose of saving a setting? I'm using the .NET settings feature to store the state of a checkbox. When 开发者_运维百科the form loads it grabs that setting and checks or un-checks that checkbox accordingly. The code below works to set the setting to true. But there is no definition for unchecked in this context.
Can anyone help?
Thanks.
PS. This is using VS2010, C# and .NET 3.5
chkBackup.Checked will contain false if the box is unchecked, so you can just always use chkBackup.Checked to get what you need. The following code should be all that you ever need -
private void chkBackup_CheckChanged(object sender, EventArgs e)
{
Properties.Settings.Default.Backup = chkBackup.Checked;
Properties.Settings.Default.Save();
}
Use (chkBackup.Checked == true) as the value:
Properties.Settings.Default.Backup = (chkBackup.Checked == true);
Properties.Settings.Default.Save();
This also gets rid of your enclosing if statement, and will work with properties that are nullable bools (bool?)
精彩评论