Learn about Windows PowerShell
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.
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.
The first thing you will want to do, NH, is to ensure you have downloaded a copy of SAPIEN’s PrimalForms Community Edition. This was mentioned in yesterday’s Hey, Scripting Guy! post. You will also want to review yesterday’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—procedures used in today’s Hey, Scripting Guy! post.
After you have installed PrimalForms, open it and drag a List Box control to the Windows Form. The ListBox class is documented on MSDN, and contains a number of methods and properties. As you are dragging the List Box 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.
After you have added the list box, on the right side of the screen in the Data section, click the Items Collection button, as shown in the following image.
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.
Name the list box lb_List. 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.
Name the top label lbl_SelectedText and the bottom lbl_Select. Set the text property of lbl_SelectedText to Text, and the text property of lbl_Select to Selected. Now drag a button control to the form. Name the button control btn_Update, and set the text property of the button control to Update. Your form should look similar to the one shown in the following image.
It is now time to wire up the OnClick event for the btn_Update. NH, what you need to do is retrieve the selected item from the list box. Luckily, there is a SelectedItem property from the ListBox class that will retrieve that information for us. Assign that value to the Text property of the lbl_SelectedText control. This is the same type of thing we did yesterday with the Scripting Guys’ clock. The applicable code is seen here:
#Provide Custom Code for events specified in PrimalForms.
#TODO: Place custom script here
$lbl_SelectedText.Text = $lb_list.SelectedItem
When the ListBox1.ps1 script runs, select the first item and then click Update. Notice that the text of the label has changed, as shown in the following image.
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 Script Center Script Repository. Graphical Windows PowerShell Week will continue tomorrow.
If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at firstname.lastname@example.org or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson and Craig Liebendorfer, Scripting Guys
<p>Thanks for the post. What if I have a data file that is changing frequently and I want to populate the list box with data from that? In this case an XML file? </p>