Easily save and restore CheckedListBox values in the Registry in C#

example

The example Easily save and restore a form’s settings in the Registry in C# shows one way you can save a form’s settings and the values placed in its controls. That example really just demonstrates the basic ideas and shows how to save the values in a few kinds of controls.

This example extends the previous one to save and restore the items that are checked in a CheckedListBox control.

Saving Values

The program’s SaveAllSettings method saves the form’s width, height, and position. It then calls the SaveChildSettings method passing it the form in its parent parameter.

The SaveChildSettings method loops through the parent’s child controls. It uses a switch statement to save the values that are appropriate for each child.

The following code shows the new case for a CheckedListBox control.

case "CheckedListBox":
    CheckedListBox lst = child as CheckedListBox;
    for (int i = 0; i < lst.Items.Count; i++)
    {
        // Default is false.
        if (lst.GetItemChecked(i))
        {
            string item_name = child.Name + "(" + i.ToString() + ")";
            SaveSetting(app_name, item_name, "True");
        }
    }
    break;

This code converts the generic child object into a CheckedListBox and then loops through the indices of the control’s items. For each item, the code uses the list’s GetItemChecked metod to determine whether the item is checked.

If the item is checked, the code creates the item’s name consisting of the control’s name plus the item’s index inside parentheses. For example, the clbMonster control’s item number 4 would have the name “clbMonster(4).” The code then calls the SaveSetting method to save the value “True” for the item. (See the previous example for a description of the SaveSetting method.)

Notice that the code does not save a value for items that are not checked. When it reloads the values, the program assumes that any missing values are false.

After it has saved the child control’s values, the SaveChildSettings method recursively calls itself to process the children of the child (if it has any).

Loading Values

The code that loads values follows a similar pattern to the one used to save values. The LoadAllSettings method loads the form’s dimensions and position, and then calls the LoadChildSettings method, passing it the form as the parent parameter.

The LoadChildSettings method loops through the parent’s child controls and uses a switch statement to save appropriate values. The following code shows the case for the CheckedListBox control.

case "CheckedListBox":
    CheckedListBox lst = child as CheckedListBox;
    for (int i = 0; i < lst.Items.Count; i++)
    {
        // Default is false.
        string item_name = child.Name + "(" + i.ToString() + ")";
        bool is_checked =
            bool.Parse(GetSetting(app_name, item_name, "False").ToString());
        lst.SetItemChecked(i, is_checked);
    }
    break;

This code converts the child control into a CheckedListBox and then loops through its items. For each item, the code composes the item’s name as before. It then calls the GetSetting method to get the item’s value using the default value “False.” The code parses the result as a Boolean value and uses the list’s SetItemChecked method to check or uncheck the item.

Summary

The code shown here only handles CheckedListBox controls. See the previous post for additional details such as how the code handles other control types.

Using similar techniques, you should be able to add other control types if you need them.

This example uses a single form. It’s Load and FormClosing load and save settings, respectively. You should be able to use a similar technique to save and restore settings for multiple forms. Simply use different app names for the different forms. Alternatively you could prepend the form’s name to each of the control names.

You can also use a similar approach to save and restore settings in places other than the Registry. For example, you could save values in a text, XML, JSON, or settings file.

Download the example to experiment with it and to try making these modifications.


Download Example   Follow me on Twitter   RSS feed




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in forms, programs, registry. Bookmark the permalink.