{"id":69173,"date":"2005-08-15T20:33:00","date_gmt":"2005-08-15T20:33:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/08\/15\/how-can-i-dynamically-populate-a-list-box-in-an-hta\/"},"modified":"2005-08-15T20:33:00","modified_gmt":"2005-08-15T20:33:00","slug":"how-can-i-dynamically-populate-a-list-box-in-an-hta","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-dynamically-populate-a-list-box-in-an-hta\/","title":{"rendered":"How Can I Dynamically Populate 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 my HTA, I would like to query a computer for a list of local user accounts, then dynamically create a list box containing those accounts. How can I do that?<BR><BR>&#8212; BN<\/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, BN. You know, if you\u2019d ever seen the Scripting Guys in \u2026 action \u2026 you\u2019d think twice about asking us a question about doing things dynamically. But as long as you stick to things like dynamic list boxes (and forego things like dynamic personalities) we might be able to help.<\/P>\n<P>Let\u2019s start off by showing you the complete code for an HTA that retrieves all the local user accounts and then displays those accounts in a list box, which is populated on-the-fly. To make use of this code, copy it from your browser, paste it into Notepad or another text editor, and then save it with a <B>.hta<\/B> file extension. And if you\u2019re already lost because you have no idea what an HTA is (hint: it\u2019s short for HTML Application), then you might want to check out our <A href=\"http:\/\/null\/technet\/scriptcenter\/hubs\/htas.mspx\"><B>HTA Developers Center<\/B><\/A>, and maybe even peruse our <A href=\"http:\/\/null\/technet\/scriptcenter\/topics\/htas\/tutorial1.mspx\"><B>HTA tutorial series<\/B><\/A>.<\/P>\n<P>Here\u2019s the code:<\/P><PRE class=\"codeSample\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Local Users&lt;\/title&gt;<\/p>\n<p>&lt;HTA:APPLICATION \n     ID=&#8221;objHTAHelpomatic&#8221;\n     APPLICATIONNAME=&#8221;LocalUsers&#8221;\n     SCROLL=&#8221;yes&#8221;\n     SINGLEINSTANCE=&#8221;yes&#8221;\n     WINDOWSTATE=&#8221;maximize&#8221;\n&gt;\n&lt;\/head&gt;<\/p>\n<p>&lt;SCRIPT Language=&#8221;VBScript&#8221;&gt;<\/p>\n<p>    Sub Window_Onload\n        Set objNetwork = CreateObject(&#8220;Wscript.Network&#8221;)\n        strComputer = objNetwork.ComputerName\n        Set colAccounts = GetObject(&#8220;WinNT:\/\/&#8221; &amp; strComputer &amp; &#8220;&#8221;)\n        colAccounts.Filter = Array(&#8220;user&#8221;)<\/p>\n<p>        For Each objUser In colAccounts\n            Set objOption = Document.createElement(&#8220;OPTION&#8221;)\n            objOption.Text = objUser.Name\n            objOption.Value = objUser.Name\n            LocalUsers.Add(objOption)\n        Next\n    End Sub<\/p>\n<p>&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body&gt;\n    &lt;select size=&#8221;5&#8243; name=&#8221;LocalUsers&#8221;&gt;\n    &lt;\/select&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/PRE>\n<P>The part we care about is the Window_Onload subroutine. (Incidentally, we use this subroutine because Window_Onload automatically runs any time an HTA is started or any time the HTA window is refreshed.) Inside this subroutine we do two things: retrieve a list of all the local user accounts, and then take those user names and put them into a list box. We won\u2019t spend any time today talking about the process for retrieving local user accounts; all we\u2019ll do for now is tell you that we do that using these four lines of code:<\/P><PRE class=\"codeSample\">Set objNetwork = CreateObject(&#8220;Wscript.Network&#8221;)\nstrComputer = objNetwork.ComputerName\nSet colAccounts = GetObject(&#8220;WinNT:\/\/&#8221; &amp; strComputer &amp; &#8220;&#8221;)\ncolAccounts.Filter = Array(&#8220;user&#8221;)\n<\/PRE>\n<P>The part we <I>will<\/I> talk about today involves taking those user accounts and adding them to the list box. Before doing that we should point out that, when we start this HTA, the list box already exists; there just aren\u2019t any items there. Admittedly, we could have been even <I>more<\/I> dynamic and created the list box itself on-the-fly. But that was a bit more complicated than we wanted to deal with, and didn\u2019t really seem worth it anyway. In case you\u2019re wondering, here are the HTML tags that create an empty list box:<\/P><PRE class=\"codeSample\">&lt;select size=&#8221;5&#8243; name=&#8221;LocalUsers&#8221;&gt;\n&lt;\/select&gt;\n<\/PRE>\n<P>And now, at last, here\u2019s the code that populates that list box with user names:<\/P><PRE class=\"codeSample\">For Each objUser In colAccounts\n    Set objOption = Document.createElement(&#8220;OPTION&#8221;)\n    objOption.Text = objUser.Name\n    objOption.Value = objUser.Name\n    LocalUsers.Add(objOption)\nNext\n<\/PRE>\n<P>What we do here is set up a For Each loop to cycle through the collection of user accounts. For each user account we create an instance of the HTML Option object; each Option object is equivalent to an item in the list box. That\u2019s what this line of code is for:<\/P><PRE class=\"codeSample\">Set objOption = Document.createElement(&#8220;OPTION&#8221;)\n<\/PRE>\n<P>We then use these two lines of code to configure the <B>Text<\/B> and <B>Value<\/B> properties for the item:<\/P><PRE class=\"codeSample\">objOption.Text = objUser.Name\nobjOption.Value = objUser.Name\n<\/PRE>\n<P>If you haven\u2019t done a lot of work with HTML, the Text is simply the text that appears in the list box. In the sample list box shown below, <B>Option 2<\/B> is not only the user name that appears in the list box, it\u2019s also the Text of the highlighted item:<\/P><IMG border=\"0\" alt=\"HTA List Box\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/htas\/tutorial\/listbox.jpg\" width=\"321\" height=\"218\"> \n<P><BR>The Value, meanwhile, is the data reported to a subroutine when a given option is chosen. Text and Value for an option do not have to be identical. We made them identical in this case simply because it makes sense: you want the user name to show up in the list box, and you probably want the user name to be used in your subroutine. (For example, your subroutine likely binds to that particular account, and you\u2019ll need the user name in order to do that.) However, we could have set the Text to be the user name and the value to be, say, an employee ID number (assuming we had that information available to us). That\u2019s up to you.<\/P>\n<P>After configuring the Option object we add the new item to the list box like so:<\/P><PRE class=\"codeSample\">LocalUsers.Add(objOption)\n<\/PRE>\n<P>As you can see, all we do is refer to the list box (LocalUsers) and call the <B>Add<\/B> method. Along the way, we pass Add a single parameter: the object reference to our Option object. That\u2019s pretty much it: we loop around, repeat the process for all the user accounts in the collection, and we\u2019re done.<\/P>\n<P>Yes, <I>very<\/I> easy and requires very little coding. But what did you expect? After all, even when the Scripting Guys are being dynamic, we still try to expend as little effort as possible.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! In my HTA, I would like to query a computer for a list of local user accounts, then dynamically create a list box containing those accounts. How can I do that?&#8212; BN Hey, BN. You know, if you\u2019d ever seen the Scripting Guys in \u2026 action \u2026 you\u2019d think twice about asking [&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,30],"class_list":["post-69173","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! In my HTA, I would like to query a computer for a list of local user accounts, then dynamically create a list box containing those accounts. How can I do that?&#8212; BN Hey, BN. You know, if you\u2019d ever seen the Scripting Guys in \u2026 action \u2026 you\u2019d think twice about asking [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69173","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=69173"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69173\/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=69173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}