Make A Preferences Form

'This code will show you how to make easily Preferences form, where the user can
'choose his options in Text Boxes, Check Boxes, Option Buttons and Combo Boxes.
'All the option wll be saved to INI File, and you will be able to get them easily from
'anywhere in your project. This Code is written in a way that you can easily add
'to the preferences form many options to choose from, without adding too much line codes.
'Add a module to your project (In the menu choose Project -> Add Module, Then click Open)
'Add another Form to your project. Add 2 Command Buttons to Form1.
'Add To Form2 one Check Box, 1 Combo Box, 1 Text Box And
'Array of Option Buttons (Option1(0), Option1(1) and so on).
'Add 2 Command Buttons and name them OKButton and CancelButton,
'Add some items to the Combo Box list.
'Insert the following code to your module :

Public INIFileName As String
'Enter here your Application name
Public Const MyAppName = "My App Name"
'Declare here one variable for every control you have in the preferences form.
'this variable will store the control value. If the control is Option Box or Check Box,
'declare him as String * 3.

Public Option1Var As String * 3, Text1Var As String * 15, Check1Var As _
String * 3, Combo1Var As String * 15
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

Function ReadFromINIToVariables()
'Add here one line for each control in the preferences form. Each line code below get the
'Value from the INI file and store it in the according variable, so you will be able to get
'the Perferences values from every part of your program.
'You should add your code line like that:
'Result = GetPrivateProfileString(MyAppName, Your Control Name, _
'The Default Value If No Data has Found, The Variable To store in it the control value, _
'Len(The Variable), INIFileName)

Result = GetPrivateProfileString(MyAppName, "Option1", "0", Option1Var, _
Len(Option1Var), INIFileName)
Result = GetPrivateProfileString(MyAppName, "Text1", "", Text1Var, _
Len(Text1Var), INIFileName)
Result = GetPrivateProfileString(MyAppName, "Check1", "0", Check1Var, _
Len(Check1Var), INIFileName)
Result = GetPrivateProfileString(MyAppName, "Combo1", "", Combo1Var, _
Len(Combo1Var), INIFileName)
End Function

'Insert the following code to your form (Form1):

'Press Command1 to launch the preferences from.
Private Sub Command1_Click()
'This code line will assure that all the project will be disable until the preferences
'form will be through.

Form2.Show 1
End Sub

'Press Command2 to get the perferences values
Private Sub Command2_Click()
MsgBox "Text1 = " & Text1Var
MsgBox "Combo1 = " & Combo1Var
MsgBox "Option1 = " & Option1Var
MsgBox "Check1 = " & Check1Var
End Sub

Private Sub Form_Load()
'Put here your INI File Name
INIFileName = App.Path & "\" & "MyFile.ini"
'This line store the perferences values inside the variables
ReadFromINIToVariables
End Sub

'Insert the following code to your form (Form2):

Function OptionStartData()
'This will center your preferences form on the screen
Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2
'This will place the current preferences in the controls
ReadFromINIToControls
End Function

Private Sub CancelButton_Click()
Unload Me
End Sub

Private Sub Form_Load()
OptionStartData
End Sub

Function SaveFromControlsToINI()
Dim Contrl As Control
Dim TempControlName As String, TempControlValue As String
On Error Resume Next
For Each Contrl In Me
If (TypeOf Contrl Is CheckBox) Or (TypeOf Contrl Is ComboBox) Then
TempControlName = Contrl.Name
TempControlValue = Contrl.Value
If (TypeOf Contrl Is ComboBox) Then
TempControlValue = Contrl.Text
If TempControlValue = "" Then TempControlValue = 1
End If
Result = WritePrivateProfileString(MyAppName, TempControlName, _
TempControlValue, INIFileName)
End If
If (TypeOf Contrl Is TextBox) Then
TempControlName = Contrl.Name
TempControlValue = Contrl.Text
Result = WritePrivateProfileString(MyAppName, TempControlName, _
TempControlValue, INIFileName)
End If
If (TypeOf Contrl Is OptionButton) Then
TempControlValue = Contrl.Value
If TempControlValue = True Then
TempControlName = Contrl.Name
TempControlValue = Contrl.Index
Result = WritePrivateProfileString(MyAppName, TempControlName, _
TempControlValue, INIFileName)
End If
End If
Next
End Function

Private Sub OKButton_Click()
SaveFromControlsToINI
ReadFromINIToVariables
Unload Me
End Sub

Function ReadFromINIToControls()
Dim Contrl As Control
Dim TempControlName As String * 30, TempControlValue As String * 20
On Error Resume Next
For Each Contrl In Me
If (TypeOf Contrl Is CheckBox) Or (TypeOf Contrl Is ComboBox) Or (TypeOf _
Contrl Is OptionButton) Or (TypeOf Contrl Is TextBox) Then
TempControlName = Contrl.Name
If (TypeOf Contrl Is TextBox) Or (TypeOf Contrl Is ComboBox) Then
Result = GetPrivateProfileString(MyAppName, TempControlName, "", _
TempControlValue, Len(TempControlValue), INIFileName)
Else
Result = GetPrivateProfileString(MyAppName, TempControlName, "0", _
TempControlValue, Len(TempControlValue), INIFileName)
End If
If (TypeOf Contrl Is OptionButton) Then
If Contrl.Index = Val(TempControlValue) Then Contrl = True
Else
Contrl = TempControlValue
If (TypeOf Contrl Is ComboBox) Then
If Len(Contrl.Text) = 0 Then Contrl.ListIndex = 0
End If
End If
End If
Next
End Function

Go Back