Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleEasily save and restore all of a form's settings and the values of its controls in the Registry in Visual Basic .NET
DescriptionThis example shows how to easily save and restore all of a form's settings and the values of its controls in the Registry in Visual Basic .NET.
Keywordssettings, save settings, restore settings, Registry, SaveSetting, GetSetting, C#, C# programming, example, example program, Windows Forms programming
CategoriesControls, Software Engineering
 
This example saves and restores the form's size, position, and window state (normal, minimized, or maximized). It also saves the values in all of the form's controls. (This example handles textboxes, combo boxes, radio buttons, numeric up/down buttons, and scroll bars. You can use similar techniques to make it handle other controls if you like.)

The code is pretty long so I'm only going to describe it here. Download the example to see how it works.

Saving Values

The SaveAllSettings function uses SaveSetting to save the form's basic information in the Registry. It first saves the form's WindowState. Then if the form is in its normal state, the function saves its current size and position. If the form is minimized or maximized, the function saves the form's restored bounds--the size and position it has when in the normal state. The function finishes by calling SaveChildSettings to save the values in the form's controls.

Function SaveChildSettings saves textbox Text values, combo box Checked values, and so forth. It takes as a parameter a parent control and loops through the child controls that the parent contains. The function uses a switch statement to see what kind of control each child is and uses SaveSetting to save the appropriate value. It then recursively calls itself for the child to save the values in the child's children.

Loading Values

The LoadAllSettings function simply uses GetSetting to retrieve the saved form size and position and uses them to arrange the form. It also sets the form's WindowState. It finishes by calling LoadChildSettings to load the controls' saved values.

Function LoadChildSettings loops through a parent control's children. It uses GetSetting to restore the child's saved value and then recursively calls itself to load the values in the control's children.

Using the Functions

Having written these functions, using them is easy. I put them in the RegistryTools class to make them easy to move into new projects. The program simply calls the static LoadAllSettings and SaveAllSettings functions in its Load and FormClosing event handlers. The following code uses the application's product name to identify the section in the Regsitry where the values should be stored.
 
' Load the saved settings.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    RegistryTools.LoadAllSettings(Application.ProductName, _
        Me)
End Sub

' Save settings.
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal _
    e As System.Windows.Forms.FormClosingEventArgs) Handles _
    Me.FormClosing
    RegistryTools.SaveAllSettings(Application.ProductName, _
        Me)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated