{"id":14745,"date":"2018-04-12T06:00:22","date_gmt":"2018-04-12T13:00:22","guid":{"rendered":"https:\/\/www.sapien.com\/blog\/?p=14745"},"modified":"2018-04-11T11:13:25","modified_gmt":"2018-04-11T18:13:25","slug":"powershell-studio-working-with-windows-control-event-handlers","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2018\/04\/12\/powershell-studio-working-with-windows-control-event-handlers\/","title":{"rendered":"PowerShell Studio: Working with Windows Form Control Event Handlers"},"content":{"rendered":"<p>PowerShell Studio has a selection of Windows Form templates that you can use to speed up your development. You will most likely be using your own code snippets to add behind the form controls, as well as the snippets available in PowerShell Studio, and perhaps snippets from someone else. This is fine until you start trying to copy\/paste an actual event code block.<\/p>\n<h3>Basic Button Control<\/h3>\n<p>There are many controls we can add to our Windows Form. The Button Control is the one you&#8217;ll click that will run your code behind the control and is the most used control in any Windows Forms application.<\/p>\n<p>The image below shows some other controls added to a form:<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_01_2018-04-06_14-50-52.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-14746\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_01_2018-04-06_14-50-52-215x300.jpg\" alt=\"\" width=\"300\" height=\"419\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_01_2018-04-06_14-50-52-215x300.jpg 215w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_01_2018-04-06_14-50-52.jpg 646w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>In the following example I am adding the &#8220;<em>Message Box (Yes \/ No)<\/em>&#8221; code from the PowerShell Studio snippet collection into the &#8220;<em>Button1<\/em>&#8221; control.\u00a0 Simply double-click on the snippet and the code will be automatically added to the control:<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_02_2018-04-06_14-50-52.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-14757\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_02_2018-04-06_14-50-52-300x72.jpg\" alt=\"\" width=\"717\" height=\"172\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_02_2018-04-06_14-50-52-300x72.jpg 300w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_02_2018-04-06_14-50-52-768x183.jpg 768w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_02_2018-04-06_14-50-52-1024x245.jpg 1024w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_02_2018-04-06_14-50-52-848x203.jpg 848w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_02_2018-04-06_14-50-52.jpg 1721w\" sizes=\"auto, (max-width: 717px) 100vw, 717px\" \/><\/a><\/p>\n<p>The result of adding the code inside the Button control is that when the form is executed, pressing &#8220;<em>Button1<\/em>&#8221; will execute the code behind the control. But, what&#8217;s really happening when you add any control to the form?<\/p>\n<h3>Control Event Handler Registration<\/h3>\n<p>Here&#8217;s where it gets interesting! Every time you drag\/drop to add a control\u2014the event handler for that control gets registered in the form. This is important and it&#8217;s done behind the scene. What will happened if you decide to delete the code block for the control?<\/p>\n<p>In our example we remove the code block for the &#8220;<em>Button1<\/em>&#8221; control:<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_03_2018-04-06_14-50-52.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-14748\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_03_2018-04-06_14-50-52-300x104.jpg\" alt=\"\" width=\"655\" height=\"227\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_03_2018-04-06_14-50-52-300x104.jpg 300w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_03_2018-04-06_14-50-52-768x266.jpg 768w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_03_2018-04-06_14-50-52-1024x354.jpg 1024w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_03_2018-04-06_14-50-52-848x293.jpg 848w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_03_2018-04-06_14-50-52.jpg 1220w\" sizes=\"auto, (max-width: 655px) 100vw, 655px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_04_2018-04-06_14-50-52.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-14749\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_04_2018-04-06_14-50-52-300x64.jpg\" alt=\"\" width=\"656\" height=\"140\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_04_2018-04-06_14-50-52-300x64.jpg 300w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_04_2018-04-06_14-50-52-768x164.jpg 768w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_04_2018-04-06_14-50-52-1024x218.jpg 1024w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_04_2018-04-06_14-50-52-848x181.jpg 848w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_04_2018-04-06_14-50-52.jpg 1225w\" sizes=\"auto, (max-width: 656px) 100vw, 656px\" \/><\/a><\/p>\n<p>Removing the code doesn&#8217;t remove the &#8220;<em>Button1<\/em>&#8221; object from the GUI form:<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_06_2018-04-06_14-50-52.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-14751\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_06_2018-04-06_14-50-52-300x229.jpg\" alt=\"\" width=\"300\" height=\"229\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_06_2018-04-06_14-50-52-300x229.jpg 300w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_06_2018-04-06_14-50-52.jpg 539w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>The following pop-up message will display when you manually remove the control code block:<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_07_2018-04-06_14-50-52.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-14752\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_07_2018-04-06_14-50-52-300x114.jpg\" alt=\"\" width=\"350\" height=\"133\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_07_2018-04-06_14-50-52-300x114.jpg 300w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_07_2018-04-06_14-50-52.jpg 449w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/a><\/p>\n<p>This indicates that removing the control code will <em>unregister<\/em> the Control Event Handler for that control in the form. And, once it is removed, copy\/pasting the code back won&#8217;t work:<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_08_2018-04-06_14-50-52.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-14753\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_08_2018-04-06_14-50-52-300x204.jpg\" alt=\"\" width=\"639\" height=\"434\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_08_2018-04-06_14-50-52-300x204.jpg 300w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_08_2018-04-06_14-50-52-768x521.jpg 768w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_08_2018-04-06_14-50-52-1024x695.jpg 1024w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_08_2018-04-06_14-50-52-848x575.jpg 848w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_08_2018-04-06_14-50-52.jpg 1279w\" sizes=\"auto, (max-width: 639px) 100vw, 639px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_09_2018-04-06_14-50-52.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-14754\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_09_2018-04-06_14-50-52-300x124.jpg\" alt=\"\" width=\"639\" height=\"264\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_09_2018-04-06_14-50-52-300x124.jpg 300w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_09_2018-04-06_14-50-52-768x317.jpg 768w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_09_2018-04-06_14-50-52-1024x423.jpg 1024w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_09_2018-04-06_14-50-52-848x350.jpg 848w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_09_2018-04-06_14-50-52.jpg 1199w\" sizes=\"auto, (max-width: 639px) 100vw, 639px\" \/><\/a><\/p>\n<p>The good news\u2014it&#8217;s easy to recover Control Event Handlers in PowerShell Studio!<\/p>\n<h3>Recovering Control Event Handlers<\/h3>\n<p>In order to register back the Control Event Handler:<\/p>\n<ol>\n<li>In the form Designer, select the control to re-register.<\/li>\n<li>In the Properties Panel, select the <em>Events<\/em> lightening-bolt button\u00a0<a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_10_2018-04-06_14-50-52.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14759\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_10_2018-04-06_14-50-52.jpg\" alt=\"\" width=\"18\" height=\"20\" \/>.<\/a><\/li>\n<li>In the <em>Action<\/em> section, select the <em>Click<\/em>\u00a0event, followed by the control that was added back: &#8220;<em>button1_Click<\/em>&#8220;.<\/li>\n<\/ol>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_11_2018-04-06_14-50-52.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-14760\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_11_2018-04-06_14-50-52-300x150.jpg\" alt=\"\" width=\"664\" height=\"332\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_11_2018-04-06_14-50-52-300x150.jpg 300w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_11_2018-04-06_14-50-52-768x384.jpg 768w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_11_2018-04-06_14-50-52-1024x512.jpg 1024w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_11_2018-04-06_14-50-52-848x424.jpg 848w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_11_2018-04-06_14-50-52.jpg 1219w\" sizes=\"auto, (max-width: 664px) 100vw, 664px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_12_2018-04-06_14-50-52.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14761\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/04\/WinEventHandler_12_2018-04-06_14-50-52.jpg\" alt=\"\" width=\"262\" height=\"181\" \/><\/a><\/p>\n<p>After this is done, the &#8220;<em>Button1<\/em>&#8221; control will start working when executing the form.<\/p>\n<h3>Summary<\/h3>\n<p>It&#8217;s easy to remove and add code back in a Windows Form. Just remember that when it involves Control Event Handlers, you will need to take some extra steps to recover the event. I hope this information will be helpful when working with new and existing forms.<\/p>\n<h3>Feedback<\/h3>\n<p>As always, if you have any ideas, comments, or feedback, please <a href=\"https:\/\/www.sapien.com\/forums\/viewforum.php?f=23\">visit our feedback forum<\/a> and reference this post.<\/p>\n<p>&nbsp;<\/p>\n<p><em>Max Trinidad is a Technology Evangelist at SAPIEN Technologies Inc., and a Microsoft PowerShell MVP. You can reach him at\u00a0<a href=\"mailto:maxt@sapien.com\">maxt@sapien.com<\/a><\/em><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PowerShell Studio has a selection of Windows Form templates that you can use to speed up your development. You will most likely be using your own code snippets to add behind the form controls, as well as the snippets available in PowerShell Studio, and perhaps snippets from someone else. This is fine until you start [&hellip;]<\/p>\n","protected":false},"author":47,"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":[2,946,1143,1238,703,1239],"tags":[641,1052,1016],"class_list":["post-14745","post","type-post","status-publish","format-standard","hentry","category-general","category-guiprogramming","category-powershell","category-powershell-core","category-powershell-studio","category-pscore6","tag-controls","tag-event-handler","tag-powershell-studio"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/14745","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\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=14745"}],"version-history":[{"count":13,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/14745\/revisions"}],"predecessor-version":[{"id":14792,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/14745\/revisions\/14792"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=14745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=14745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=14745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}