{"id":68993,"date":"2005-09-09T09:46:00","date_gmt":"2005-09-09T09:46:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/09\/09\/how-can-i-remove-an-item-from-a-list-box-in-an-hta\/"},"modified":"2005-09-09T09:46:00","modified_gmt":"2005-09-09T09:46:00","slug":"how-can-i-remove-an-item-from-a-list-box-in-an-hta","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-remove-an-item-from-a-list-box-in-an-hta\/","title":{"rendered":"How Can I Remove an Item from 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! In an HTA, how can I remove an item from a list box?<BR><BR>&#8212; LW<\/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, LW. You know, we Scripting Guys are forward-thinking guys and, as such, we never look back. Did we put an item in a list box? If so, there must have been a darn good reason for that and we\u2019d never even <I>consider<\/I> removing it. What\u2019s done is done, you know; we can\u2019t spend all our time reliving past mistakes. (Trust us: it would take a <I>long<\/I> time for the Scripting Guys to relive all their past mistakes.)<\/P>\n<P>Most people aren\u2019t like the Scripting Guys, however. (Hey, who said \u201cthank goodness\u201d?) On top of that, there are plenty of valid reasons for wanting to remove options from a list box. For example, suppose you have an HTA (an HTML Application) that lists computer names; that HTA might allow you to select a computer and then perform some operation against it. After finishing the task, you might want to remove that computer from the list box; that way there won\u2019t be any confusion about which computers you\u2019ve used and which ones you haven\u2019t. (This isn\u2019t necessarily the best way to handle such a task, but it\u2019ll do as a simple example.) <\/P>\n<P>So how do you remove an option from a list box? Well, here\u2019s an HTA that features a list box with 8 items. When you click the <B>Clear List Box<\/B> button, option 7 will be removed from the list box:<\/P><PRE class=\"codeSample\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Clear a List Box&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        If objOption.Value = &#8220;7&#8221; Then\n            objOption.RemoveNode\n            Exit Sub\n        End If\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;5&#8243;&gt;Option 5&lt;\/option&gt;\n&lt;option value=&#8221;6&#8243;&gt;Option 6&lt;\/option&gt;\n&lt;option value=&#8221;7&#8243;&gt;Option 7&lt;\/option&gt;\n&lt;option value=&#8221;8&#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 List Box&#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>\n<P>We won\u2019t spend much time talking about the code for creating an HTA; if you aren\u2019t familiar with HTAs and what they can do, you might take a look at <A href=\"http:\/\/null\/technet\/scriptcenter\/topics\/htas\/tutorial1.mspx\"><B>Part 1<\/B><\/A> in our HTA tutorial series. For today\u2019s column, we\u2019re going to concentrate on the subroutine named ClearListbox, the code that actually removes option 7 from the list box:<\/P><PRE class=\"codeSample\">Sub ClearListbox\n    For Each objOption in AvailableOptions.Options\n        If objOption.Value = &#8220;7&#8221; Then\n            objOption.RemoveNode\n            Exit Sub\n        End If\n    Next \nEnd Sub\n<\/PRE>\n<P>Admittedly, we\u2019re using a kind of brute-force method in order to grab &#8211; and remove &#8211; option 7, but that should be fine; unless you have tens of thousands of options in your list box your code will run plenty fast. When we say we\u2019re using a brute-force method we simply mean this: we\u2019re grabbing a list of all the items in the list box and then examining each one to see if the Value is equal to 7. This line of code retrieves a collection of all the items in the list box named AvailableOptions:<\/P><PRE class=\"codeSample\">For Each objOption in AvailableOptions.Options\n<\/PRE>\n<P>And <I>this<\/I> line of code checks each item to see if the Value is equal to 7:<\/P><PRE class=\"codeSample\">If objOption.Value = &#8220;7&#8221; Then\n<\/PRE>\n<P>And what if the value <I>is<\/I> equal to 7? In that case, we call the <B>RemoveNode<\/B> method to remove that option from the list box. And then, having done what we set out to do, we use this line of code to exit the subroutine:<\/P><PRE class=\"codeSample\">Exit Sub\n<\/PRE>\n<P>After all, having already found and removed the option in question, there\u2019s no sense in looping through the remainder of the collection.<\/P>\n<P>Keep in mind that this only works for removing items from a list box. We\u2019re experimenting with a modified version that will loop through your life and remove items (and mistakes) from there, but that might take awhile. We\u2019ll keep you posted.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! In an HTA, how can I remove an item from a list box?&#8212; LW Hey, LW. You know, we Scripting Guys are forward-thinking guys and, as such, we never look back. Did we put an item in a list box? If so, there must have been a darn good reason for that [&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-68993","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! In an HTA, how can I remove an item from a list box?&#8212; LW Hey, LW. You know, we Scripting Guys are forward-thinking guys and, as such, we never look back. Did we put an item in a list box? If so, there must have been a darn good reason for that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68993","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=68993"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68993\/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=68993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}