Gambas - Programming and Distribution

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

In the first article of this series I described the free GUI programming environment Gambas, and how to set up the controls for a simple word-count program. This article looks at writing the code to make the program do something, and how to distribute the finished product

Gambas programming

Now we need to make the program DO something. Let's start with the menus. Click on the "Quit" entry in the File menu and a text area should appear with the cursor in the middle of a pre-named subroutine called PUBLIC SUB Quit_Click(). Whatever code we put in here will run when the user clicks the Quit item (easy, isn't it?). Enter the following code:

FMain.Close

and save the project. Run the project now and the Quit menu option should close the form for us.

Gambas Basic is an object-oriented language, which means that all our controls, including the form itself, carry baggage around with them in the form of properties and methods. Properties are settings that we can change, while methods are things that can be made to happen. One of the things that can be made to happen to the form is closing, and we just made it happen. Object-oriented programming can either be incredibly easy or horrendously difficult, depending on how far you want to go with it, but friendly user interfaces like this would be virtually impossible without it.

Add the following code to the other menu items:

Clear -- TextArea1.Text = " " Copy -- Clipboard .Copy(TextArea1.Text) Paste -- TextArea1.Text = Clipboard.Paste() In this case, Text is a property of the control called TextArea1, and Copy and Paste are methods associated with the system object Clipboard. Run the program and check that your code works so far.

Programming the word count

Programming our word count button is going to be a little more difficult, but not much. If you know some Basic or Visual Basic already you will find that most of it works in a predictable way within Gambas. If not, check the Web or hunt up a good book. There are a variety of ways we can get Basic to count the words in our box, but for the sake of simplicity I will do it by running through all the text one character at a time, counting the number of spaces, and adding one. The result won't be perfect, but it should be close enough. Click on the Count Words! button and enter the following code after PUBLIC SUB Button1_Click(). Gambas will take care of the spaces and capitalisation.

DIM i, w AS Integer

FOR i = 1 TO Len(TextArea1.Text)

IF Mid(TextArea1.Text, i, 1) = " " THEN

w = w + 1

NEXT

Label1.Text = w + 1

Here we are defining two variables as integers (whole numbers) and calling them i and w. As newly defined variables they start off with a value of zero. The Len() function gives us the number of characters in our text area, and the Mid() function pulls out each new character in turn. If it is a space character, the variable w is increased by one. Having looked through all the characters, we add 1 to the value of w for the last word (which isn't followed by a space) and put the final value in the label.Check if your program is working, make any corrections that are necessary, and save it.