Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Programming Gambas from Zip/SaveSettings

From Wikibooks, open books for an open world
<Programming Gambas from Zip

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

Add Settings Saving to the Radio Buttons

[edit |edit source]

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:

PublicSubrbTransport_Click()Settings["Radiobuttons/Transport"]=Last.TextEndPublicSubrbFruit_Click()Settings["Radiobuttons/Fruit"]=Last.TextEndPublicSubForm_Open()SelectCaseSettings["Radiobuttons/Transport"]Case"Road"rbRoad.value=TrueCase"Sea"rbSea.Value=TrueCase"Air"rbAir.value=TrueEndSelectSelectCaseSettings["Radiobuttons/Fruit"]Case"Apple"rbApple.value=TrueCase"Orange"rbOrange.Value=TrueCase"Pear"rbPear.value=TrueEndSelectEnd

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 hasthis 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:

PublicSubForm_Open()'Restore settingsMe.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]EndPublicSubForm_Close()'Save settingsSettings["Window/Top"]=Me.TopSettings["Window/Left"]=Me.LeftSettings["Window/Height"]=Me.HeightSettings["Window/Width"]=Me.WidthEnd

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.

Gambas Settings File example showing in Kate
The settings text file for theRadio 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 theFruit setting under theRadiobuttons 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 |edit source]

Saving settings for other objects in GambasSaving settings for other objects in Gambas, running

On the form is a checkboxcbSurnameFirst, a panelPanel1, a label with the text “Choose colour:”, a colorbuttonColorButton1, a labelLabel1 whose text is “Fill”, colour blue and underlined, and a tableviewtv1.

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.

PublicSubColorButton1_Change()Panel1.Background=ColorButton1.ColorSettings["Colours/PanelColour"]=Panel1.BackgroundEndPublicSubLabel1_MouseDown()tv1.Columns.count=2Settings["TableView/Columns"]=tv1.Columns.counttv1.Rows.count=4Settings["TableView/Rows"]=tv1.Rows.countForiAsInteger=0Totv1.Rows.MaxForjAsInteger=0Totv1.Columns.Maxtv1[i,j].text=Chr(Rand(Asc("A"),Asc("Z")))Settings["TableView/"&i&","&j]=tv1[i,j].textNextNextEndPublicSubcbSurnameFirst_Click()Settings["Names/SurnameFirst"]=cbSurnameFirst.ValueEndPublicSubForm_Open()'restore settingsDimSurnameAsString=Settings["Names/SurnameFirst"]cbSurnameFirst.Value=If(IsNull(Surname),False,Surname)DimnColsAsString=Settings["TableView/Columns"]tv1.Columns.count=If(IsNull(nCols),2,nCols)DimnRowsAsString=Settings["TableView/Rows"]tv1.Rows.count=If(IsNull(nRows),4,nRows)ForiAsInteger=0Totv1.Rows.MaxForjAsInteger=0Totv1.Columns.Maxtv1[i,j].text=Settings["TableView/"&i&","&j]NextNextDimcolourAsString=Settings["Colours/PanelColour"]Panel1.Background=If(IsNull(colour),&hFFFFFF,colour)End

IF Function

[edit |edit source]

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:

ifIsNull(colour)ThenPanel1.Background=&hFFFFFF'whiteElsePanel1.Background=colourEndIf

is equivalent to

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

In the one-line statement, theIf(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 eithertrue orfalse, the answer if the test comes uptrue and the answer if the test comes upfalse. The pattern isif( TrueOrFalseThing, ValueIfTrue, ValueIfFalse).&hFFFFFF is the hexadecimal number forWhite (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.

Settings text file showing an unticked checkboxSettings text file showing a ticked checkbox


Programming Gambas from Zip
 ← RadioButtonsSaveSettingsModules → 
Retrieved from "https://en.wikibooks.org/w/index.php?title=Programming_Gambas_from_Zip/SaveSettings&oldid=4475009"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp