Practical Learning Logo

Keeping Values in the Registry

 

Introduction

The registry is a huge database that holds all necessary the information that the operating system needs to function and communicate with anything that resides in the computer. It is organized as a tree view and functions like Windows Explorer. Although it is highly used by applications installed in the computer, you can use it to store information related to your application and retrieve that information when necessary.

Most of the information in the registration is organized like a dictionary, as a combination of keys and values. In this exercise, we will create an application with a form that the user can move while using the application. When the user closes the application, we will save the location of the form in the registry. The next time the user opens the application, we will retrieve the previous location and apply it to the form.

 

Practical Learning: Creating the Application

  1. Start Microsoft Visual Studio .NET or Visual Basic .NET and create a Windows Application named Remember1
  2. Right-click the body of the form and click View Code
  3. In the top section of the file, type Imports Microsoft.Win32 to be the first line of the file
  4. In the Class Name combo box, select [Form1 Events]
  5. In the Method Name combo box, select Closing
  6. Implement the Closing event as follows:
     
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As _
    		System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
            ' When the user closes the application,
            ' Examine the HKEY_CURRENT_USER key in the registry
            ' If there is no key named FormLoc, create it
            Dim regKey As RegistryKey = Registry.CurrentUser.CreateSubKey("FormLoc")
    
            ' In the RememberLocation key, create a sub-key named Left and
            ' store the Left position of the form in it
            regKey.SetValue("Left", Me.Left)
            ' Create a sub-key named Top and
            ' store the Top position of the form in it
            regKey.SetValue("Top", Me.Top)
    
            ' Close the registry object
            regKey.Close()
        End Sub
  7. Execute the application
  8. Move the form and close it
  9. In the Method Name combo box, select Load
  10. Implement the Load event as follows:
     
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' Find out if a key named FormLocexists in HKEY_CURRENT_USER
            ' If it exists, open it
            Dim regKey As RegistryKey = Registry.CurrentUser.CreateSubKey("FormLoc")
    
            ' Retrieve the values of its Left and its Top keys
            Dim strLeft As String = CStr(regKey.GetValue("Left"))
            Dim strTop As String = CStr(regKey.GetValue("Top"))
    
            ' Set the location of the form based on the values
            ' retrieved from the registry
            Me.Left = CInt(strLeft)
            Me.Top = CInt(strTop)
    
            ' Release the resources that the registry variable was using
            regKey.Close()
    End Sub
  11. Execute the application again and notice that it remembers where the form was positioned the last time the application was closed
  12. Open the registry and examine the HKEY_CURRENT_USER node. Notice that it has a new node named RememberMe
  13. Close the registry
 

Home Copyright © 2005-2012 FunctionX