Article Preview
Buy Now
COLUMN
Creating Custom Events 2
Adding events to your classes
Issue: 17.3 (May/June 2019)
Author: Marc Zeedar
Author Bio: Marc taught himself programming in high school when he bought his first computer but had no money for software. He's had fun learning ever since.
Article Description: No description available.
Article Length (in bytes): 11,942
Starting Page Number: 70
Article Number: 17005
Resource File(s):
project17005.zip Updated: 2019-05-15 15:40:54
Related Link(s): None
Excerpt of article text...
In the last issue, I demonstrated using custom events in a traditional way by demonstrating an on/off control that fired a
clicked
event when clicked. But events can be used for non-controls, too, and that can produce some interesting behavior.Events are basically just messages, one object telling another that something happened. This is a great feature of object-oriented coding as you can put code that only gets run when the event happens in that event's handler. That gets the code out of the way of other methods and keeps it right where it needs to be.
In general, events get fired when the
user does something, like click or scroll. But events can be triggered in other ways. For instance, you could fire an event when a long calculation is completed or a download is finished. Both of those are examples of things that take an unknown amount of time so an event alerting the user or another object when it is done is useful.You could even use events to chain a series of actions together. This could happen without user-interaction if that was appropriate. Take, for instance, a "watch folder" scenario.
A "watch folder" is where your program watches a folder to see if something is put in it. When a file is added, the program might do a series of processes on that file, perhaps varying according to the file type. In a graphic design business, for example, there might be a lot of automation. Graphics might have their color adjusted for a particular paper and printing press while text files might be converted to XML or tagged in some way. Often there's a series of steps. The graphic might be resized, text could be manipulated to a certain standard, and so on.
Your program could watch for the folder and when a new file is added, it would run the appropriate actions. It could do this with events. When the "watch folder" portion of your program (a custom timer that checks the folder for changes every few seconds) sees a new file, it fires a "file dropped" event.
...End of Excerpt. Please purchase the magazine to read the full article.