Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / Visual Basic
Article

Save settings in a configuration file - for Preferences/Options/Settings box

Rate me:
Please Sign up or sign in to vote.
3.42/5 (12 votes)
26 Jan 20054 min read 104.9K   1.8K   43   23
An article that describes how to save an options box completely, with just a single routine.

Sample Image - Settings.jpg

Introduction

This article (and a module along with it) helps you to save the information in most controls in a Form, to a file (or registry with slight tweaking), and load them whenever.

Background

A little information on how to use files and hash tables might help, but is not necessary.

Using the code

The code that accompanies demonstrates how to use the module to save the controls in a form. Typically, I have a TextBox, CommandButton, Label, OptionBox, and RadioButton in a form, and clicking Save/Load does the same.

This is done using recursion - thus, you save the parent control, and the recursive method calls its children and saves them all too. Similar fashion can be extended for the loading process.

Let's say we have a Form with a TextBox T, a GroupBox GB and option buttons (OB1, OB2,..) inside GB. You can also use the Tab control to provide the interface seen in Internet Explorer Options toolbox. So, to save these controls, we just use the following method:

VB
SaveSettings(Me,"settings.set")

SaveSettings calls SaveControls recursively, and saves each control based on their signature (control.GetType.toString) to the given file.

VB
Sub SaveControls(ByVal cnt1 As Control)
    '.....Recursive call....
End Sub

Saving is done using a generic function called RSet(setting as String, value as String). The beauty of this method is that it can be extended later to suit your purpose: maybe you want to use the registry (refer below for a comparison), instead of files, and so just replace file methods with GetSetting etc. You can also use various API / .NET calls to provide alternate versions too. Thus, it's a template that can be extended.

Loading is similar in process, but as we read the properties from the file, we have to trace them back to the control they belong to.

For e.g.,

Text1
This is a text
Opt1
1
Opt2
0

Thus, we are faced with the challenge of searching through all the controls, and matching it against the one currently read. This is inefficient, since it is a O(n^2) algorithm - which can deteriorate if you try with a Form with 100 or so controls...

A little more thought takes us to the almost optimal solution: .NET's HashTable. This is amortized constant (i.e., usually retrieves the value within one step or so) algorithm, and hence well optimized.

So, LoadSettings first builds a hash table based on the control's signature - namely its Name property.

VB
Dim myHashes As Hashtable
Sub BuildHashTable(ByVal myControl As Control, _
           Optional ByVal isInit As Boolean = True) 

    '.....Hash is built here.....

End Sub

Now, we can use the GetType.toString to know the type (e.g., "System.Windows.Forms.TextBox") and follow the appropriate steps to retrieve the value.

Points of Interest

I have modularized the methods to read/write a file, using my own methods - if you want, you can change them to use the registry using, say, GetSetting and SaveSetting methods in .NET. Remember, if you are using registry, then GetSetting will work only if the setting is already there - in which case, you have to read the controls first, and then use that signature to read from the registry.

Registry/File:

File is flat file organized, and cannot be shared (readers/writers problem), whereas the registry is a database and simultaneous reading / writing is taken care by Windows. But, this shouldn't discourage file usage, since only multiple processes reading the same file should cause a problem - which is a rarity.

File is easier to delete in case of uninstallers, whereas, Registry requires lots of underlying work to delete (even if you don't, Windows doesn't).

File usage allows you to understand the settings without much ado, (like going through regedit and finding the settings...). But, beware that the settings themselves are not per user - which is easily possible using Registry.

The rest of the issues lies upon you.

I used this settings saver/loader extensively to use a Options dialog box. And all I need to do was call a method - that's it. However, I did need to handle the changes in the controls - using events. Thus, it saved me tons of lines of code that I should have written.

The source shows a simple demo.

I haven't tried ListBox/ComboBox, since I haven't felt the need. But doing so is fairly easy - using the SelectedItems.Index property of that control.

Issues:

Sometimes, you might delete some controls/add new ones to the Form. In which case, the file doesn't correspond to the updated Forms. However, the loading of such a file will not cause problems, since the hash for that control won't exist (if the control is deleted) and will just proceed.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
Got good experience in C/C++/VB/VC++/ and now to an extent, .NET. Learning ASP .NET (http://perumaal.homeip.net).

Doing a major 3d visualization project for a granite company.

Pursuing my BS Comp Sci in Univ of Texas, Austin.

Comments and Discussions

 
GeneralSave and Load text from Multiline TextbBox Pin
daviddg29-Oct-10 23:52
daviddg29-Oct-10 23:52 
GeneralSave and Load background colors of a PictureBox Pin
daviddg27-Oct-10 3:06
daviddg27-Oct-10 3:06 
GeneralLoad and Save NumericUpDown control Pin
Andre Proulx6-Apr-10 9:07
Andre Proulx6-Apr-10 9:07 
GeneralGot error An unhandled exception of type 'System.ArgumentException' Pin
Gwilym Davies31-Jul-09 5:43
Gwilym Davies31-Jul-09 5:43 
QuestionSave and Load Listbox items? Pin
The Mighty Atom7-Jan-09 4:15
The Mighty Atom7-Jan-09 4:15 
QuestionHow would you extend this to read from a listbox and/or listview Pin
Finbarr Collins21-Apr-08 3:48
Finbarr Collins21-Apr-08 3:48 
AnswerRe: How would you extend this to read from a listbox and/or listview Pin
Alasdair Cameron19-Oct-08 14:17
Alasdair Cameron19-Oct-08 14:17 
GeneralRegistation code Pin
Mubeen Ajmal10-Aug-07 21:55
Mubeen Ajmal10-Aug-07 21:55 
QuestionThis would have been better implemented using Xml and XmlSerialization? Pin
p_caburian6-Sep-06 22:13
p_caburian6-Sep-06 22:13 
AnswerRe: This would have been better implemented using Xml and XmlSerialization? Pin
p_caburian6-Sep-06 22:33
p_caburian6-Sep-06 22:33 
GeneralRe: This would have been better implemented using Xml and XmlSerialization? Pin
The Mighty Atom11-Nov-06 8:33
The Mighty Atom11-Nov-06 8:33 
QuestionHow to save/load background colors of controls Pin
Mr. Culo Peluo29-Aug-06 5:24
Mr. Culo Peluo29-Aug-06 5:24 
AnswerRe: How to save/load background colors of controls Pin
Perumaal.Shanmugam29-Aug-06 8:13
Perumaal.Shanmugam29-Aug-06 8:13 
QuestionRe: How to save/load background colors of controls Pin
Mr. Culo Peluo30-Aug-06 5:11
Mr. Culo Peluo30-Aug-06 5:11 
AnswerRe: How to save/load background colors of controls Pin
Perumaal.Shanmugam30-Aug-06 5:53
Perumaal.Shanmugam30-Aug-06 5:53 
GeneralRe: How to save/load background colors of controls Pin
Mr. Culo Peluo30-Aug-06 6:47
Mr. Culo Peluo30-Aug-06 6:47 
Im sorry, but i can't get it to work. There are still a couple of blue underlined words, i don't know how to fix it.
If you say: "insert code for getting the value" im not gonna understand what cde that would be.

Look at this:

http://themightyatom.nl/screenshots/confusing_code.png

Probably horribly wrong wrong wrong. Im so close to completing my app, so i have to get this working, i just need some help, because i did'nt get all this in .NET class at school.

XML is so totally not usable for me at this moment, so that out of the question.

I hope you understand my problem. Smile | :)

BTW, im using a public account taken from bugmenot.com. Im going to make an account for myself. Smile | :)
GeneralRe: How to save/load background colors of controls Pin
The Mighty Atom31-Aug-06 8:00
The Mighty Atom31-Aug-06 8:00 
GeneralRe: How to save/load background colors of controls Pin
p_caburian6-Sep-06 22:27
p_caburian6-Sep-06 22:27 
GeneralRe: How to save/load background colors of controls Pin
p_caburian6-Sep-06 22:38
p_caburian6-Sep-06 22:38 
GeneralChanging Name settings.set Pin
Vitoto28-Jan-06 16:18
Vitoto28-Jan-06 16:18 
GeneralSave boolean values Pin
datlq19-Oct-05 19:19
datlq19-Oct-05 19:19 
GeneralComboBox and List IP Pin
Vitoto18-Oct-05 6:52
Vitoto18-Oct-05 6:52 
GeneralRe: ComboBox and List IP Pin
Perumaal.Shanmugam18-Oct-05 13:52
Perumaal.Shanmugam18-Oct-05 13:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.