Introduction to PowerBuilder

HomePrevious Lesson: Cascaded Events and Functions
Next Lesson: Posting an Event

Does TriggerEvent Really Trigger an Event?

What happens when you use a TriggerEvent()? What happens is that PowerBuilder executes script for the specified event. Does it mean we are really triggering the event? No, it's not. What does that mean? Without triggering how does the script written for an event get executed? Good question. Before explanation, let's see an example.

Let's take a simple example of window close event. If you trigger it, what you expect to happen? We expect, the window to close. In reality, that doesn't happen. When calling TriggerEvent() or PostEvent() (explained in the next topic), PowerBuilder puts the message at the top of PowerBuilder's internal object queue and then executes it. In other words, PowerBuilder does not place the message in the MS-Windows message queue. To test the above theory, place a CommandButton in the w_script_practice window and name it 'cb_event_test' and for the clicked event write Parent.TriggerEvent(Close!). Write MessageBox("Hello!","I am in Window Close Event") to the window's close event. Then run the window and click on the button. Do you see the window closing when the button is clicked. No!, it simply displays the message box.

To really close the window, we need to send a MS-Window's close message to MS-Windows. We need to call Send(). The format is as follows:
Send(Handle, MessageNo, LowWord, Long)

Handle is the handle of the window to which you want to send the message. You can get the handle of the window by calling Handle(). MessageNo is the Windows message number.

Replace the script for cb_event_test with the following code and run the window again. You get the expected results. The following code sends MS-Windows's close message and closes the parent window.
Send (Handle(Parent), 16, 0, 0)

The above code triggers MS-Window's Close event, which in turn triggers PowerBuilder window's CloseQuery. If CloseQuery event is executed successfully, PowerBuilder executes the Close event script and then it closes the window.

CloseQuery event is always fired before window's Close event is fired.

How to find out what message number (16 in the example) to send. First check what message is mapped to the Close event. It is pbm_close event id. MS-Windows message names are nothing but "pbm" replaced by "WM", i.e., WM_CLOSE in this case. We found the window message name, now how to find out message no?

All Windows messages are listed in Window.H file. If you have Visual C++, you will find it in "INCLUDE\WINUSER.H" file. The entry for WM_CLOSE looks like:
#define WM_CLOSE 0x0010

The number you see in the above definition is in hexadecimal. Converting this number from hexadecimal to decimal gives you 16. You can use MS-Window's calculator for conversion, in the scientific mode; type the above number in hexadecimal mode and choose decimal mode, you see the decimal equivalent of the typed number. Sometimes, you may want to see what events are occurring, in that case, you can use Visual C++'s Spy++ or Watcom's Wspy utilities.

This command executes synchronously, like TriggerEvent(). To do things asynchronously, use the Post() function which has format similar to Send().
HomePrevious Lesson: Cascaded Events and Functions
Next Lesson: Posting an Event