During the development of your project, the types and number of settings can grow and grow. With these routines, you can write the code once, then you usually won’t need to update this code even when you add new controls.
For each setting that you save, whether to the Registry or an INI, you’ll need to define “section” and “key” strings. See the help on GetSetting and/ or SaveSetting for examples.
For each control that you’ll use to maintain a setting, put the name of the “key” in the control’s TAG property. Put the default value of that key in the control itself at design time. For the section name, you must put your control into a container control with the desired “section” string in its own .TAG field. Often, this is actually very convenient, as controls get grouped into frames and other logical containers. When that isn’t fully effective, use any kind of invisible container which supports a .TAG property, or use the TAG of the form itself. I usually reserve that last tactic for the most general of settings.
Call GetOptions before showing the form to the user (I usually call it from form_load) and call PutOptions whenever you need to save settings.
Code
Public Sub GetOptions(Frm As Form)
Dim Ctrl As Control
For Each Ctrl In Frm.Controls
With Ctrl
If .Enabled Then
If Len(.Parent.Tag & .Tag) Then
If TypeOf Ctrl Is TextBox Then
.Text = Trim$(GetSettingString(.Parent.Tag, .Tag, .Text))
ElseIf TypeOf Ctrl Is CheckBox _
Or TypeOf Ctrl Is HScrollBar _
Or TypeOf Ctrl Is VScrollBar _
Then
.Value = GetSettingInt(.Parent.Tag, .Tag, .Value)
End If
End If
End If
End With
Next
End Sub
Public Sub PutOptions(Frm As Form)
Dim Ctrl As Control
For Each Ctrl In Frm.Controls
With Ctrl
If .Enabled Then
If Len(.Parent.Tag & .Tag) Then
If TypeOf Ctrl Is TextBox Then
SaveSettingString .Parent.Tag, .Tag, .Text
ElseIf TypeOf Ctrl Is CheckBox _
Or TypeOf Ctrl Is HScrollBar _
Or TypeOf Ctrl Is VScrollBar _
Then
SaveSettingInt .Parent.Tag, .Tag, .Value
End If
End If
End If
End With
Next
End Sub
You’ll need to include GetSettingInt, GetSettingString, SaveSettingInt, and SaveSettingString routines to save the settings to your own INI file.
Also, you can certainly adjust the code for other types of controls besides the TextBox, CheckBox, HScrollBar and VScrollBar.
Additional Code
'Set this public variable before you call GetOptions or PutOptions
Public gsIniFile As String
Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA"
(ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long,
ByVal lpFileName As String) As Long
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA"
(ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String,
ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA"
(ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any,
ByVal lpFileName As String) As Long
Private Function FixString(sInString As String) As String
Dim nPos As Integer
On Error Resume Next
nPos = InStr(sInString, Chr$(0))
If nPos Then
FixString = Left$(sInString, nPos - 1)
Else
FixString = sInString
End If
End Function
Private Function GetSettingString(sSection As String, sKey As String, sDefault As String) As String
Const INI_BUF_SIZE = 255
Dim iReturn As Integer
Dim sBuffer As String
On Error Resume Next
sBuffer = String$(INI_BUF_SIZE + 1, 0)
iReturn = GetPrivateProfileString(sSection, sKey, sDefault, sBuffer, INI_BUF_SIZE, gsIniFile)
GetSettingString = FixString(sBuffer)
End Function
Private Function GetSettingInt(sSection As String, sKey As String, nDefault As Integer) As Integer
On Error Resume Next
GetSettingInt = GetPrivateProfileInt(sSection, sKey, nDefault, gsIniFile)
End Function
Private Function SaveSettingInt(sSection As String, sKey As String, nValue As Integer) As Boolean
On Error Resume Next
SaveSettingInt = IniPrivateWriteString(sSection, sKey, CStr(nValue), gsIniFile)
End Function
Private Function SaveSettingString(sSection As String, sKey As String, sValue As String) As Boolean
On Error Resume Next
SaveSettingString = IniPrivateWriteString(sSection, sKey, sValue, gsIniFile)
End Function
Private Function IniPrivateWriteString(sSection As String, sKey As String,
sValue As String, sIniFile As String) As Boolean
On Error Resume Next
If (Len(sKey) > 0) Then
IniPrivateWriteString = (WritePrivateProfileString(sSection, sKey, sValue, sIniFile) <> 0)
Else
'----- Delete all keys in this section
IniPrivateWriteString = (WritePrivateProfileString(sSection, vbNullString, sValue, sIniFile) <> 0)
End If
End Function
Tip Submitted By: Bob O`Bob
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.
