{"id":7541,"date":"2014-08-05T09:00:14","date_gmt":"2014-08-05T17:00:14","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=7541"},"modified":"2014-08-05T10:56:40","modified_gmt":"2014-08-05T18:56:40","slug":"powershell-studio-initializing-my-gui-controls","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2014\/08\/05\/powershell-studio-initializing-my-gui-controls\/","title":{"rendered":"PowerShell Studio: Initializing my GUI controls"},"content":{"rendered":"<p>If you are working with a GUI script within PowerShell Studio and find that you need to edit the exported script to modify the control\u2019s initialization, then you are doing something wrong.<\/p>\n<p>Don\u2019t get me wrong, modifying and initialization of control properties is not the issue; in fact that it part of writing a GUI. What I\u2019m referring to is the part where you are editing the exported scripts.<\/p>\n<p>If you are editing the exported script:<\/p>\n<p>1. You are creating an extra step which means you need to export and modify the script <span style=\"text-decoration: underline\">anytime<\/span> you make a change to the original file.<\/p>\n<p>2. It is time consuming and unproductive.<\/p>\n<p>3. If you are working with Source Control or within a team environment, chances are the change will be missed or forgotten by somebody.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>So if I\u2019m not supposed to edit the exported script, then how do I initialize my controls?<\/strong><\/p>\n<p>Thankfully WinForms provides a convenient place to initialize your GUI controls, the Form control\u2019s <em>Load<\/em> event.<\/p>\n<p>The Form control\u2019s <em>Load<\/em> event is called right before the form is displayed, which makes it a perfect place to initialize your controls.<\/p>\n<pre><span style=\"color: #8b0000\">$formServices_Load<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">{\n    <\/span><span style=\"color: #008000\">#TODO: Initialize Form Controls here\n<\/span><span style=\"color: #000000\">    <\/span><span style=\"color: #008000\">#Load the services into the the combobox<br>    <\/span><span style=\"color: #008080\">Load-ComboBox <\/span><span style=\"color: #3399ff\">-ComboBox <\/span><span style=\"color: #8b0000\">$comboboxServices <\/span><span style=\"color: #3399ff\">-Items<\/span><span style=\"color: #000000\"> (<\/span><span style=\"font-weight: bold; color: #0000ff\">Get-Service<\/span><span style=\"color: #000000\">) <\/span><span style=\"color: #3399ff\">-DisplayMember<\/span><span style=\"color: #000000\"> Name\n\n    <\/span><span style=\"color: #008000\">#Display the computer name in the form's title<br>    <\/span><span style=\"color: #8b0000\">$formServices<\/span><span style=\"color: #000000\">.Text <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #ff0000\">\"Services on $env:ComputerName\"<\/span><span style=\"color: #000000\">\n}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000\">Warning: Do not try to initialize a control outside an event block within the script.<\/span><\/p>\n<p>1. The control might not exist when this line is executed by PowerShell.<\/p>\n<p>2. If the control does exist, your changes will most likely get overwritten by the designer generated script.<\/p>\n<p><strong>Alternatives to the Load event:<\/strong><\/p>\n<p>It is possible to use other events to trigger initialization. For example, some control sets such as the <em>Chart \u2013 Disk Space<\/em> use the <em>VisibleChanged<\/em> event to trigger initialization. This event is triggered when the control is displayed (such as when the form is loaded) or hidden using the <em>Visible<\/em> property.<\/p>\n<pre><span style=\"color: #8b0000\">$chartDiskSpace_VisibleChanged<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">{\n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$this<\/span><span style=\"color: #000000\">.Visible)\n    {\n        <\/span><span style=\"color: #008080\">Load-DiskChart<\/span><span style=\"color: #8b0000\">$this<\/span><span style=\"color: #000000\">\n    }\n}<\/span><\/pre>\n<p>In the sample above, the control set initializes when the control becomes visible by checking its <em>Visible<\/em> property.<\/p>\n<p>&nbsp;<\/p>\n<p>If your initialization script is running slow it can prevent the GUI from displaying in a timely manner or cause it to hang. In instances such as this, you might want to delay your initialization or simply run the query in a separate job then initialize and enable the controls after the job has completed the query.<\/p>\n<p><span style=\"color: #ff0000\">Warning: You cannot access GUI controls directly from a Job. You must return the results first and then initialize the controls on the main thread \/ script.<\/span><\/p>\n<p>For more details on how to use Jobs within a GUI script, please refer to the <a href=\"http:\/\/www.sapien.com\/blog\/2012\/05\/16\/powershell-studio-creating-responsive-forms\/\">Creating Responsive Forms<\/a> article.<\/p>\n<p>For more details on Form control, please refer to the <a href=\"http:\/\/www.sapien.com\/blog\/2011\/06\/01\/primalforms-2011-spotlight-on-the-form-control\/\">Spotlight on the Form Control<\/a> article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are working with a GUI script within PowerShell Studio and find that you need to edit the exported script to modify the control\u2019s initialization, then you are doing something wrong. Don\u2019t get me wrong, modifying and initialization of control properties is not the issue; in fact that it part of writing a GUI. [&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":[703,644,25],"tags":[641,874,312],"class_list":["post-7541","post","type-post","status-publish","format-standard","hentry","category-powershell-studio","category-spotlight-on-controls","category-windows-powershell","tag-controls","tag-powershell-studio-2014","tag-winforms"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/7541","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=7541"}],"version-history":[{"count":6,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/7541\/revisions"}],"predecessor-version":[{"id":7593,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/7541\/revisions\/7593"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=7541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=7541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=7541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}