Home > Software design >  Why when saving to Settings radio button states it keep loading the first radio button state and not
Why when saving to Settings radio button states it keep loading the first radio button state and not

Time:08-13

When loading :

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
radioButtonWatchDirectory.IsChecked = Properties.Settings.Default.RadioButtonWatchDirectory;
radioButtonWatchFile.IsChecked = Properties.Settings.Default.RadioButtonWatchFile;
checkBoxIncludeSubdirectories.IsChecked = Properties.Settings.Default.IncludeSubDirectories;
textBoxFileDirectory.Text = Properties.Settings.Default.BrowseFolderDialog;
        }

When setting and saving :

private void radioButtonWatchFile_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.RadioButtonWatchFile = (bool)radioButtonWatchFile.IsChecked;
            Properties.Settings.Default.Save();
        }

    

private void radioButtonWatchDirectory_Checked(object sender, RoutedEventArgs e)
    {
        Properties.Settings.Default.RadioButtonWatchDirectory = (bool)radioButtonWatchDirectory.IsChecked;
        Properties.Settings.Default.Save();
    }

When running the application once i checked true the Watch File radio button no matter if i check the Watch Directory radio button next time i will run the application the Watch File radio button will be checked. like it's not remembering changing it to the Watch Directory radio button.

The Include Subdirectories is working fine.

Radio buttons

I tried this when loading :

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            radioButtonWatchDirectory.IsChecked = Properties.Settings.Default.RadiosTesting;
            radioButtonWatchFile.IsChecked = Properties.Settings.Default.RadiosTesting;
            checkBoxIncludeSubdirectories.IsChecked = Properties.Settings.Default.IncludeSubDirectories;
            textBoxFileDirectory.Text = Properties.Settings.Default.BrowseFolderDialog;
        }

When saving :

private void RadiosTests(object sender, RoutedEventArgs e)
        {
            if((bool)radioButtonWatchFile.IsChecked)
            {
                Properties.Settings.Default.RadiosTesting = (bool)radioButtonWatchFile.IsChecked;
                Properties.Settings.Default.Save();
            }

            if((bool)radioButtonWatchDirectory.IsChecked)
            {
                Properties.Settings.Default.RadiosTesting = (bool)radioButtonWatchDirectory.IsChecked;
                Properties.Settings.Default.Save();
            }
        }

but always the radioButtonWatchFile is checked when running the application again.

CodePudding user response:

When a radio button is unchecked, the Checked event is not fired again. So, when you select another option, the setting of the previously checked option is not changed.

You could additionally subscribe to the Unchecked event of each radiobutton in order to manage the state in your settings object.

However, it might be a better idea to save some consolidated state of the currently selected option, instead of saving each radiobutton state individually.

Answer to why it always selects the file option: because both IsChecked properties are assigned true but only one can legally be selected, the selection of the first radiobutton is reset when the second one is initialized.

If you would revert the order of loading, the other button would be selected (this code is not a solution, just a demonstration of your error the other way around)

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    // reverse initialization order changes the outcome

    radioButtonWatchFile.IsChecked = Properties.Settings.Default.RadiosTesting;

    radioButtonWatchDirectory.IsChecked = Properties.Settings.Default.RadiosTesting;

    // ...
}

CodePudding user response:

Working solution.

Loading part :

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            radioButtonWatchDirectory.IsChecked = Properties.Settings.Default.RadioButtonWatchDirectory;
            radioButtonWatchFile.IsChecked = Properties.Settings.Default.RadioButtonWatchFile;
        }

Then when saving :

private void radioButtonWatchFile_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.RadioButtonWatchFile = true;
            Properties.Settings.Default.RadioButtonWatchDirectory = false;
            Properties.Settings.Default.Save();
        }

        private void radioButtonWatchDirectory_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.RadioButtonWatchFile = false;
            Properties.Settings.Default.RadioButtonWatchDirectory = true;
            Properties.Settings.Default.Save();
        }
  • Related