Programming Gambas from Zip/SaveSettings

This section builds on the Radio Buttons exercise from the previous page.

Add Settings Saving to the Radio Buttons

edit

Gambas provides a neat way to save settings. Settings can be the path to the last data file, so it does not have to be relocated the next time the program starts. They can be anything the user typed or chose that you want to remember for next time. Here we shall save the selected radio buttons.

First, make sure the Settings component is enabled as part of your project. After starting a new QT graphical project, select Project Menu > Properties…, look through for the gb.settings component and tick it:

 

Use the same form as on the previous page (RadioButtons) with the fruit and transport buttons, but change the code to this:

Public Sub rbTransport_Click()
  Settings["Radiobuttons/Transport"] = Last.Text
End

Public Sub rbFruit_Click()
  Settings["Radiobuttons/Fruit"] = Last.Text
End

Public Sub Form_Open()

  Select Case Settings["Radiobuttons/Transport"]
    Case "Road"
      rbRoad.value = True
    Case "Sea"
      rbSea.Value = True
    Case "Air"
      rbAir.value = True
  End Select

  Select Case Settings["Radiobuttons/Fruit"]
    Case "Apple"
      rbApple.value = True
    Case "Orange"
      rbOrange.Value = True
    Case "Pear"
      rbPear.value = True
  End Select

End

Run the program. Select a transport and fruit. Close the program. Run it again: your choices have been restored. You could have your settings saved when the form closes. Gambas wiki has this example, showing how you can restore the window to whatever place it was last dragged to and whatever size it was resized to when last the program ran:

Public Sub Form_Open()  'Restore settings
  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 settings
  Settings["Window/Top"] = Me.Top
  Settings["Window/Left"] = Me.Left
  Settings["Window/Height"] = Me.Height
  Settings["Window/Width"] = Me.Width
End

Me means the current form.

Where are these settings actually stored? In your home folder is a hidden folder for settings called .config . In Linux any file or folder whose name starts with a dot is hidden. Look in .config for the Gambas3 folder. In it you will find a text file with the same name as your program. Open it and you will see the settings file.

 
The settings text file for the Radio Buttons application


Settings are neatly arranged under headings. Now you can see the significance of the string that has the slash in it: the first item is the heading. Settings["Radiobuttons/Fruit"] is the Fruit setting under the Radiobuttons heading.

You need to be careful: the very first time you run your program there may not be a settings file. If your form opens and looks for a particular setting when no settings file exists there will be problems. Test for empty (null) strings.

Saving a colour, a checkbox and the contents of a TableView

edit

   

On the form is a checkbox cbSurnameFirst, a panel Panel1, a label with the text “Choose colour:”, a colorbutton ColorButton1, a label Label1 whose text is “Fill”, colour blue and underlined, and a tableview tv1.

Run the program. Fill the tableview with random letters. Choose a colour. Highlight the completely useless button “Surname first”. Close the program. Run the program again. Settings are restored.

Public Sub ColorButton1_Change()
  Panel1.Background = ColorButton1.Color
  Settings["Colours/PanelColour"] = Panel1.Background
End

Public Sub Label1_MouseDown()

  tv1.Columns.count = 2
  Settings["TableView/Columns"] = tv1.Columns.count
  tv1.Rows.count = 4
  Settings["TableView/Rows"] = tv1.Rows.count
  For i As Integer = 0 To tv1.Rows.Max
    For j As Integer = 0 To tv1.Columns.Max
      tv1[i, j].text = Chr(Rand(Asc("A"), Asc("Z")))
      Settings["TableView/" & i & "," & j] = tv1[i, j].text
    Next
  Next

End

Public Sub cbSurnameFirst_Click()
  Settings["Names/SurnameFirst"] = cbSurnameFirst.Value
End

Public Sub Form_Open() 'restore settings

  Dim Surname As String = Settings["Names/SurnameFirst"]
  cbSurnameFirst.Value = If(IsNull(Surname), False, Surname)
  Dim nCols As String = Settings["TableView/Columns"]
  tv1.Columns.count = If(IsNull(nCols), 2, nCols)
  Dim nRows As String = Settings["TableView/Rows"]
  tv1.Rows.count = If(IsNull(nRows), 4, nRows)
  For i As Integer = 0 To tv1.Rows.Max
    For j As Integer = 0 To tv1.Columns.Max
      tv1[i, j].text = Settings["TableView/" & i & "," & j]
    Next
  Next
  Dim colour As String = Settings["Colours/PanelColour"]
  Panel1.Background = If(IsNull(colour), &hFFFFFF, colour)

End

IF Function

edit

There is a special form of the IF...THEN...ELSE statement that saves writing several lines of code. It is in the form of a function. These two are equivalent:

if IsNull(colour) Then
   Panel1.Background = &hFFFFFF 'white
Else
   Panel1.Background = colour
EndIf

is equivalent to

Panel1.Background = If(IsNull(colour), &hFFFFFF, colour)

In the one-line statement, the If(IsNull(colour), &hFFFFFF, colour) is one single thing. It is a number representing a color. Which colour? In the brackets are three items: a test that is either true or false, the answer if the test comes up true and the answer if the test comes up false. The pattern is if( TrueOrFalseThing, ValueIfTrue, ValueIfFalse). &hFFFFFF is the hexadecimal number for White (all red, green and blue LED lights fully on).

This is a sample settings file. On the left the checkbox is unchecked. On the right, the checkbox is checked.

   


Programming Gambas from Zip
 ← RadioButtons SaveSettings Modules →