comp • gb.settings • settings

Settings (gb.settings)

This class manages global configuration files.

This class can be used like an object by creating a hidden instance on demand.
This class is
Creates a new settings object from the configuration file specified by Path.
creatable
.
This class acts like a
Returns a setting from its key.
read
/
Associates a setting with a key.
write
array.

Static properties

Static methods
This property returns the default directory where settings configuration files are stored.
DefaultDir  
FromString   ToString  

Properties

Methods
Return a virtual object used for enumerating all the keys used by the setting file.
Keys  
Return the path of the setting file.
Path  
Clears all settings.
Clear  
Initializes the specified object from the settings file.
Read  
Cancels all settings changes, by reloading the settings file.
Reload  
Saves the configuration file to the disk.
Save  
Writes the settings of the specified object in the settings file.
Write  

If you use this class statically, then the default project configuration file will be used.

The path of the default configuration file is:

User.Home &/ ".config/gambas3" &/ Application.Name & ".conf"

For example, the development environment configuration file is stored in ~/.config/gambas3/gambas3.conf

This example loads a window's position from a settings file when a form is opened. If the settings file does not exist, then the form's default position would be used. The example also saves the forms position when the form closes. The default settings file for the application is used.

Examples

Public Sub Form_Open()
  ' Update window position from settings file
  ' If the settings file is not found then
  ' use default position.
  Me.Top = Settings["Window/Top", Me.Top]
  Me.Left = Settings["Window/Left", Me.Left]
  Me.Height = Settings["Window/Height", Me.Height]
  Me.Width = Settings["Window/Width", Me.Width]
End

Public Sub Form_Close()
  ' Save window settings when application closes
  Settings["Window/Top"] = Me.Top
  Settings["Window/Left"] = Me.Left
  Settings["Window/Height"] = Me.Height
  Settings["Window/Width"] = Me.Width
End

The saved settings file would look something like the following settings in the Window group.

[Window]
Top = 13
Left = 18
Height = 154
Width = 235
Public Sub ShowASpecialSetting()
  Dim prhOtherConfFile As Settings
  Dim prhLangUsed As String

  ' Read a setting from a file used by another application
  ' located in /home/user/.config/otherapp/
  ' and named "international.conf"
  'First create a new instance of Settings class
  'with the full path to desired file
  prhOtherConfFile = New Settings("/home/user/.config/otherapp/international.conf")
  'next read the "Lang1" value from the "UserPreferences" section
  prhLangUsed = prhOtherConfFile["UserPreferences/Lang1", Null]
  If IsNull(prhLangUsed) Then
    Message.Info("No Lang1 found in conf file (or no Section named UserPreferences)")
  Else
    Message.Info("The value of Lang1 that found in conf file is:\n" & prhLangUsed)
  End If
End

Note that you can directly use the Write method to store the settings of a window, and the Read method to recall them.