|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThere are several samples available to save and restore application or user preferences, but most are limited to primatives (numbers, dates, booleans, strings, etc.), or have code to handle specific types of objects. I found this approach too limiting, and frankly, too complex for what is a fairly simple operation - convert _X_ to a string and back again. The answer lies mainly in two built-in interfaces:
BackgroundThis sample is built around actual production code that I use, to save user preferences to a SQL Server database. The code in the demo was modified to simply use text files. Using the codeSample CodeThe following excerpts from the demo code show, how easy it is to save and restore various user preferences: a Protected Overrides Sub OnLoadSettings()
' Let mybase do its thing...
MyBase.OnLoadSettings()
' Load my form's data:
' TextBox attributes: text (String), font (Object), color (Structure)
tbFreeForm.ForeColor = CType(UserPreferences.GetPreference
("FREEFORM.COLOR", tbFreeForm.ForeColor), Color)
tbFreeForm.Font = CType(UserPreferences.GetPreference
("FREEFORM.FONT", tbFreeForm.Font), Font)
tbFreeForm.Text = CType(UserPreferences.GetPreference
("FREEFORM.TEXT", tbFreeForm.Text), String)
End Sub
Protected Overrides Sub OnSaveSettings()
' Let mybase do its thing...
MyBase.OnSaveSettings()
' Save my form's data:
UserPreferences.SavePreference("FREEFORM.COLOR",
tbFreeForm.ForeColor, True)
UserPreferences.SavePreference("FREEFORM.FONT", tbFreeForm.Font, True)
UserPreferences.SavePreference("FREEFORM.TEXT", tbFreeForm.Text, True)
' Since this is the "main" window, now we
' tell UserPrefs to actually save:
UserPreferences.SavePreferences()
End Sub
UserPreferencesThe Public Shared Function GetPreference(ByVal Key As String,
ByVal DefaultValue As Object) As Object
Returns a saved preference (if found), or the provided Public Shared Sub SavePreference(ByVal Key As String, ByVal Value As Object,
ByVal Persist As Boolean)
Saves the specified value to the local cache; will optionally flag persistence to a file. Public Shared Property AutoSave() As Boolean
Sets the save mode for the Public Shared Function DefaultFileName() As String
Returns the default filename used to save/load user preferences. It will be in the format: x:\Documents and Settings\<username>\Application Data\
<exename>\Preferences.datPublic Shared Sub FetchPreferences(Optional ByVal FileName As String = "")
Loads in all user preferences saved in the specified file. Public Shared Sub SavePreferences(Optional ByVal FileName As String = "")
Saves all user preferences to the specified file. frmWindowSettingsAlso included is an ancestor form, that automatically saves and restores its last size, position, and state. I use this form as an inheritance base, whenever creating a new window. It provides custom "events" to easily add load/save code, as well as a new property: Protected Overridable Sub OnLoadSettings()
Called when the form and controls have been created and initialized. Protected Overridable Sub OnSaveSettings()
Called when the form is closing. Protected Overridable Sub OnRendered()
Called when the form is first made visible. Public Property SaveSettings() As Boolean
Set Points of interestWhen developing the routines, I found that History
|
||||||||||||||||||||||