0
votes

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.

enter image description here

Can anyone help?

Thanks.

PS. This is using VS2010, C# and .NET 3.5

2

2 Answers

3
votes

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();
}
1
votes

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?)