Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm writing a program that uses about 15 textboxes/comboboxes.

I want to use a "list selection" so the user can select a preset that fills the textboxes with previous user inputed values.
The user can edit and select these presets during runtime. He can fill in different values in the 15 textboxes and save them under an other preset name. All presets have to be saved when cloosing the program. At startup all presets have to be loaded again.
Do you have suggestions how I should do this?

Setting up an access or SQL database is to bigg an not usefull to distribute.
Do I put all textboxes in a datagridview? But I don't know how to bind a "preset" combobox to an index row in de datagridbiew, so the user can select a preset.
How do I save everything an load it again at startup? Visual studio application settings can save only one property at a time, not multiple user selections, wright?

thanks a lot
Posted

1 solution

First, create a data model of all settings. This should be a set of some data classes, not related to functionality. This model should be central to you settings system. I mean, if the functional unit of the applications needs to "decide" how to proceed, it should ask the model, not UI control. The UI controls should be populated from the instance of the model, and the model should be instantiated based on the state of controls. The functionality should not be altered directly, only through the data model.

On next step, you need to develop a persistence for the instanced of the model. To do so, I would advice using Data Contracts. Please see:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

This method is most comprehensive and at the same time, easiest to use and non-intrusive. It means, you do not need to modify your data model types in order to comply. You only add some Data Contract related attributes to the types of the model and some of their members (only those essential for the contract).

Please see my past answers where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

After this step, you will be able to store the model is some XML file (only one) or a stream and load it back exactly as it was in memory. If does not have to be a file on the file system, it could be in a network or resource string, anything.

That brings us to a last step: presets. I would advise to create the presets in the resources embedded in your assembly. First of all, prepare the presets. For this purpose, set some options manually and save each preset as a regular XML file using the persistence created on the previous step. You can have a file open dialog, file save dialog and two corresponding menu options to store/load a preset by this time, after you have your persistence working. Now, it's a time to embed the presets.

You don't have to work with resources directly. Instead, create some *.resx resource in the assembly you want to keep the presets. In the resource windows, click on Add Resource, Add Existing File and just add the preset XML files you already prepared. The files will be copies to the project, added to the project and referenced from the resource file. All the preset files and a resource file will become a part of the source code, so don't remove them. At the same time, make sure you do not store the presets file twice: the procedure shown above will create copies, so, if the location of original files is different, the duplicates will appear, so make sure to delete original files; you only need files included in your project.

Now, finally: how to use the embedded resources? In case of XML files, this is way too simple. Look at the auto-generated VB file, found in the project tree with Solution Explorer as a child item under the node of the resource file. Look at this file in the editor: it will contain a static class with static properties of the string type, containing the context of your preset XML files. You will be able to recognize those properties because their name will be the same or close to the name of your source XML file. User these properties directly in your code to load a preset.

That's it.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900