Store/load form settings with a generic Settings class?

Hi,

I have a form containing graphical components such as textboxes, checkboxes and radiobuttons. When the program closes I want a Settings class to handle the state of these components and Save/Load them to/from a text file. I want the Settings class to be able to retrieve the state from each component without hard coding each component in the Settings class. The reason for this is that if I want to add/remove/change the components in the form, I don't want to change the Settings class. It should be genereric enough to handle the cahnge in the form.

I am grateful for all suggestions on how to solve this

/Hubba


Answer this question

Store/load form settings with a generic Settings class?

  • Al Sauve

    You might also look at this thread:
    http://www.windowsforms.com/Forums/ShowPost.aspx tabIndex=1&tabId=41&PostID=22004

    It is much along the same lines...

  • Ninja_Programmer

    Sorry, I think I messed this one up a bit...

    When you were asking about saving the "state" of various controls, I thought you meant saving the values of all of the control's properties.  But you actually meant just one specific property for each control; i.e.  Text for Textboxes, Checked for Checkboxes, etc.

    I don't think databinding is right for this.

    Databinding works well if you allow your user to customize the layout of the form at runtime and want to save that design and reload it later.  Each record in the table the control binds to would represent a "view" of that control.  You might bind a control's width to an integer column.  That column would contain rows values of, say, 25,35,45,55,65.  When the datatable's position is at 0, the control would be 25 wide.  Move to the next record (position 1) and the control widens to 35.

    You should probably just loop through the Form.Controls collection, compare the type of the control and then write the appropriate property value to a config file or registry entry.

    I guess I'm not quite clear on what you're actually trying to do...  It sounds like you might just have some options in your application that you want the user to set and you want to remember from one instance of the app to the next what the user chose.  In that case your talking about config files or registry entries.  The data might be control property values, but it could be anything else too...  Is this more along the lines of what you're trying to do

  • Mark71

    You can bind the properties of the controls to a dataset using advanced databinding.  This is the fastest way I know of to set control properties en masse.

    You will have to manually create a new entry for each control if you want to set the databinding at design time.

    But if you have a lot of controls you want to do this with, databinding is about the only option that isn't going to require a massive loop through Form.Controls; and that loop could take a while with many controls and properties to get/set.

  • r0br0y

    Thanks for the reply. Good to know that there is a simpler way :) I have some questions though:

    I have created a DataSet, and a table called ComponentState. In that table I have created a columne for each of the components which I want to store the state (checked for checkboxes and radiobuttons for example). 

    1. When I start my program and select a CheckBox, does it automatically change the state in the dataSet or do I have to trigger that

    2. When the program terminates I would like to save this DataSet in some way to the hard drive. I can think of two options; first using the Marshal function to save the dataaset to the hard drive or second to loop through the DataSet saving the components state to a text file. I think the Marshal option would be most beneficial. Is that correct

    3. After the program has started and the DataSet is initialized correctly using one of the two options in 2. Is each components state automatically changed when the corresponding column in the DataSet has changed or do I have to trigger the DataSet in some way to apply the settings to each component

    I am grateful for all answers, 
    /Hubba

  • Store/load form settings with a generic Settings class?