Suite101

Events in VBScript


© Maxim Karetnikov

Events are mostly a result of something you do. You click the mouse on a button, for example, or you press the key in a dialog box. You are not the only source of events, however. Windows also causes events such as a timer event that occurs each time Windows internal clock ticks. The Web browser creates events, too, such as an event that occurs each time it loads a Web page.  Events in Windows generate messages. Messages in Windows tell the applications and the operating system what to do and what is going on. Along with message, Windows sends any data that the object needs to better understand the message such as the mouse's position when you clicked a window. The object's message loop transforms that message to the bit of code that handles the message. Objects which can receive these messages include windows, dialog boxes, buttons, images and so on.

Events have a lot of benefits over the old style of programming. The biggest benefit is the fact that you stay in control of your computer. Here are the main points:
  • Events let you determine the order in which a program executes.
    Clicking different parts of the screen, such as buttons, menus, and windows, causes different bits of code to execute.
  •  When an event-driven program is waiting for an event, it is not using much of the computer's resources.
    This lets you do other tasks such as run other programs. Event-driven programs have a message loop that checks for messages, and if no messages are waiting, they return control back to the operating system to run another programs.
  • Events simplify programming.
    Instead of a  constantly checking each input device, it waits for the operating system to send it a message.
  • Events allow programs to work with objects that define their own behavior.
    The operating system simply forwards messages to an object, and the object's event handlers determine how to handle it. The program doesn't need to know how the object works internally; it just needs to know how to talk to it.

An object doesn't always know how to handle every message, either. A window may not understand what to do with a message telling it that the user is moving it. In these cases, the object gives the message back to Windows and tells it to handle the message. Windows doesn't do events itself. It does messages. The objects cause events in response to something you do to it. In VBScript, a button object causes a click event when it receives a mouse click message from Windows. Likewise, an edit box causes a change event when it receives keyboard messages from Windows that change the contents of the field.

Go To Page: 1 2


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo