![]() | ||
Introduction Instantiation Events & Event Handlers Global Methods & Properties | ||
What are they? An Event is simply something that happens while the program is running. Events allow a program to react to things such as the user doing something (eg. pressing a radio button) or something happening during the running of the program (eg., the need to display a Message Box to ask the user what he/she wants to do as a result of something happening). RB, being an Object Oriented Program ("OOP"), is said to be "Event Driven". That is different from a conventional program written in standard BASIC (or in many other languages) which often starts executing at the beginning and proceeds step-by-step to the end with relatively little intervention by the user. The code in an RB program however is divided into separate blocks of code. When associated with an Event, the block is called an Event Handler. When an Event is triggered, RB automatically causes some computer code (if there is any) in its associated Event Handler to be executed so that a desired result will occur. (An Event Handler is a Method with a special name.) For example, consider an Edit Field.
| ||
![]() | Each of these three things causes RB to trigger an Event called "MouseEnter", "MouseDown" and "TextChange". (The Mac's Operating System also triggers its own Events which create a blue border around the Edit Field etc. but we are not considering the Mac's OS here.) When RB triggers each of these Events, it looks to see if there is any code in the associated Event Handler. If there is it will be executed and the desired results will occur. What those results will be depend on that code (if there is any) which the programmer will have written. In the image nearby the only Event Handler that has been written for EditField1 is for its TextChange Event. RB triggers this Event when the user (or some code) changes the contents of the Edit Field's Text | |||
Property, recording the fact in the Project that the text has changed. Its code (written by you) has Set a "Flag" (changed it to "True") called "B_TextHasChanged". Why would it be important for this Event to be triggered? Well, imagine that you have designed a word processor. The text would be held in a large Edit Field the same size as the word processor's window. If any change is made to that text, RB must at the very least remember later on, when the user tries to quit the word processor, to display a Message Box saying something like "Are you sure that you want to quit? If you do, you will loose all recent changes." The user therefore will have the opportunity to Save the changed text. The fact (that the text has changed) must therefore be stored for use somewhere else in the program, and after the changed text has been Saved, code in that "somewhere else" must reset the Flag to False. Also, the Save and Save As... Menu Items should be disabled (dimmed) if that Flag is Set to "False" - there is no point in offering Save operations if there is no fresh text to save. The code in an Event Handler could be as complex and sophisticated as you wanted. You could start World War III if you liked. For example, in a program to generate all prime numbers between a range from low to high, three Edit Fields would respectively hold the lower and upper ends of the range of numbers to be tested, and the list of primes generated. There would be a Pushbutton ("PushButton1") Captioned "Calculate", and the EventHandler "Action" for "PushButton1" would hold the code for:
This would involve about 15 lines of RB code. It should be noted that RB's Browser (the section that appears to the left of a window's Source Code window) shows only one category called "Events". That category lists Window Events. However, it is clear that if you open a Control's Disclosure Triangle and the Menu Handler's Disclosure Triangle, what you see are also Events. This confused me at first. It would be better of RB listed them as such. | ||
MORE ON HANDLERS | ||
The following diagram sets out what I think is the relationship between the different types of Handler from my beginner studies of RB. First however, a few explanations. A "Control" is an item that appears in a window. A Control can have its own code (Event Handler) which will be executed when its associated Event is triggered. Examples are Edit Fields, Pushbuttons, Radio Buttons, Rectangles, Timers - all the things in the Controls Palette. Windows, and Menu Items, are not Controls. "Method" is a general term covering a piece of code which when executed produces a result. It can take the form of either a Subroutine or a Function. It can be called an Event Handler, or a Menu Handler, if it is associated with an Event, or with a Menu Item. Examples of a Method include a Function to calculate something and Return the result, or a Subroutine to move something. It is convenient here to classify a Method's code along with Event Handlers code in the diagram below because it will be executed when the Method is Called, rather like when a Control or Window Event Calls its Handler. (However, Methods do not themselves have Handlers) | ||||
HANDLERS | |||||||||||||||||||||||||
![]() | |||||||||||||||||||||||||
CONTROL EVENT HANDLERS | WINDOW EVENT HANDLERS | MENU | METHOD | ||||||||||||||||||||||
CREATED BY: | RB * | PROGRAMMER | PROGRAMMER | ||||||||||||||||||||||
CODE WRITTEN BY: | PROGRAMMER | |||||||
PROGRAMMER | PROGRAMMER | |||||||
RB'S CODE | ||||||||
TRIGGERED BY: | RB'S CODE | PROGRAMMER'S CODE | ||||||
EXAMPLES: | ||
PUSHBUTTON | OPEN | FILE > OPEN | ||||||
WHATEVER YOU LIKE | ||||||||
* If you create a new Class, you can create Events to go with it. See http://realgurus.com/board/viewtopic.php?t=90 | ||
The Event system effectively works like this: The user, or the code somewhere in the Project, does something. This could, for example, be caused by the user pressing Pushbutton1 or pressing the Close button on Window1, or the code issuing a Message to close the window. RB immediately detects this as an Event and notices its as "Pushbutton1 has been pressed" (note - not "a pushbutton has been pressed"), or "Window1 must close" Event (not a window). RB sends a Message to the appropriate Event Handler - Pushbutton1's Action Event Handler or Window1's Close Event Handler in this example - saying "Execute now". If you (the programmer) have written some code in that Event Handler it will be executed and produce the result that you want (hopefully). This could be to calculate the day of the week of 25 December 2055 (the pushbutton example), or to open another window and position it on the right of the screen and change the text in one of its StaticText Controls (the window example). In the case of the window example, (its close button being pressed), it seems that RB automatically sends an "internal" Message, within its own code, causing the window to actually close. You do not have to write code for that. (Windows are rather different from other Objects in RB.) Menu Events and their Menu Event Handlers essentially work the same way, triggered when the user chooses a Menu Item - eg., File > Open. (They are listed in the Browser as Menu Handlers - it would have been more logical - and easier for the beginner to understand - if they had been listed as Menu Events, their Handlers being revealed by opening their disclosure triangle, as with windows' Events.) Your body has Events. You touch something hot. The nerves in your finger trigger an Event called "Burn". A Message travels up your nerves to your brain where the code in your "Burn" Event Handler executes, sending a Message to your arm muscles to pull back, and another Message to your mouth to shout. Download WindowDemo.rb and EventsDemo.rb Run the first program (in the IDE for convenience), and the second to see and hear various Events being triggered. Also, examine its code. Have a look at the Flow Diagram to see how the Events system works in a demo program and which you can download there and examine in detail. | ||
Introduction Instantiation Events & Event Handlers Global Methods & Properties | ||