{"id":3170,"date":"2011-07-05T13:16:00","date_gmt":"2011-07-05T21:16:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=3170"},"modified":"2011-07-05T13:37:07","modified_gmt":"2011-07-05T21:37:07","slug":"primalforms-preventing-multiple-button-clicks","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2011\/07\/05\/primalforms-preventing-multiple-button-clicks\/","title":{"rendered":"PrimalForms: Preventing Multiple Button Clicks"},"content":{"rendered":"<p>A problem you may face when creating Graphical User Interfaces (GUI) is preventing users from clicking a button multiple times.&nbsp; <\/p>\n<p>It is helpful for our users to learn how to handle this situation. In the following sample, we have a button that is disabled immediately when the button is pressed and then enabled at&nbsp; the end of click event script. <\/p>\n<pre><span style=\"color: #8b0000\">$buttonPress_Click<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">{\n    <\/span><span style=\"color: #8b0000\">$this<\/span><span style=\"color: #000000\">.Enabled <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$False<\/span><span style=\"color: #000000\">\n    \n    <\/span><span style=\"color: #008000\"># Output a message on click<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #8b0000\">$textboxOutput<\/span><span style=\"color: #000000\">.AppendText(<\/span><span style=\"color: #ff0000\">\"Button was clicked`r`n\"<\/span><span style=\"color: #000000\">)\n  <\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #008000\"># Simulate work<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #0000ff; font-weight: bold\">Start-Sleep<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-<\/span><span style=\"color: #000000\">Seconds 2<\/span><span style=\"color: #000000\">\n\n    <\/span><span style=\"color: #8b0000\">$this<\/span><span style=\"color: #000000\">.Enabled <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$True<\/span><span style=\"color: #000000\">\n}<\/span><\/pre>\n<p>You will find that when you click on the button multiple times while a long event script is processing, the button\u2019s click event will still fire multiple times despite the fact that the button was disabled at the start of the click event.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"ButtonMultipleClicks\" border=\"0\" alt=\"ButtonMultipleClicks\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/07\/ButtonMultipleClicks.png\" width=\"304\" height=\"304\"><\/p>\n<p>The multiple clicks happen because the form is unable to process any new event messages until the current click event is completed. This results in the application queuing the event messages in memory until the form is able process them.&nbsp; The cause of our dilemma is the queuing of event messages. So how do we resolve this problem?<\/p>\n<p>One solution is to use Jobs, but we will save this subject for a future blog article. Now we will look at how to handle this situation without resorting to Jobs.<\/p>\n<p>It turns out you can resolve this problem by adding one line of code:<\/p>\n<p>[System.Windows.Forms.Application]::DoEvents()<\/p>\n<p>Essentially this method tells the application to process all its pending event messages. So when this line is executed, before the button is enabled, it will disregard any click messages sent to the disabled button; thus preventing the user from clicking the button multiple times. Afterwards we enable the button and the user will be able to click the button once more.<\/p>\n<pre><span style=\"color: #8b0000\">$buttonPress_Click<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">{\n    <\/span><span style=\"color: #8b0000\">$this<\/span><span style=\"color: #000000\">.Enabled <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$False<\/span><span style=\"color: #000000\">\n    \n    <\/span><span style=\"color: #008000\"># Output a message on click<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #8b0000\">$textboxOutput<\/span><span style=\"color: #000000\">.AppendText(<\/span><span style=\"color: #ff0000\">\"Button was clicked`r`n\"<\/span><span style=\"color: #000000\">)\n<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #008000\"># Simulate work<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #0000ff; font-weight: bold\">Start-Sleep<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-<\/span><span style=\"color: #000000\">Seconds 2<\/span><span style=\"color: #000000\">\n    \n    <\/span><span style=\"color: #008000\"># Process the pending messages before enabling the button<\/span><span style=\"color: #000000\">\n    [System.Windows.Forms.Application]<\/span><span style=\"color: #0000ff\">::<\/span><span style=\"color: #000000\">DoEvents()\n    \n    <\/span><span style=\"color: #8b0000\">$this<\/span><span style=\"color: #000000\">.Enabled <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$True<\/span><span style=\"color: #000000\">\n}<\/span><\/pre>\n<p>I would like to thank Joel Bennett for bringing this issue to my attention.<\/p>\n<p>You can download the sample <a href=\"http:\/\/www.sapien.com\/downloads#Sample Scripts\/PrimalForms 2011\/PreventMultipleButtonClicks.zip\">Prevent Multiple Button Clicks Form<\/a> from the <a href=\"http:\/\/sapien.com\/downloads\">Downloads<\/a> section.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A problem you may face when creating Graphical User Interfaces (GUI) is preventing users from clicking a button multiple times.&nbsp; It is helpful for our users to learn how to handle this situation. In the following sample, we have a button that is disabled immediately when the button is pressed and then enabled at&nbsp; the [&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,575,25],"tags":[652,28,314,633],"class_list":["post-3170","post","type-post","status-publish","format-standard","hentry","category-howto","category-primalforms-software-news","category-primalforms-2011-software-news","category-windows-powershell","tag-button","tag-powershell","tag-primalforms","tag-primalforms-2011"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3170","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=3170"}],"version-history":[{"count":2,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3170\/revisions"}],"predecessor-version":[{"id":3174,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3170\/revisions\/3174"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=3170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=3170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=3170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}