Populating a Listbox from an XML file

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 3 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
james burger
Posts: 4
Last visit: Thu Nov 05, 2020 8:49 am

Populating a Listbox from an XML file

Post by james burger »

Product, version and build: SAPIEN PowerShell Studio 2020, Version 5.7.180
Operating system: Windows 10, 10.0.18363
PowerShell version: 5.1.18362.752
32 or 64 bit OS: 64

I'm new to PowerShell Studio and forms. How can I build a Listbox with a drop down list that is populated from an XML file? I want to use this to make selections from the listbox and store the results in an object.
User avatar
brittneyr
Site Admin
Posts: 1672
Last visit: Thu Apr 18, 2024 7:41 am
Answers: 39
Been upvoted: 31 times

Re: Populating a Listbox from an XML file

Post by brittneyr »

[Topic moved to PowerShell GUIs by moderator]
Brittney
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Populating a Listbox from an XML file

Post by jvierra »

Hi James,

You need to load the XML into an object then extract the items you need into an array that can be assigned to the ListBox.

What have you tried?
User avatar
james burger
Posts: 4
Last visit: Thu Nov 05, 2020 8:49 am

Re: Populating a Listbox from an XML file

Post by james burger »

Thanks for your reply. So I'm new to forms but not PowerShell and scripting. The only thing I have tried was using the controls on the form. I got the listbox to work by populating "Items" list. I guess before I read the items in from and XML file I need to know how to populate the items from an array. Can you help with this? do you have any examples?

On a related question, how to I make the Listbox behave as a dropdown list instead of the scrolling controls on the side?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Populating a Listbox from an XML file

Post by jvierra »

$listbox1.Itema.AddRange($array)

The toolbox controls can be right-clicked and the menu has links to the docs and SpotLight articles that give examples of how to work with most controls.
User avatar
james burger
Posts: 4
Last visit: Thu Nov 05, 2020 8:49 am

Re: Populating a Listbox from an XML file

Post by james burger »

there are no Spotlight articles for this and the MSDN examples are all C# instead of PowerShell.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Populating a Listbox from an XML file

Post by jvierra »

The MSDN docs explain how each property/method/event work. C# is very similar to PowerShell so it provides some clues.

The first thing that is required to use forms is to read the articles in the Info Center that will help you to understand what forms are and how they are intended to be used. Without some basic learning forms will be very challenging application of PowerShell. Forms also requires more than just a passing knowledge of PowerShell. You will have to learn PowerShell at a more fundamental level.

You will also need to learn how to read technical documentation. The links will take you to the available objects and the properties and methods of those objects. "Items" is a collection of special objects. Take time to learn to navigate and understand teh docs.

With form there are many thousands or properties/methods and events that are available. It is nearly impossible for anyone to answer every question without referencing the docs. WinForms is one of the largest APIs anywhere for a single subsystem. Learning how it is deployed is critical to using it in more than the simplest way. For simple forms the InfoCenter has almost everything you need along with many examples.

For the "Items" collection method see the following.

https://docs.microsoft.com/en-us/dotnet ... tem_Object___
This topic is 3 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked