Visual Studio, how to get files to show in Listbox Powershell

Viewed 318

I have created a PowerShell GUI that combines 2 pdf documents, then converts it to a .tif, then renames it and finally moves it to its final location.

I would like to use a ListBox to show the actions of the above scripts if this is possible

So in the first ListBox it needs to show the 2 pdf files that are going to be combined.

I cant seem to get it to show any file!

$listBox1_SelectedIndexChanged = {
    $listBox1.Text = Get-ChildItem "E:\SIGNEDNOTES" -Filter *.pdf
}
$listBox1_SelectedIndexChanged = {

    $rootFolder = "E:\SIGNEDNOTES\" 
    $subfolders = (Get-ChildItem -Path $rootFolder -Filter *.pdf -Recurse -Directory).FullName
    $listBox1 = New-Object System.Windows.Forms.ListBox
    $listBox1.Items.AddRange($subfolders)

}

enter image description here enter image description here

1 Answers

To add new items to a ListBox you need to populate the ListBox.Items collection. Here's a quick example.

Given this XAML

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ListBox x:Name="listBox" HorizontalAlignment="Left" Height="138" Margin="84,103,0,0" VerticalAlignment="Top" Width="332"/>

    </Grid>
</Window>

I'm assuming you've already got a method to use a XAML layout in PowerShell, but if you don't, you can use the method I cover in this blog post 'Using GUI Elements in PowerShell'.

My listBox is called $WPFlistBox, so here's how I would populate that.

$items = get-childItem c:\temp\*.exe 
ForEach($item in $items){
   $WPFlistBox.Items.Add($item.FullName)
}   

Just change the variables to match your own and you should be well on your way.

Here's an example of what it looks like. enter image description here

Related