AndreasB
AndreasB

Reputation: 562

Save checkbox state to localsettings, windows 8

I'm currently trying to save the state of a checkbox (checked/unchecked) to ApplicationData's LocalSettings for my Modern UI app. I've been working on this problem for the last 6-7 hours, tried a ton of different approaches, but I believe I'm totally lost in my own code by now. My code so far:

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        if (localSettings.Values.ContainsKey("isFirstChecked") == false || cbFirst.IsChecked == false)
        {
            cbFirst.IsChecked = false;
            test.Text = "UnChecked";
        }
        else 
        {
            cbFirst.IsChecked = true;
            test.Text = "Checked";
        }
    }

    private void cbFirst_Checked(object sender, RoutedEventArgs e)
    {
        test.Text = "Checked";
        localSettings.Values["isFirstChecked"] = cbFirst.IsChecked = true;
    }

    private void cbFirst_Unchecked(object sender, RoutedEventArgs e)
    {
        test.Text = "UnChecked";
        var test2 = localSettings.Values["isFirstChecked"] = cbFirst.IsChecked = false;

    }

Like I've said already, I've tried several approaches to solve this problem, but none of them has managed to actually save both states, only one of them (which I hope some of you may know why, as I'm so lost at the moment).

For some reason, when I debug the "Checked" code is fired even though the checkbox is unchecked.

Upvotes: 3

Views: 935

Answers (2)

ZombieSheep
ZombieSheep

Reputation: 29953

The problem with your initial code is that you are checking different things in your condition. You are checking whether the setting you have stored exists, and then whether the checkbox is checked. What you aren't currently doing is getting the setting value if it exists. Try the following on load.

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    bool valueFromSettings = false;

    if (!localSettings.Values.ContainsKey("isFirstChecked"))
    {
        // if the setting doesn't exist, probably wise to create it here.
        // setting the default to "false", but you can change to true if that makes more sense.
        localSettings.Values.Add("isFirstChecked", false);
    }
    else
    {
        // read the value of the setting here.  
        // If we just created it, it should default to false (see above)
        valueFromSetting = ((bool)localSettings.Values["isFirstChecked"]);
    }

    if(valueFromSettings)
    {
        cbFirst.IsChecked = true;
        test.Text = "Checked";
    }
    else 
    {
        cbFirst.IsChecked = false;
        test.Text = "UnChecked";
    }
}

Then, you can use the handlers you previously defined (but notice the subtle changes - you don't need to check the value of the checkbox, but can assign the setting directly.)

private void cbFirst_Checked(object sender, RoutedEventArgs e)
{
    test.Text = "Checked";
    localSettings.Values["isFirstChecked"] = true;
}

private void cbFirst_Unchecked(object sender, RoutedEventArgs e)
{
    test.Text = "UnChecked";
    localSettings.Values["isFirstChecked"] = false;
} 

Upvotes: 2

burning_LEGION
burning_LEGION

Reputation: 13450

private void cbFirstCheckedChanged(object sender, EventArgs e)
{
    var checkBox = serder as CheckBox;
    if (checkBox == null) return;
    test.Text = checkBox.IsChecked ? "Checked" : "UnChecked";
    localSettings.Values["isFirstChecked"] = cbFirst.IsChecked
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    checkBox.IsChecked = localSettings.Values["isFirstChecked"];
}

Upvotes: 1

Related Questions