Click here to Skip to main content
15,885,365 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Handling Application Settings

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
15 Mar 2014CPOL2 min read 8.1K   1  
Handling large numbers of application settings

Introduction

I have a suite of applications that have individual settings as well as suite wide settings. As it is a set of database applications, the settings are stored in a single string using Split to load them into a List(Of String) for access by the applications. There are over 100 settings and when saving or loading making sure you get every parameter loaded or saved has become cumbersome. Recently I had to revisit the code and add a few new features which needed new parameter settings, having to add lines to get the setting into the control, then get it from the control to save the code lines was getting unmanageable.

So I developed a simpler method; Controls on a form often have a Tag field which is not used by the control itself but is a useful place to store data for the control. In this case, I store the string name of the parameter in the associated controls Tag. Now it is just a matter of adding a control to the settings form and setting the controls Tag to the parameter name and it is done.

The only caveat I know of is to verify the Tag setting if you cut or copy and paste controls, VS 2010 seems to forget contents.

Using the Code

For my application, the settings management form is a tabbed control broken into the different sections of the suite. Some settings are grouped in container controls.

I have included a simple class to manage setting and getting parameters, it is not perfect and needs more trapping round the .Parse calls. To initialise it with current settings, call the Load() sub with a parameter string from wherever you store it. To save settings, call the UnLoad function to return a string of the current parameter settings and store them where you will.

VB.NET
Public Paramset as New SystemParameters

To save parameters:

VB.NET
For Each x As TabPage In TabControl1.TabPages
    For Each y As Control In x.Controls
        If TypeOf (y) Is Panel Or TypeOf (y) Is GroupBox Then
           For Each z As Control In y.Controls
               SaveControlsValue(z)
           Next
        Else
               SaveControlsValue(y)
        End If
    Next
Next 

To load parameters:

VB.NET
 For Each x As TabPage In TabControl1.TabPages
     For Each y As Control In x.Controls
         If TypeOf (y) Is Panel Or TypeOf (y) Is GroupBox Then
            For Each z As Control In y.Controls
                SetControlsValue(z)
            Next
         Else
            SetControlsValue(y)
         End If
     Next
 Next 
 Private Sub SetControlsValue(ControlToSet As Control)
'
' Get the parameter value for a control using the parameter name stored in the controls tag
'
  If ControlToSet.Tag <> "" Then
     If TypeOf (ControlToSet) Is CheckBox Then
        CType(ControlToSet, CheckBox).Checked = ParamSet.GetBoolean(ControlToSet.Tag)
     ElseIf TypeOf (ControlToSet) Is TextBox Then
         CType(ControlToSet, TextBox).Text = ParamSet.GetString(ControlToSet.Tag)
     ElseIf TypeOf (ControlToSet) Is RadioButton Then
         CType(ControlToSet, RadioButton).Checked = ParamSet.GetBoolean(ControlToSet.Tag)
     ElseIf TypeOf (ControlToSet) Is NumericUpDown Then
         CType(ControlToSet, NumericUpDown).Value = ParamSet.GetInteger(ControlToSet.Tag)
     End If
  End If
End Sub  
 Private Sub SaveControlsValue(ControlToSave As Control)
   If ControlToSave.Tag <> "" Then
      If TypeOf (ControlToSave) Is CheckBox Then
         ParamSet.SetParameter(ControlToSave.Tag, CType(ControlToSave, CheckBox).Checked)
      ElseIf TypeOf (ControlToSave) Is TextBox Then
         ParamSet.SetParameter(ControlToSave.Tag, CType(ControlToSave, TextBox).Text)
      ElseIf TypeOf (ControlToSave) Is RadioButton Then
         ParamSet.SetParameter(ControlToSave.Tag, CType(ControlToSave, RadioButton).Checked)
      ElseIf TypeOf (ControlToSave) Is NumericUpDown Then
         ParamSet.SetParameter(ControlToSave.Tag, CType(ControlToSave, NumericUpDown).Value)
      End If
  End If
End Sub 
  Public Class SystemParameters
    Private ParameterArray As List(Of String)
    Public SplitChar As String = "¬"

    Public Sub Load(ByVal iParam As String)
        '
        ' Load the main parameter array from the iParam string
        '
        If ParameterArray IsNot Nothing Then
            ParameterArray = Nothing
        End If

        ParameterArray = New List(Of String)(Split(iParam, SplitChar, , CompareMethod.Text))
    End Sub
    Public Function UnLoad() As String
        If ParameterArray IsNot Nothing Then
            UnLoad = Join(ParameterArray.ToArray, SplitChar)
        Else
            UnLoad = ""
        End If
    End Function
    Public Sub Clear()
        ParameterArray = Nothing
    End Sub
    Public Function GetString(ByVal nParam As String) As String
        Dim i As Integer = -1

        GetString = ""

        If ParameterArray IsNot Nothing Then
            i = ParameterArray.IndexOf(nParam)
        End If

        If i <> -1 Then
            GetString = ParameterArray.Item(i + 1)
        End If
    End Function
    Public Function GetInteger(ByVal nParam As String) As Integer
        Dim s As String

        GetInteger = 0

        s = GetString(nParam)

        If s <> "" Then
            GetInteger = Integer.Parse(s)
        End If
    End Function
    Public Function GetDecimal(ByVal nParam As String) As Decimal
        Dim s As String

        GetDecimal = 0

        s = GetString(nParam)

        If s <> "" Then
            GetDecimal = Decimal.Parse(s)
        End If
    End Function
    Public Function GetBoolean(ByVal nParam As String) As Boolean
        Dim s As String

        GetBoolean = False

        s = GetString(nParam)

        If s <> "" Then
            GetBoolean = Boolean.Parse(s)
        End If
    End Function
    Public Sub SetParameter(ByVal nParam As String, ByVal nValue As Object)
        If nValue IsNot Nothing And nValue.ToString <> "" Then
            If ParameterArray Is Nothing Then
                ParameterArray = New List(Of String)
            End If

            Dim i As Integer

            i = ParameterArray.IndexOf(nParam)

            If i <> -1 Then
                ParameterArray.Item(i + 1) = nValue
            Else
                ParameterArray.Add(nParam)
                ParameterArray.Add(nValue)
            End If
        End If
    End Sub
End Class 

Points of Interest

Doing it this way has made life a lot simpler and I have transferred the method to other programs making them simpler too.

History

  • 15th March, 2014: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Retired
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --