Items in listbox

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 7 years and 2 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
v35678
Posts: 23
Last visit: Fri Jan 05, 2024 8:25 am

Items in listbox

Post by v35678 »

Hi is they any way to take the items that are listed in a listbox and export them to a text file or csv? Thanks for any help in advance.
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: Items in listbox

Post by Alexander Riedel »

Sure there is. Create a stringbuilder object, iterate over the items in the list and add them line by line.
The use something like out-file to write the string to a file.
If you use a listview rather than a listbox, then you can use the Items property. It has a CopyTo method that can be used to export the items to an array, which in turn you can pipe to out-file.
(Not sure at the moment if you can pipe the Items property itself)

Depending on the amount of data, the method of writing to a file may be of interest.
See https://blogs.technet.microsoft.com/gbo ... to-a-file/
Alexander Riedel
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: Items in listbox

Post by jvierra »

if the items in the listbox are objects: $listbox1.Items | Export-Cs <filename>

If the items are just simple strings:
$listbox1.Items |Out-File <filename>
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Items in listbox

Post by jvierra »

Here is a simple way to test and inspect and forms control without a form.

At a CLI prompt do the following (PS5)
  1. PS > add-type -AssemblyName System.windows.forms
  2.  
  3. PS > using namespace System.Windows.Forms
  4. PS > $lb = [Listbox]::New()
  5. PS > $lb.Items.AddRange(@('one','two','three'))
  6. PS > $lb.Items
  7. one
  8. two
  9. three
  10. PS > $lb.Items | Out-File lbtest.txt
  11. PS > cat lbtest.txt
  12. one
  13. two
  14. three
  15.  
  16. # to a csv
  17. PS >$lb.Items | Export-Csv lbtest.csv -Header LBItems -NoType
  18.  
  19.  
  20. PS >
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Items in listbox

Post by jvierra »

Here is how to use objects in a listbox and export them as a CSV. You can use PowerShell to export the same as any other object collection.
  1. add-type -AssemblyName System.windows.forms
  2.  
  3. using namespace System.Windows.Forms
  4. $lb = [Listbox]::New()
  5. $files = Get-ChildItem c:\
  6.  
  7. # convert to loadable collection
  8. $lb.DataMember = 'Name' # pick property to display
  9. $lb.DataSource = [collections.arraylist]$files
  10.  
  11. $lb.DataSource | Export-Csv lbtest.csv -NoType
  12.  
  13. $lb.DataSource  | Export-CliXml lbtest.clixml
  14.  
  15. $lb.DataSource | Format-Table
If you need to export only selected items then use $lb.SelectedItems instead of $lb.DataSource.
User avatar
v35678
Posts: 23
Last visit: Fri Jan 05, 2024 8:25 am

Re: Items in listbox

Post by v35678 »

Thanks for the help. Really appreciate it.
This topic is 7 years and 2 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