{"id":70143,"date":"2005-03-29T20:02:00","date_gmt":"2005-03-29T20:02:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/03\/29\/how-can-i-clear-a-list-box-in-an-hta\/"},"modified":"2005-03-29T20:02:00","modified_gmt":"2005-03-29T20:02:00","slug":"how-can-i-clear-a-list-box-in-an-hta","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-clear-a-list-box-in-an-hta\/","title":{"rendered":"How Can I Clear a List Box in an HTA?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I clear a list box in an HTA?<BR><BR>&#8212; AK<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, AK. For those of you who aren\u2019t familiar with the term, an HTA &#8211; among other things &#8211; is a way to use Internet Explorer to provide a graphical user interface for your scripts. (If that <I>still<\/I> doesn\u2019t answer your questions, don\u2019t despair. Instead, stay tuned for April 11<SUP>th<\/SUP> when we debut our new HTA Developers\u2019 Center.) With an HTA (short for <I>HTML Application<\/I>) you can make your system administration scripts look &#8211; and run &#8211; like real applications, applications complete with radio buttons, text boxes, check boxes, and, yes, list boxes.<\/P>\n<P>So suppose you <I>do<\/I> have a list box, one with a bunch of items (technically known as <I>options<\/I>) already in it. In other words, something that looks like this:<\/P><IMG border=\"0\" alt=\"Listbox\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/listbox1.jpg\" width=\"335\" height=\"196\"> \n<P><BR>Suppose that you want to remove all of those options, leaving you with a nice, clean, empty list box. How can you do that? <\/P>\n<P>To begin with, as far we know there\u2019s no single command that can clear all the options in a list box. But that\u2019s OK; even if we can\u2019t clear all the options at once, we <I>can<\/I> clear the options one at a time using the <B>RemoveNode<\/B> method. All we have to do is iterate through the all the options in the list box and then delete each one. We can do that by adding a subroutine (in this case, a subroutine named <B>ClearListbox<\/B>) to our HTA:<\/P><PRE class=\"codeSample\">Sub ClearListbox\n    For Each objOption in AvailableOptions.Options\n        objOption.RemoveNode\n    Next \nEnd Sub\n<\/PRE>\n<P><B>Note<\/B>. This subroutine assumes that the list box we want to clear is named <B>AvailableOptions<\/B>. <\/P>\n<P>As you can see, we create a For Each loop that loops through all the <B>Options<\/B> in the list box named AvailableOptions. For each option we find (and, remember, the options correspond to the items in the list box), we call the <B>RemoveNode<\/B> method to remove that option. By the time the For Each loop is done, we will have found &#8211; and removed &#8211; every item in the list box.<\/P>\n<P>The only other thing we need to do is add a button to our HTA; this button, when clicked, will call the ClearListbox subroutine. Here\u2019s some HTML that will add a button like this to our HTA:<\/P><PRE class=\"codeSample\">&lt;input id=runbutton  class=&#8221;button&#8221; type=&#8221;button&#8221; value=&#8221;Clear Listbox&#8221; \nname=&#8221;run_button&#8221;  onClick=&#8221;ClearListbox&#8221;&gt;&lt;p&gt;\n<\/PRE>\n<P>That\u2019s all we have to do. If we run our revised HTA and click the <B>Clear Listbox<\/B> button, all the options in the list box will be removed, and our HTA will look like this:<\/P><IMG border=\"0\" alt=\"Listbox\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/listbox2.jpg\" width=\"335\" height=\"196\"> \n<P><BR>Breath-taking, isn\u2019t it?<\/P>\n<P>If you\u2019d like to try this out for yourself, here\u2019s code that will bring up an HTA with a list box pre-populated with 8 items. Click <B>Clear Listbox<\/B> and all 8 items will be deleted. To use this sample code, copy it, paste it into Notepad, and then save it with a <B>.hta<\/B> file extension (for example, <I>listbox.hta<\/I>). Then just double-click the file in My Computer or Windows Explorer, and see what happens.<\/P>\n<P>Here\u2019s the HTA code:<\/P><PRE class=\"codeSample\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Clear a Listbox&lt;\/title&gt;\n&lt;\/head&gt;<\/p>\n<p>&lt;SCRIPT Language=&#8221;VBScript&#8221;&gt;<\/p>\n<p>Sub ClearListbox\n    For Each objOption in AvailableOptions.Options\n        objOption.RemoveNode\n    Next \nEnd Sub<\/p>\n<p>&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body bgcolor=&#8221;buttonface&#8221;&gt;<\/p>\n<p>&lt;select size=&#8221;8&#8243; name=&#8221;AvailableOptions&#8221; style=&#8221;width:400&#8243; &gt;\n&lt;option value=&#8221;1&#8243;&gt;Option 1&lt;\/option&gt;\n&lt;option value=&#8221;2&#8243;&gt;Option 2&lt;\/option&gt;\n&lt;option value=&#8221;3&#8243;&gt;Option 3&lt;\/option&gt;\n&lt;option value=&#8221;4&#8243;&gt;Option 4&lt;\/option&gt;\n&lt;option value=&#8221;1&#8243;&gt;Option 5&lt;\/option&gt;\n&lt;option value=&#8221;2&#8243;&gt;Option 6&lt;\/option&gt;\n&lt;option value=&#8221;3&#8243;&gt;Option 7&lt;\/option&gt;\n&lt;option value=&#8221;4&#8243;&gt;Option 8&lt;\/option&gt;\n&lt;\/select&gt;<\/p>\n<p>&lt;p&gt;\n&lt;input id=runbutton  class=&#8221;button&#8221; type=&#8221;button&#8221; value=&#8221;Clear Listbox&#8221; \nname=&#8221;run_button&#8221;  onClick=&#8221;ClearListbox&#8221;&gt;&lt;p&gt;<\/p>\n<p>&lt;\/body&gt;\n&lt;\/html&gt;\n<\/PRE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I clear a list box in an HTA?&#8212; AK Hey, AK. For those of you who aren\u2019t familiar with the term, an HTA &#8211; among other things &#8211; is a way to use Internet Explorer to provide a graphical user interface for your scripts. (If that still doesn\u2019t answer your [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,5,30],"class_list":["post-70143","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I clear a list box in an HTA?&#8212; AK Hey, AK. For those of you who aren\u2019t familiar with the term, an HTA &#8211; among other things &#8211; is a way to use Internet Explorer to provide a graphical user interface for your scripts. (If that still doesn\u2019t answer your [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70143"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70143\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}