{"id":2311,"date":"2021-05-06T12:50:35","date_gmt":"2021-05-06T16:50:35","guid":{"rendered":"https:\/\/anvil-of-time.com\/?p=2311"},"modified":"2021-05-06T12:50:35","modified_gmt":"2021-05-06T16:50:35","slug":"100-days-of-powerbuilder-tutorial-day-9-window-methods","status":"publish","type":"post","link":"https:\/\/anvil-of-time.com\/powerbuilder\/100-days-of-powerbuilder-tutorial-day-9-window-methods\/","title":{"rendered":"100 Days of PowerBuilder &#8211; Tutorial &#8211; Day 9 &#8211; Window Methods"},"content":{"rendered":"<p>This is part of my project &#8216;100 Days of PowerBuilder&#8217; which is a series of discussions focused on basic PowerBuilder development.<\/p>\r\n<!-- \/wp:paragraph -->\r\n\r\n<!-- wp:paragraph -->\r\n<p><em>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.<\/em><\/p>\r\n<!-- \/wp:paragraph -->\r\n\r\n<!-- wp:paragraph -->\r\n<p>Before we go much further we need to think about common functionality within our application.\u00a0 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 &#8216;down the road&#8217;.\u00a0 So lets think about some common methods\/processes for windows.<\/p>\r\n<p>Previously we created the &#8216;we_postopen&#8217; event which is posted by the window open event\u00a0 To enhance our common window process flow we are expanding on this.<\/p>\r\n<p><a href=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2021\/05\/D9tut-001.png\"><img loading=\"lazy\" class=\"alignleft size-full wp-image-2315\" src=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2021\/05\/D9tut-001.png\" alt=\"\" width=\"813\" height=\"288\" srcset=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2021\/05\/D9tut-001.png 813w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2021\/05\/D9tut-001-300x106.png 300w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2021\/05\/D9tut-001-768x272.png 768w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2021\/05\/D9tut-001-150x53.png 150w\" sizes=\"(max-width: 813px) 100vw, 813px\" \/><\/a><\/p>\r\n<p>So now to modify the existing window.\u00a0 Add the we_preopen, we_close, we_validate, we_savechange, we_save, we_cancelchanges, and we_cancel events to w_main.<\/p>\r\n<p>Change the code in the Open event from this<\/p>\r\n<pre><code>THIS.PostEvent(\"we_postopen\")<\/code><\/pre>\r\n<p>to this<\/p>\r\n<pre><code>THIS.EVENT TRIGGER we_preopen()\r\n\r\nTHIS.EVENT POST we_postopen()<\/code><\/pre>\r\n<p>I prefer the second syntax for triggering\/posting events.<\/p>\r\n<p>Now in the we_close event add the following<\/p>\r\n<pre><code>THIS.EVENT TRIGGER we_validate()\r\n\r\nTHIS.EVENT TRIGGER we_savechange()\r\n\r\nTHIS.EVENT TRIGGER we_cancel()\r\n\r\nClose(THIS)<\/code><\/pre>\r\n<p>Hmm, something needs changing here.\u00a0 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.\u00a0 So open the we_validate event and change the return value from (none) to &#8220;integer&#8221;<\/p>\r\n<p><a href=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2021\/05\/d9Tut-002.png\"><img loading=\"lazy\" class=\"size-full wp-image-2323 aligncenter\" src=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2021\/05\/d9Tut-002.png\" alt=\"\" width=\"691\" height=\"303\" srcset=\"https:\/\/anvil-of-time.com\/wp-content\/uploads\/2021\/05\/d9Tut-002.png 691w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2021\/05\/d9Tut-002-300x132.png 300w, https:\/\/anvil-of-time.com\/wp-content\/uploads\/2021\/05\/d9Tut-002-150x66.png 150w\" sizes=\"(max-width: 691px) 100vw, 691px\" \/><\/a>and add the following.<\/p>\r\n<pre><code>\/\/validate\r\n\/\/ do some sort of checking to see if values have changed and prompt the user\r\ninteger li_rc\r\nli_rc = MessageBox('Save before closing','Save changes?',Question!, YesNoCancel!)\r\nRETURN li_rc<\/code><\/pre>\r\n<p>Now go to the we_close event script and change it to:<\/p>\r\n<pre><code>\/\/ close \r\ninteger li_rc\r\nli_rc = THIS.EVENT TRIGGER we_validate()\r\nCHOOSE CASE li_rc\r\n\tCASE 1 \/\/yes\r\n\t\tTHIS.EVENT TRIGGER we_savechange()\r\n\tCASE 2 \/\/ no \r\n                \/\/ reset things if needed\r\n\t\tTHIS.EVENT TRIGGER we_cancelchanges()\r\n\tCASE 3 \/\/ cancel\r\n\t\tRETURN \/\/prevents window from closing\r\nEND CHOOSE\r\n\r\nClose(THIS)<\/code><\/pre>\r\n<p>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.<\/p>\r\n<p>Next remove the code from our earlier exercise from the Closequery event.\u00a0 Now for demonstration purposes put MessageBox code in both the we_savechange and we_cancelchange events.\u00a0 Something like this:<\/p>\r\n<pre><code>Messagebox('Cancel Change','we_cancelchanges')  \/\/change wording for the we_savechange event<\/code><\/pre>\r\n<p>Now go to the &#8216;Exit&#8217; button on the window and change the code to<\/p>\r\n<pre><code>PARENT.EVENT TRIGGER we_close()<\/code><\/pre>\r\n<p>Now when you run the program and click on the &#8216;Exit&#8217; 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.<\/p>\r\n<p>However, now we have to account for the user closing the window via the titlebar or &#8216;Alt-F4&#8217;. Go back to the Closequery event and insert following code:<\/p>\r\n<pre><code>\/\/ closequery\r\ninteger li_rc\r\nli_rc = THIS.EVENT TRIGGER we_validate()\r\nCHOOSE CASE li_rc\r\n\tCASE 1 \/\/yes\r\n\t\tTHIS.EVENT TRIGGER we_savechange()\r\n\tCASE 2 \/\/ no\r\n\t\tTHIS.EVENT TRIGGER we_cancelchanges()\r\n\tCASE 3 \/\/ cancel\r\n\t\tRETURN 1 \/\/prevents window from closing\r\nEND CHOOSE\r\nRETURN 0<\/code><\/pre>\r\n<p>Notice this code isn&#8217;t much different from we_close.<\/p>\r\n<p>Now change the code in we_save to the following;<\/p>\r\n<pre><code>\/\/ save the window then exit. \r\n\/\/ \r\n\/\/insert save checking logic \r\n\r\nTHIS.EVENT TRIGGER we_savechange()\r\nCLOSE(THIS)<\/code><\/pre>\r\n<p>So you may wonder why don&#8217;t I use functions instead of events for some of this and my answer is that you could.\u00a0 However, in the next session I will be going over some basic window inheritance and the flexibility of events will be demonstrated.<\/p>","protected":false},"excerpt":{"rendered":"<p>This is part of my project &#8216;100 Days of PowerBuilder&#8217; 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&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[86,3],"tags":[62],"_links":{"self":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/2311"}],"collection":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/comments?post=2311"}],"version-history":[{"count":22,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/2311\/revisions"}],"predecessor-version":[{"id":2335,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/2311\/revisions\/2335"}],"wp:attachment":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/media?parent=2311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/categories?post=2311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/tags?post=2311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}