Results 1 to 8 of 8

Thread: Modifying the registry:

Threaded View

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Modifying the registry:

    There are a few ways to go about using the registry, (the first way keeps the code inside a specific part of the registry called VB/VBA) the easiest are Called SaveSetting and GetSetting:

    To start, lets save our forms X,Y values so it starts off in the same place as it last closed at:
    VB Code:
    1. Private Sub Form_Load()
    2.     Me.Left = GetSetting(App.EXEName, "Options", "X")'Retrieves The value of X
    3.     Me.Top = GetSetting(App.EXEName, "Options", "Y")'Retrieves the value of Y
    4. End Sub
    5.  
    6. Private Sub Form_Unload(Cancel As Integer)
    7.     SaveSetting App.EXEName, "Options", "X", Me.Left 'Saves the value of me.left to X
    8.     SaveSetting App.EXEName, "Options", "Y", Me.Top 'Saves the value of me.top to Y
    9. End Sub

    Now if you want to check if a registry key exists youd do the following:
    VB Code:
    1. If len(GetSetting(App.EXEName, "Options", "X"))>0'then it exists

    --------
    Now onto the hard way. This way is EXTREMELY useful. It lets you read and write ANYWHERE in the registry.
    Download this Class Module:Reg.Cls

    Applying the same thought as above, we will save the forms top and left coordinates on form unload, and load them on form load.

    VB Code:
    1. Option Explicit
    2. Dim reg As cRegistry
    3.  
    4. Private Sub SaveSettings() 'Writes to Registry
    5.     Set reg = New cRegistry
    6.     'This will create a new value in the Run section
    7.     'so your program will automatically run when Windows starts
    8.        'This will create a new section to store your own keys in
    9. reg.Classkey = HKEY_CURRENT_USER
    10. reg.SectionKey = "Software\My Company\My App"
    11. 'create the above key
    12. reg.CreateKey
    13. 'Save the Form settings to the registry
    14. reg.Valuetype = REG_DWORD
    15. reg.Valuekey = "FormTop"
    16. reg.Value = CLng(Me.Top)
    17. reg.Valuekey = "FormLeft"
    18. reg.Value = CLng(Me.Left)
    19. reg.Valuekey = "FormWidth"
    20. reg.Value = CLng(Me.Width)
    21. reg.Valuekey = "FormHeight"
    22. reg.Value = CLng(Me.Height)
    23. End Sub
    24.  
    25. Private Sub GetSettings() 'Reads the Registry
    26.     Set reg = New cRegistry
    27.     reg.ClassKey = HKEY_CURRENT_USER
    28.     reg.SectionKey = "Software\My Company\My App"
    29.     reg.ValueKey = "FormTop"
    30.     Me.Top = reg.Value
    31.     reg.ValueKey = "FormLeft"
    32.     Me.Left = reg.Value
    33. End Sub
    34.  
    35. Private Sub Form_Load()
    36. Call GetSettings
    37. End Sub
    38.  
    39. Private Sub Form_Unload(Cancel As Integer)
    40. Call SaveSettings
    41. End Sub

    Now what ive done with these function below, is simplified EVERYTHING.
    VB Code:
    1. ''For Saving
    2. Private Function CreateKeys(HKey As String, SectionKey As String, ValueKey As String, Valuetype As String, Value As String)
    3.    Set reg = New cRegistry
    4.        'This will create a new section to store your own keys in
    5. reg.Classkey = Hkey
    6. reg.SectionKey = SectionKey
    7. 'create the above key
    8. reg.CreateKey
    9. 'Save the Form settings to the registry
    10. reg.Valuetype = Valuetype
    11. reg.Valuekey = Valuekey
    12. reg.Value = Value
    13. End Function

    Usage:
    VB Code:
    1. Call CreateKeys(HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyServer", Reg_sz, "80.2523.135.315:70")

    For Reading:
    VB Code:
    1. Dim reg As cRegistry
    2.  
    3. Private Function GetKeys(Hkey As String, Section As String, Valuekey As String)  'Reads the Registry
    4.     Set reg = New cRegistry
    5.     reg.Classkey = Hkey
    6.     reg.SectionKey = Section
    7.     reg.Valuekey = Valuekey
    8.     Msgbox reg.value
    9. End Function

    Usage:
    VB Code:
    1. Private Sub Form_Load()
    2. Call GetKeys(HKEY_CURRENT_USER, "SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings", "ProxyServer")
    3. End Sub

    Hope this cleared some stuff up, it took me forever to understand how it all works, hope i helped you
    Last edited by |2eM!x; Jun 14th, 2005 at 02:23 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width