Article Preview
Buy Now
COLUMN
Using AddHandler
Xojo has a way to connect events to handlers outside of a Window or sublcass.
Issue: 12.1 (January/February 2014)
Author: Seth Verrinder
Author Bio: Seth is a consultant who uses Real Studio every day in his job at BKeeney Software, Inc.
Article Description: No description available.
Article Length (in bytes): 5,235
Starting Page Number: 70
Article Number: 12108
Related Link(s): None
Excerpt of article text...
Usually you handle events in Xojo by adding code to the window that contains the control. Xojo automatically calls your code when the event is raised. This only works for objects that are placed on a window, though. What if you want to handle an event raised by an object that's not placed on a window?
AddHandler
andRemoveHandler
give you a way to do just that.Using a Timer
Let look at an example: say that you have a
Calendar
control that's a subclass of theCanvas
. Your calendar has anAddEvent
method that adds an event to your calendar and you want to automatically refresh the display when that happens. But someone might add a bunch of events and you don't want to repaint the canvas for each one. You could leave it up to the calling code to callRefresh
when it's done. Or, you could add a method that takes a list of events. Another approach is to start a timer inside ofAddEvent
. When the timer fires, it callsRefresh
.One place where this is handy is for handling
Timer
events. Normally you can just place a timer on your window and add code to theAction
event in the window. But, if you want to use a Timer that's not on a window, then you can't do that. There are a few options here:
- You can create a separate
Timer
subclass that knows about the class that you want to use it in. So, for example, if you have aCalendar
class, then you might createCalendarUpdateTimer
where you store a reference to the calendar that you want to update.
...End of Excerpt. Please purchase the magazine to read the full article.