Real Logo
REALbasic Home
Thank you for using this example tutorial ... It is not associated with REAL Software.     powered by ... a desire to share

Introduction to REALbasic for the Mac

Learn By Example - Beautiful

My Other RB Pages
Sample RB Projects and Code
My Newbie RB Tutorial - Page One

space space
Example Two: Control Events
by Thomas J. Cunningham

  space space

An Eventful Project
This REALbasic Project Code Is Here

"When these events occur is handled by the built-in events of REALbasic."
This is one of the last lines from my Example One ( ... here ...).
Most of the tutorials for REALbasic I've read, assume we as newbie programmers, already know what an Event in REALbasic is, for example, "MouseDown". Ok, when a user clicks their mouse down this Event happens, that makes sense, right? Well lets check and see, shall we?
Most events are aptly named, but we should learn what and when these Events are called and used in our REALbasic programs. After all, if we write some code in the Open Event handler, shouldn't we know what and when this Event occurs?

So I propose we investigate the Events associated with a Canvas class. Lets put up some flags in each event listed in the code editor for a Canvas. A flag is just a term that means we some how notify ourselves (or our program) that something has happened.

The simplest of flags is to simply type "beep" in an event handler. When that particular event fires (occurs), we will hear a sound. Our canvas however has 10 different events listed and we would not know what event was associated with the many beeps we would hear, if we type "beep" in all ten events.

The next simplest approach would be to use the Message Box Method. We could code:
msgBox "The MouseMove Event just occured."
in the MouseMove event and every time this event happens, we would see the message box repeat this message to us. Each of the ten events would have their own unique message. This too works very well, but it interrupts the program a lot as we test it out.

This project is not very sophisticated, there are many way to "dump" this information for evaluation. An experienced programmer would be amused by this example, but it certainly suits the needs of us neophyte programmers.
The Project Code Is Here
Into our main window1, I dragged a canvas from the tools window, canvas1. I added some StaticText fields to reflect the Events I would like to follow.
I first thought it would be nice to have a StaticText tell me a particular event was occurring. I would merely put the code that changes the text to read something like, "OK the MouseEnter Event just fired.". I created labels via StaticTexts and into each appropriate event, I changed the answers from code. This worked fine, but once the event was over with, I needed to change the answer back to something like: "Ok now I'm not firing.".

Events happen very quickly from a Mac stand point. Too quick for us to see when the texts change from off to on, then back to off. You only see that it is off all of the time. We need to delay the response. So I dragged a timer in to the window and in to its action event, I reset the StaticTexts to "Off" after a period of time. This way there is a period of time that we can see that the Event did in fact occur. Like I said, its' a bit unsophisticated, but it works fine to help understand what and when the Event occurs.

The second technique of our Event monitoring project, is to drag a ListBox in to our window1. This will be where we will create a diary of events. I use this technique a lot. It's very easy to do and I find it very helpful. Gosh, it's hard to believe that my perfect code doesn't do what I thought it would!

In to each Event of canvas1, I added this code:
listBox1.addrow "Paint Event" This shows the code for the paint event. Each Event has its own unique descriptive text that relates to its Event. Now we have an Event record. Our listbox will, line by line (addrow-Command One-look it up in the ListBox refrence-grin), show us which Events occurred and in what order they fired in. Cool. A very simple way for us to have a record of Events. We can now gain some insight in to what an Event is and when it happens.

screen shot Observations
Run the project and experiment by moving your mouse around the window in and out of the canvas1 area.
Please note that I added a color background and a rectangle behind the canvas to make it clear where the canvas outline is in our window1.

  • Remember, these Events are in the Canvas1 area, not the window itself. As an exercise, make an event record of window1, by adding another Listbox.
  • When you first run the project, a message box appears indicating the Open Event occured for the canvas. Look at the ListBox entries, Paint-Open-Paint. The Paint Event is called first then the Open Event, then the Pint Event again. The Paint Event is called everytime another window (the message box) interacts with our main window, so it is called again after the message box is dismissed. The Paint Event "refreshes" the canvas. This follows what is documented in the REALbasic explanations of when a Paint Event occurs.
  • Click and hold your mouse down outside of the canvas and drag it across the canvas. Note the events listed in the listbox and the StaticTexts. Now click and hold your mouse down starting inside of the canvas and drag it out side of the canvas. Wow, look at all of those MouseDrag events! Apparently, as long as you hold your mouse down, regardless if it is moving or not, a mouse drag event is occuring.
    So what you say? Maybe it is nothing, but perhaps in the future when you are trying to get your program to do something at a particular time, this information will come in handy.
  • Be sure and experiment with other objects and their Events, including the Events of window 1.

Summary REALbasic is an Event driven programming application. Your Mac is an Event driven computer. Your computer, sits there, waiting for you, the user, to perform some event. Or even to do nothing for awhile in the case of a screen saver.
Having an understanding of Events will help you to decide, "I wonder where I should put this code?". Or, perhaps you write a program and it reacts to something in a completely unexpected manner than you intended. This of course never happens to me .... [grin]. Perhaps it's just because the code was placed in the wrong Event.
Writing your own programs that explore what is behind how REALbasic and your computer works, is fun, beneficial to your leaning experience and confidence building.

Our next example project will expand on these techniques by using some of the parameters passed to these Events. e.g the X as Integer, Y as Integer parameters of the MouseMove Event. Stay tuned!


Project Code:

Sub DropObject(obj As DragItem)
' this event not tested here.
End Sub

Sub Close()
msgBox "Canvas1 has closed."
End Sub

Sub Open()
' turn on our timer to reset the statictexts
msgBox "Canvas1 Has Opened!"
listBox1.addrow "Canvas Open Event" ' log the event
End Sub

Sub MouseExit()

ExitAnswer.text= "Mouse Has Exited" 'show me
listBox1.addrow "MouseExit Event" ' log the event
' turn on our timer to reset the statictexts
ClearSettings.Mode=1 ' mode 1 fires the timer once
End Sub

Sub MouseEnter()
EnterAnswer.text= "Mouse Enters Now" 'show me
listBox1.addrow "MouseEnter Event" ' log the event
End Sub

Sub MouseMove(X As Integer, Y As Integer)
MoveAnswer.text= "Mouse is Moving Now" 'show me
listBox1.addrow "MouseMove Event" ' log the event
End Sub

Sub MouseDrag(X As Integer, Y As Integer)
DragAnswer.text= "Dragging Mouse Now" ' show me
listBox1.addrow "MouseDrag Event" ' log the event
End Sub

Sub MouseUp(X As Integer, Y As Integer)
UpAnswer.text="Mouse is Up" ' then let me know
listBox1.addrow "MouseUp Event" ' log the event
End Sub

Function MouseDown(X As Integer, Y As Integer) As Boolean
DownAnswer.text="Mouse is Down" ' then let me know
listBox1.addrow "MouseDown Event" ' log the event
return true ' see above
End Function

Sub Paint(g As Graphics)
beep ' let me hear when this happens
listBox1.addrow "Paint Event" ' record the event
g.foreColor=rgb(255,0,0) ' pick a color -red
g.fillrect 0,0,me.width,me.height ' fill in a rectangle the same size as the canvas.
' same as:
' 0,0,me.width,me.height
End Sub

Sub Action()
End Sub

Sub Action()
Downanswer.text= "Not Down"
DragAnswer.text="Not Dragging"
UpAnswer.text="Not Up"
MoveAnswer.text= "Not Moving"
EnterAnswer.text= "Not Entering"
ExitAnswer.text= "Not Exiting"
End Sub


Find This Useful / Not ? Email Me :

Copyright 2001,Thomas J. Cunningham