100 Days of PowerBuilder – Tutorial – Day 9 – Window Methods
This is part of my project ‘100 Days of PowerBuilder’ which is a series of discussions focused on basic PowerBuilder development.
Note: This article is written with examples created in PowerBuilder 2019 R3. Most steps/examples will be identical with any version going back to 9.
Before we go much further we need to think about common functionality within our application. One advantage of Object Oriented Programming is the ability to reuse code which not only makes development faster but also aids in understanding and maintaining the application ‘down the road’. So lets think about some common methods/processes for windows.
Previously we created the ‘we_postopen’ event which is posted by the window open event To enhance our common window process flow we are expanding on this.
So now to modify the existing window. Add the we_preopen, we_close, we_validate, we_savechange, we_save, we_cancelchanges, and we_cancel events to w_main.
Change the code in the Open event from this
THIS.PostEvent("we_postopen")
to this
THIS.EVENT TRIGGER we_preopen()
THIS.EVENT POST we_postopen()
I prefer the second syntax for triggering/posting events.
Now in the we_close event add the following
THIS.EVENT TRIGGER we_validate()
THIS.EVENT TRIGGER we_savechange()
THIS.EVENT TRIGGER we_cancel()
Close(THIS)
Hmm, something needs changing here. We need to change the we_validate event to return a status so we can proceed to either the we_savechange or we_cancel methods. So open the we_validate event and change the return value from (none) to “integer”
//validate
// do some sort of checking to see if values have changed and prompt the user
integer li_rc
li_rc = MessageBox('Save before closing','Save changes?',Question!, YesNoCancel!)
RETURN li_rc
Now go to the we_close event script and change it to:
// close
integer li_rc
li_rc = THIS.EVENT TRIGGER we_validate()
CHOOSE CASE li_rc
CASE 1 //yes
THIS.EVENT TRIGGER we_savechange()
CASE 2 // no
// reset things if needed
THIS.EVENT TRIGGER we_cancelchanges()
CASE 3 // cancel
RETURN //prevents window from closing
END CHOOSE
Close(THIS)
So now we first trigger the we_validate event and return the Messagebox choice value. Based on that value we either save the changes, discard any changes, or cancel the action altogether.
Next remove the code from our earlier exercise from the Closequery event. Now for demonstration purposes put MessageBox code in both the we_savechange and we_cancelchange events. Something like this:
Messagebox('Cancel Change','we_cancelchanges') //change wording for the we_savechange event
Now go to the ‘Exit’ button on the window and change the code to
PARENT.EVENT TRIGGER we_close()
Now when you run the program and click on the ‘Exit’ button you are prompted to save the changes. Run three times and choose each of the options (Yes, No, Cancel). You will see the two messageboxes for either Yes or No and the Messagebox close but the main window remain open for Cancel.
However, now we have to account for the user closing the window via the titlebar or ‘Alt-F4’. Go back to the Closequery event and insert following code:
// closequery
integer li_rc
li_rc = THIS.EVENT TRIGGER we_validate()
CHOOSE CASE li_rc
CASE 1 //yes
THIS.EVENT TRIGGER we_savechange()
CASE 2 // no
THIS.EVENT TRIGGER we_cancelchanges()
CASE 3 // cancel
RETURN 1 //prevents window from closing
END CHOOSE
RETURN 0
Notice this code isn’t much different from we_close.
Now change the code in we_save to the following;
// save the window then exit.
//
//insert save checking logic
THIS.EVENT TRIGGER we_savechange()
CLOSE(THIS)
So you may wonder why don’t I use functions instead of events for some of this and my answer is that you could. However, in the next session I will be going over some basic window inheritance and the flexibility of events will be demonstrated.
Thank You Sir, hpe read next posting soon,
Herman ,Bekasi, Ind.