{"id":2013,"date":"2010-01-06T10:23:52","date_gmt":"2010-01-06T18:23:52","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2010\/01\/06\/primalforms-2009-onapplicationload-function\/"},"modified":"2010-01-06T10:23:52","modified_gmt":"2010-01-06T18:23:52","slug":"primalforms-2009-onapplicationload-function","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2010\/01\/06\/primalforms-2009-onapplicationload-function\/","title":{"rendered":"PrimalForms 2009: OnApplicationLoad Function"},"content":{"rendered":"<p>Recently on the forums we had a user who wanted to know why he couldn\u2019t initialize the form controls within the <strong>OnApplicationLoad<\/strong> function. Seeing how this can result in some confusion, we would like to elaborate on the subject.<\/p>\n<p>The purpose of <strong>OnApplicationLoad<\/strong> is to initialize any dependencies necessary for your form script to run, such as PSSnapins. In the case where the necessary dependencies are unavailable, you can return <em>$false<\/em> and the form will not display.<\/p>\n<p>To understand why you can\u2019t access the form controls within the <strong>OnApplicationLoad<\/strong> function, you need to understand when and where the <strong>OnApplicationLoad<\/strong> is called. <\/p>\n<p>The following diagram demonstrates the process flow of the generated script:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" height=\"189\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/12\/image3.png\" width=\"240\" border=\"0\" \/> <\/p>\n<\/p>\n<p>As you can see the <strong>OnApplicationLoad<\/strong> is called before the <strong>GenerateForm<\/strong> function is called. If the <strong>OnApplicationLoad<\/strong> returns <em>$true<\/em>, the script will call the <strong>GenerateForm<\/strong> function, which contains the form code including your custom event scripts; therefore the controls are only exist in the scope of this function. Once the form is closed, the <strong>OnApplicationExit<\/strong> function is called. The <strong>OnApplicationExit<\/strong> function can be used to remove any dependencies you may have included. Like the <strong>OnApplicationLoad<\/strong>, the form controls are out of scope when the <strong>OnApplicationExit<\/strong> is called.<\/p>\n<p>Since the form controls are not accessible by the <strong>OnApplicationLoad<\/strong> function where can you dynamically initialize the controls? The answer lies in the Form\u2019s Load event. The Load event will fire the right before the form is displayed, but after the controls are defined, making the event an ideal location for initialization.<\/p>\n<p>To add a Load event handler simply double click on the Form in the Designer or use the Properties Panel\u2019s Events Tab and type in the event hander name next to the Load entry.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"Properties Load Event\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" height=\"222\" alt=\"Properties Load Event\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/12\/PropertiesLoadEvent.png\" width=\"323\" border=\"0\" \/> <\/p>\n<p>Now that you have a Load Event Handler you can use Read-Host or other methods to initialize your controls.<\/p>\n<p>&#160;<a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/12\/LoadEventScriptBlock1.png\"><img loading=\"lazy\" decoding=\"async\" title=\"Load Event Script Block\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" height=\"69\" alt=\"Load Event Script Block\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/12\/LoadEventScriptBlock_thumb.png\" width=\"244\" border=\"0\" \/><\/a><\/p>\n<p>PrimalForms 2009 includes various sample forms and one of particular interest to this subject is SystemInformation.pff. If you refer to the SystemInformation.pff sample, you will notice that in the <strong>OnApplicationLoad<\/strong> is used to prompt the user for the path to a text file that contains a list of computer names. The path is stored in a script scope variable: <em>$script:list<\/em>. If the file is not found, the <strong>OnApplicationLoad<\/strong> returns <em>$false<\/em> and the form is never displayed.<\/p>\n<p>&#160;<a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/12\/SystemInformationSampleOnApplicationLoad.png\"><img loading=\"lazy\" decoding=\"async\" title=\"SystemInformationSampleOnApplicationLoad\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" height=\"111\" alt=\"SystemInformationSampleOnApplicationLoad\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/12\/SystemInformationSampleOnApplicationLoad_thumb.png\" width=\"244\" border=\"0\" \/><\/a> <\/p>\n<p>In the form\u2019s Load event ($PopulateList), the script calls uses the <em>$list<\/em> variable (initialized in <strong>OnApplicationLoad<\/strong>) to load a list of computer names into a ComboBox control. Since the <em>$list<\/em> variable was declared in the script scope, the Load event is able to access it.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/12\/LoadEventPopulateList1.png\"><img loading=\"lazy\" decoding=\"async\" title=\"Load Event Populate List\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" height=\"81\" alt=\"Load Event Populate List\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/12\/LoadEventPopulateList_thumb.png\" width=\"244\" border=\"0\" \/><\/a>&#160;<\/p>\n<p>To recap, the <strong>OnApplicationLoad<\/strong> function is used to pre-initialize your script, but it <em>can not<\/em> access the form controls. Instead it is recommended to use the Load event of the form to directly initialize the controls. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently on the forums we had a user who wanted to know why he couldn\u2019t initialize the form controls within the OnApplicationLoad function. Seeing how this can result in some confusion, we would like to elaborate on the subject. The purpose of OnApplicationLoad is to initialize any dependencies necessary for your form script to run, [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[283,509,25],"tags":[198,1000,28,426,312],"class_list":["post-2013","post","type-post","status-publish","format-standard","hentry","category-howto","category-primalforms-software-news","category-windows-powershell","tag-function","tag-howto","tag-powershell","tag-primalform","tag-winforms"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/2013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=2013"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/2013\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=2013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=2013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=2013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}