Gambas - Introduction (Page 2 of 2)

Article by jonjermey (3,172 pts ) , published Apr 10, 2009

A simple program

The first step in creating a basic Gambas program is to design a form and put together the controls that you will need for it to function. Let's say, for instance, that you want to create a program which counts the number of words in a text area. The most basic requirements -- after the form -- are the text area itself, a button to click when you are ready to count the words, and a label to display the result. Each of these can be created with a tool from the Form controls area and stretched and moved to an appropriate position. They will be given names automatically -- Label1, TextArea1 and Button1. This names will also appear in the text areas and on the button as the default text. We can change the initial settings via the text properties shown at the right. The button text should say something like "Count Words", the text area should be empty, and the label should be set to zero. The illustrations below show the design process, and the result that appears when the user presses the Run button in the main toolbar.

A simple program

Our basic form

The Menu Editor

Menus are created in Gambas with the Menu Editor. Open the project from the previous article and double-click on "FMain" at the left to bring up the form. In the small toolbar running across the top of the form there is a button labelled "Menu Editor." Click on it to bring up the menu editing panel. We are going to add two menus to our form: one File menu with a single entry "Quit" which closes the program, and an Edit menu with three options: "Clear," which clears the text area; "Copy," which copies the text in the text area to the Clipboard; and "Paste," which pastes the contents of the Clipboard into the text area.

Menu items have a name, which identifies them to the program, and a caption which appears to the user. If you add an ampersand to the caption -- e.g. "C&lear" -- the resulting menu item will appear with an underline below the letter following the ampersand, and will automatically be linked to an Alt-key combination -- in this case Alt-L. You can use the Menu Editor to link menu items to other shortcut keys as well.

Create the menus shown below, then close the Menu Editor. Don't worry at this stage about the Action setting.

This is a good spot to save. In Part Two of this series I will describe how to add code so that our new program actually does something.

Menus

Menu Editor