{"id":51133,"date":"2010-03-02T00:01:00","date_gmt":"2010-03-02T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/03\/02\/hey-scripting-guy-how-can-i-use-the-list-box-control-in-windows-powershell\/"},"modified":"2010-03-02T00:01:00","modified_gmt":"2010-03-02T00:01:00","slug":"hey-scripting-guy-how-can-i-use-the-list-box-control-in-windows-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-use-the-list-box-control-in-windows-powershell\/","title":{"rendered":"Hey, Scripting Guy! How Can I Use the List Box Control in Windows PowerShell?"},"content":{"rendered":"<p class=\"MsoNormal\"><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a>&nbsp;<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" 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\">Hey, Scripting Guy! I am wondering if it would be possible to have a box that included several items in it. I would then like to be able to choose an item from that box and use it elsewhere in the script. I know it can be done, because I see it in Windows programs all the time. I just am not sure what it is called, and therefore am having a problem finding information about how to accomplish this using Windows PowerShell. <\/p>\n<\/p>\n<p class=\"MsoNormal\">&#8212; NH<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" 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\">Hello NH, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. My friend, Don, has been in Australia for the last few weeks and he has been posting lots of pictures on Facebook. This got me looking at some of the pictures I took during my various trips down under. I took in the following picture in Surfers Paradise on the Gold Coast of Australia. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Photograph of Surfers Paradise on the Gold Coast of Australia\" alt=\"Photograph of Surfers Paradise on the Gold Coast of Australia\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0302\/hsg-03-02-10-01.jpg\" width=\"585\" height=\"386\"><\/p>\n<p class=\"Fig-Graphic\">&nbsp;<\/p>\n<p class=\"MsoNormal\">The first thing you will want to do, NH, is to ensure you have downloaded a copy of SAPIEN&rsquo;s PrimalForms Community Edition. This was mentioned in <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2010\/03\/01\/hey-scripting-guy-march-1-2010.aspx\"><span>yesterday&rsquo;s Hey, Scripting Guy! post<\/span><\/a>. You will also want to review yesterday&rsquo;s Hey, Scripting Guy! post, if you did not read it, because it talks about the naming of controls as well as working with label and button controls&mdash;procedures used in today&rsquo;s Hey, Scripting Guy! post. <\/p>\n<p class=\"MsoNormal\">After you have installed PrimalForms, open it and drag a <b>List Box<\/b> control to the Windows Form. The <strong>ListBox<\/strong> class is <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.forms.listbox.aspx\"><font face=\"Segoe\">documented on MSDN<\/font><\/a>, and contains a number of methods and properties. As you are dragging the <strong>List Box<\/strong> onto your form, keep moving your mouse until the blue guidelines indicate you are close to the right side of your form. At this point, you can release your mouse button. This is shown in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of dragging a List Box onto the form\" alt=\"Image of dragging a List Box onto the form\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0302\/hsg-03-02-10-02.jpg\" width=\"302\" height=\"309\"><\/p>\n<p class=\"Fig-Graphic\">After you have added the list box, on the right side of the screen in the <b>Data<\/b> section, click the <b>Items Collection<\/b> button, as shown in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of clicking Images Collection\" alt=\"Image of clicking Images Collection\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0302\/hsg-03-02-10-03.jpg\" width=\"217\" height=\"307\"><\/p>\n<p class=\"Fig-Graphic\">&nbsp;<\/p>\n<p class=\"MsoNormal\">Now the String Collection Editor appears. This editor will allow you to add items that will appear in the list box when the code is run. If you have only a few items that will never change, this is a great way to add information to the list box. As shown in the following image, add only one item per line. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of adding one item per line to list box\" alt=\"Image of adding one item per line to list box\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0302\/hsg-03-02-10-04.jpg\" width=\"491\" height=\"319\"><\/p>\n<p class=\"Fig-Graphic\">&nbsp;<\/p>\n<p class=\"MsoNormal\">Name the list box <b>lb_List<\/b>. Now add two label controls to the form. As you drag the controls, use the blue guidelines to help with alignment, as shown in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of using blue guidelines for help with alignment\" alt=\"Image of using blue guidelines for help with alignment\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0302\/hsg-03-02-10-05.jpg\" width=\"309\" height=\"327\"><\/p>\n<p class=\"Fig-Graphic\">Name the top label <b>lbl_SelectedText<\/b> and the bottom <b>lbl_Select<\/b>. Set the text property of <b>lbl_SelectedText<\/b> to <b>Text<\/b>, and the text property of <b>lbl_Select<\/b> to <b>Selected<\/b>. Now drag a button control to the form.&nbsp;Name the button control <b>btn_Update<\/b>, and set the text property of the button control to <b>Update<\/b>. Your form should look similar to the one shown in the following image. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of how the form should look\" alt=\"Image of how the form should look\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0302\/hsg-03-02-10-07.jpg\" width=\"600\" height=\"417\"><\/p>\n<p class=\"Fig-Graphic\">&nbsp;<\/p>\n<p class=\"MsoNormal\">It is now time to wire up the <b>OnClick<\/b> event for the <b>btn_Update<\/b>. NH, what you need to do is retrieve the selected item from the list box. Luckily, there is a <b>SelectedItem<\/b> property from the <b>ListBox<\/b> class that will retrieve that information for us. Assign that value to the <b>Text<\/b> property of the <b>lbl_SelectedText<\/b> control. This is the same type of thing we did yesterday with the Scripting Guys&rsquo; clock. The applicable code is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">#Provide Custom Code for events specified in PrimalForms.<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$btn_Update_OnClick= <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">{<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">#TODO: Place custom script here<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$lbl_SelectedText.Text = $lb_list.SelectedItem <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">&nbsp;<\/font><\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">}<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">When the ListBox1.ps1 script runs, select the first item and then click <b>Update<\/b>. Notice that the text of the label has changed, as shown in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image showin text of button label has changed\" alt=\"Image showin text of button label has changed\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0302\/hsg-03-02-10-08.jpg\" width=\"300\" height=\"276\"><\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\">NH, that is all there is to using the list box control in Windows PowerShell. The complete text of the ListBox1.ps1 script is on the <a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/en-us\/de33d1e7-eee6-495b-8d23-402dcd5c75d8\"><font face=\"Segoe\">Script Center Script Repository<\/font><\/a>. Graphical Windows PowerShell Week will continue tomorrow. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a title=\"Facebook\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><font face=\"Segoe\">Facebook<\/font><\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p class=\"MsoNormal\">&nbsp;\n<b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/p>\n<p><\/span><\/b><\/p>\n<p><b><span><\/span><\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I am wondering if it would be possible to have a box that included several items in it. I would then like to be able to choose an item from that box and use it elsewhere in the script. I know it can be done, because I see it in [&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":[71,3,4,45],"class_list":["post-51133","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-graphical","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I am wondering if it would be possible to have a box that included several items in it. I would then like to be able to choose an item from that box and use it elsewhere in the script. I know it can be done, because I see it in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51133","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=51133"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51133\/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=51133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}