A Beginner's Guide to Gambas, Revised for Version 3

Support for questions or comments about the book

Some links for Gambas:

Gambas BASIC home page, with 10 language options:  http://gambas.sourceforge.net/en/main.html



German:  http://gambas-club.de/index.php

Indonesian/Bahasa: http://www.MarkasGambas.blogspot.com

Italian: http://www.gambas-it.org/wp/


Filed under: Comments/Questions

Some useful Gambas usages and examples

PDF in Gambas — I review the Search Engine Terms that show up on people’s searches that bring people to this blog.  One of the terms that shows up frequently is “Gambas .PDF”, “Gambas open .pdf”, “Gambas save .pdf”, or something similar.  For anyone who is interested in interfacing Gambas with .PDF files:

  • You can find information about the gb.pdf component at “http://gambasdoc.org/help/comp/gb.pdf?v3”
  • A “PDF Printer” is an included installed printer on every GUI distribution of Linux that I’m aware of.  If it is not included with your distro, it can easily be installed from the repositories.  An easy way to generate a .pdf file from inside your program is to write a printing routine, and the user can select “Print to File”, and select the .PDF file format.  To make it easier, you can link a button control directly to your print routine and make the PDF printer the default choice.  You can see an example of this in Download #4 in the Flash Widget — the  “Screenshooter” program includes a pre-selected “Save as PDF” print-to-file option.

Filling list controls from arrays — Just in case anyone has not noticed this, you can assign an array list to a list-based control in Gambas:

  • “ListBox1.List = strSomeKindOfArray”

That’s really fast and convenient.  However, you can’t assign the ListBox1 contents to an array:  “strSomeKindOfArray = ListBox1.List” will not work — and it will not give you an error message!  You must use some kind of loop like “FOR…NEXT” to transfer the control list to an array.

Some notes about Key functions:  The Web-based documentation for Gambas is really good (much improved from the past).  But some things need more explanation.  We didn’t cover everything in detail in our book “A Beginner’s Guide to Gambas”, because … you know, “…Beginner’s…”  Here is some clarification about the Key component that might be useful.

  • Using Key.Code — You can only use Key.Code inside a Sub for a _KeyPress or _KeyRelease event:

Public Sub TextBox1_KeyRelease()  ‘When TextBox1 has the focus, if you hit Return or Enter, it will be the same as clicking Button1.

__If Val(TextBox1.Text) Then   ‘If only numerical values have been entered.

‘Note that the “Return” key is the “enter” key near the center of a standard desktop PC keyboard, with the “return” symbol.
‘The “Enter” key is the “enter” key at the right side of a standard desktop PC keyboard, with the numeric keypad.
‘The keys are equivalent in function, but each one returns a different key code.
____If Key.Code = Key.Return Or Key.Code = Key.Enter Then
______Button1_Click()  ‘Here, we call the Sub Button1_Click as if we actually had a Button_Click event.  It is not necessary for the user to trigger the actual event.

  • “Key.Code” returns an integer code for each key, and String.Chr or CHR$ converts that integer into a character.  Therefore, to capture the key that was pressed on a keyboard (again, it must be inside a Sub for a _KeyPress or _KeyRelease event), you can use this syntax:

strChar = String.Chr(Key.Code)

Have radiobuttons with no radiobutton selected:  Unlike CheckBox controls, RadioButton controls automatically de-select the previously selected button, when you click on another RadioButton in the same container, but one of them will always be selected.  Have you ever wanted to have no RadioButton selected?  You can do that by creating one more RadioButton than you need, and make that RadioButton invisible — “RadioButton7.Visible = False”.  Then, you can open a form with RadioButtons, but you can set the focus to the invisible RadioButton — “RadioButton7.SetFocus”.  In the course of the program, you can use that statement to transfer the selection back to that invisible RadioButton, if you want to.

Shell to multi-line Linux commands, and receive output from interactive Linux commands:  When you want to use a Linux program and capture its output (like “ps aux”), or use the sudo command (which requires that you issue the “sudo…” command, then follow up with a password after you are prompted), the Gambas “Shell” function will work fine.

  • The Shell statement below allows you to run the Linux commandline program to find running processes, and the TO keyword allows you to capture the output in a string.  You can parse the string with the Split function, etc., display the data in one of the View controls (like ColumnView or GrindView), and analyze the processes — maybe manage the processes, like killing a process that is hogging too much memory or processor time:  see http://gambasdoc.org/help/comp/gb/process?v3

Shell “ps aux” TO strShellResponseString

  •  In the Shell statement below, the -S (stdin) option after “sudo” causes sudo to read the password from the standard input instead of the terminal device.  In this case, the standard input reads from your program.  The password must be followed by a newline character.
    In total, the construction below is a shell script construction meaning: run the command sequence “sudo -S chmod 0777 ~/myfilename”  and redirect (<< EOF) the string “mypassword” into its standard input after that sequence, until you hit a line with “EOF” on it, which signifies the end of input to the interactive shell.
    The shell will recognize “\n” as the newline code — there must be NO extra spaces before or after “\n”, because empty spaces will be interpreted as part of the script or part of the password.  The TO keyword will assign the output of this command to your string variable “strShellResponseString”, in case you want to display that in your program — for example, if you input the wrong password, sudo will give you an error message.

Shell “sudo -S chmod 0777 ~/myfilename << EOF\nmypassword\nEOF” TO strShellResponseString

Just a few interesting Gambas usages that were not obvious from the online documentation.

Filed under: Comments/Questions, , , , ,

New sample project — Screenshooter, a full-featured screenshot program

UPDATED 2013/1/10:  I revised the code:

1.  Now the screenshot window will always rise to the top of the stack after the screenshot.

2.  If a target window is partially off of the screen, the program will reposition the window so that a complete screenshot can be made.


A third sample program has been added to item #4 in “Free Downloads Below!”.  That project is named “Screenshooter”.  It is a full-featured screenshot program that makes use of the new features in Gambas3 and the Qt4 toolkit.  “Screenshooter” also makes use of the “Desktop” component of Gambas3, so that you can access the X11 windows list, and activate a window.  This program offers 3 ways to make screenshots:

1. Use spinboxes to precisely set the X and Y coordinates and the Width and Height of the screenshot area (View Window).  This gives pixel-by-pixel control over the screenshot area.

2.  Use drag-and-drop features of the View Window to position the screenshot area to the proper screenshot geometry.

3.  Make a screenshot of a specific window in the X11 window list, without bothering to set any parameters.

4.  You can print the screenshots to paper, or you can save the screenshots in either .JPG or .PDF format.

NOTE:  I felt that the existing Gambas3 documentation about printing was not very clear, so both “NorthStar Books” and “Screenshooter” have comments in the Gambas3 code that explain how to set the page parameters for print jobs.

Filed under: Comments/Questions

New program in “Free Downloads Below!”

Item #4 in “Free Downloads Below!” was previously only “NorthStar_Books”.  Now, the tar.gz archive includes both “NorthStar_Books” and “CONVERT2”, a units conversion program.  The units chosen for “CONVERT2” are ones that I frequently use, so users will want to revise it for their own needs.  Both of these programs are written in Gambas3.

In the near future, another program will be added — Formul8, a program for rubber chemists/compounders to write and maintain rubber compound formulas.

Filed under: Comments/Questions

NorthStar Books — small business bookkeeping for cash-based accounting

Corrections made on 2012/8/29:

In Reports.Class:

1.  Add the code block that determines the “Draw” that is included in the “Multi-” payments.  Without this number, the Profit/Loss statement reported too little “Total Expenses”, which caused “Net Income” to be too large.

In these multi-part payments (usually credit card payments), the “Draw” is calculated in the main program — it is not held as data in the sub-payment data file.  This is an advantage because if you modify a sub-payment record, you don’t need to modify the “Draw” part of the record.  However, this calculation was missing from Reports.Class, causing the error on the Profit/Loss report.  This problem is now fixed.

Corrections made on 2012/7/20:

In FMain.Class:

1.  In Public Sub Form_Open(), move the line InfoHandling.LoadAccountsListFile() above the line LoadColumnView1(bReloadTF)

This allows ComboBox2.Text to show the account of the selected record when the program opens.

2.  In Public Subs Button6_Click and Button7_Click, change the calls from InfoHandling.WriteFile(bReloadTF) to InfoHandling.WriteMasterFile(bReloadTF)


NorthStar Books:  a sample program, free for your examination or use.  This is a program that I wrote to run my own business bookkeeping.

I have changed the downloads in the Box.com “flash_widget”  window to the right.

  • Gambas2 and Gambas3 examples are now contained in a single .tar.gz file, in item #3.
  • Item #4 is NorthStar Books.  This download contains the Gambas3 source project “NorthStar_Books”, some “fake” data so that the program can be evaluated, a “readme” file, and a folder containing files needed for installation of the executable for this program (if you want to install it).  I recommend just running the project from the Gambas 3 IDE, and you may see things that you may want to change.

This project is offered as free software under the GNU GPL Version 3 license.

After downloading, see the file “README_NorthStar_Books.txt” to read about how to set up the “fake” data files to explore the operations of the program.

Best regards,


Filed under: Comments/Questions

Having trouble with the Gambas IDE text spacing?

If you are using XFCE/Xubuntu, you may have a problem with the code editor,
where the spacing of the text does not correspond to the cursor position
in a line of code.  I had this problem, and I found the solution on this page:

The relevant part is:

Comment 3 by zachsmit…@gmail.com, Jan 16, 2012

I've solved the problem by changing the font hinting in 
Xfce (Settings-> Appearance-> Fonts-> Rendering-> Hinting). 
If anti-aliasing is enabled and hinting is set to 'none or 
'slight' then it causes problems in the Gambas IDE.  
If it is set to 'medium' or 'full' then there is no problem. 
Most running apps are affected immediately by changing 
the hinting but the IDE apparently only uses the 
setting at start up.  Changing the hinting while the 
IDE is running has no effect. It took a while to figure 
it out because among the apps I use, only Gambas 
was affected by this.

Filed under: Comments/Questions

Summary table for common Form Elements

Because I often forget which control is best for some purpose, I created a table of the Gambas3/Qt4/GTK+ Form Elements and I have added it to the Downloads widget, as download #5.  Everyone should feel free to download this table, modify and translate it, and distribute it.

Best regards, minnesotajon

Filed under: Comments/Questions

Adjustment to code on page 54, for Gambas3 only

Thanks to “stanks”, who downloaded the book, started working with it, and encountered a problem related to a change in Gambas3.  On the web page “Array Declaration“, note that “In Gambas 3, embedded arrays cannot be used as local variables anymore. But they can be public!

Therefore, on page 54, when you are using Gambas3, the code should read:

Public narMatrix[3, 3, 3] As Integer

Public Sub Main()
Dim i As Integer
Dim ii As Integer
Dim iii As Integer
For i = 0 To 2
For ii = 0 To 2
For iii = 0 To 2
Print i, ii, iii & ” ==> “;  ‘note that “;” prevents a newline.
narMatrix[i, ii, iii] = i * 9 + ii * 3 + iii
Print narMatrix[i, ii, iii]

If you don’t make this change, you will get the error message “Embedded arrays are forbidden here in MMain.module“.

Also, note that declaring the array “PUBLIC” means that you don’t use the keyword DIM — “PUBLIC” automatically does the dimensioning .

Filed under: Comments/Questions

Install Gambas3 using Synaptic or Ubuntu Software Center

If you are using one of the “Buntus” (Ubuntu, Xubuntu, Lubuntu, Edubuntu, etc.), you can add an “untrusted” PPA to your Software Sources, in order to download and install Gambas3 using Synaptic or the Ubuntu Software Center.

To add the PPA, open your terminal and type this line:  sudo add-apt-repository ppa:gambas-team/gambas3

After you have added the PPA, open Synaptic or the Ubuntu Software Center, and the Gambas3 packages can be installed automatically, and updated whenever updates are available.

To purchase the paperback version of the book, you can click on the book cover to the right. ===>

Filed under: Comments/Questions, , , , ,

Update — end of 2011

In total, there have been more than 1,700 visits to this blog, and more than 1,500 downloads of Beginner’s Guide to Gambas, Revised for Version 3.  We are delighted that programmers are showing so much interest — please let your programming friends know that this book is available as a download for free.

To purchase the paperback version of the book, you can click on the book cover to the right. ===>


Filed under: Comments/Questions


1. A Beginner's Guide to Gambas - Revised for Version 3 -- .PDF version, OCL license. The complete book!

Click on this link: FREE DOWNLOAD

A Beginner’s Guide to Gambas, Revised for Version 3

Click on the book cover to order the paperback book at Infinity Publishing!

Click on the shrimp to go to the main website for Gambas!

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 78 other followers