Gambas Documentation
Application Repository
Code Snippets
Compilation & Installation
Components
gb
gb.args
gb.cairo
gb.chart
gb.clipper
gb.complex
gb.compress
gb.crypt
gb.data
gb.db
gb.db.form
gb.db.mysql
gb.db.odbc
gb.db.postgresql
gb.db.sqlite2
gb.db.sqlite3
gb.dbus
gb.dbus.trayicon
gb.debug
gb.desktop
gb.desktop.gnome.keyring
gb.desktop.x11
gb.eval
gb.eval.highlight
gb.form
gb.form.dialog
gb.form.editor
gb.form.htmlview
gb.form.mdi
gb.form.print
gb.form.stock
gb.form.terminal
gb.gmp
gb.gsl
gb.gtk
gb.gtk.opengl
gb.gtk3
gb.gtk3.opengl
gb.gtk3.webview
gb.gui
gb.gui.opengl
gb.gui.qt
gb.gui.qt.ext
gb.gui.qt.opengl
gb.gui.qt.webkit
gb.gui.trayicon
gb.gui.webview
gb.hash
gb.highlight
gb.image
gb.image.effect
gb.image.imlib
gb.image.io
gb.inotify
gb.jit
gb.libxml
gb.logging
gb.map
gb.markdown
gb.media
gb.media.form
gb.memcached
gb.mime
gb.mysql
gb.ncurses
gb.net
gb.net.curl
gb.net.pop3
gb.net.smtp
gb.openal
gb.opengl
gb.opengl.glsl
gb.opengl.glu
gb.opengl.sge
gb.openssl
gb.option
gb.pcre
gb.pdf
gb.poppler
gb.qt4
gb.qt4.ext
gb.qt4.opengl
gb.qt4.webkit
gb.qt4.webview
gb.qt5
gb.qt5.ext
gb.qt5.opengl
gb.qt5.webkit
gb.qt5.webview
gb.qt6
gb.qt6.ext
gb.qt6.opengl
gb.qt6.webview
gb.report
gb.report2
gb.scanner
gb.sdl
gb.sdl.sound
gb.sdl2
gb.sdl2.audio
gb.settings
_Settings_Keys
Settings
_get
_new
_put
Clear
DefaultDir
Keys
Path
Read
Reload
Save
Write
gb.signal
gb.term
gb.test
gb.util
gb.util.web
gb.v4l
gb.vb
gb.web
gb.web.feed
gb.web.form
gb.web.gui
gb.xml
gb.xml.html
gb.xml.rpc
gb.xml.xslt
Controls pictures
Deprecated components
Developer Documentation
Development Environment Documentation
Documents
Error Messages
Gambas Playground
How To's
Language Index
Language Overviews
Last Changes
Lexicon
README
Search the wiki
To Do
Topics
Tutorials
Wiki License
Wiki Manual

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 creatable.

This class acts like a read / write array.

Static properties
DefaultDir   This property returns the default directory where settings configuration files are stored.

Static methods
FromString   Return a value from its settings internal string representation
ToString   Convert a value into its settings internal string representation

Properties
Count   Return the number of entries in the settings file
Keys   Return a virtual object used for enumerating all the keys used by the setting file.
Path   Return the path of the setting file.

Methods
Clear   Clears all settings.
Exist   Return if the specified settings key has a value
Read   Initializes the specified object from the settings file.
Reload   Cancels all settings changes, by reloading the settings file.
Save   Saves the configuration file to the disk.
Write   Writes the settings of the specified object in the settings file.

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.

Warning.
When you quit the program the settings are automatically saved but if your program is running in the background and your computer is shutdown it will hard exit and not save the settings. so if your program is likely to exit because of a system shutdown and not manually then you should use the Save function to save the settings as they are changed and not leave it for when the program exits normally as it won't exit normally.