0

I created a .xaml file using Visual Studio to be used in Powershell. the purpose of the script i am trying to create is designed to popup for the user and have them select 1 of the provided options and hit the button. It will then schedule a restart based on the time they selected in the listbox. I have them somewhat working but the problem is that it when clicking on the button it keeps returning the same value despite the listbox changing values.

i have been running this in the Powershell ISE. the Write-Host $listBox.SelectedIndex and Write-Host $listBox.SelectedItem returns System.Windows.Controls.ListBoxItem: 3 Hours and System.Windows.Controls.ListBox Items.Count:4 again despite the listbox changing values between clicks.

I know powershell decently well but the xaml i am brand new with

Powershell code

Add-Type -AssemblyName PresentationFramework

#gets the xaml information from the app.xaml, its the design of the window
[XML]$form = Get-Content "$PSScriptRoot\app.xaml"
$NR = (New-Object System.Xml.XmlNodeReader $form)
$win = [Windows.Markup.XamlReader]::Load( $NR )

function execute-restart{
        [CmdletBinding()]
    Param(
        [Parameter(Mandatory)]
        [System.Object] $listBox
    )

   Write-Host $listBox.SelectedIndex
   Write-Host $listBox.SelectedItem
   Write-Host $listBox
}

$listBox = $win.FindName('listBox')

$button = $win.FindName('restart')

#the button
$button.add_click({
    execute-restart $listBox

})

$win.showdialog()

Xaml code

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    Title="Your computer needs a restart" Height="450" Width="800" Icon="C:\ITTemp\PoShGuid\wallpaper.jpg" WindowStyle="None" ResizeMode="NoResize">
<Grid>
    <Image Name="image" HorizontalAlignment="Left" Height="450" Margin="0" VerticalAlignment="Top" Width="800" Source="C:\ITTemp\PoShGuid\wallpaper.jpg" Stretch="Fill"/>
    <Label Name="TopLabel" Content="there is a need to restart your computer" HorizontalAlignment="Left" Margin="50,23,0,0" VerticalAlignment="Top" Height="45" Width="465" FontSize="24"/>
    <Label Name="BottomLabel" Content="Please select when you would like to reschedule your pc" HorizontalAlignment="Left" Margin="50,73,0,0" VerticalAlignment="Top" Width="635" Height="44" FontSize="24"/>
    <ListBox Name="listBox" HorizontalAlignment="Left" Height="40" Margin="125,330,0,0" VerticalAlignment="Top" Width="256" FontSize="24" SelectionMode="Single" IsSynchronizedWithCurrentItem="True">
        <ListBoxItem Name="ListBoxItem1" Content="Immediately" FontSize="24" VerticalAlignment="Center" IsSelected="True"/>
        <ListBoxItem Name="ListBoxItem2" Content="1 Hour" FontSize="24" VerticalAlignment="Center" IsSelected="True"/>
        <ListBoxItem Name="ListBoxItem3" Content="2 Hours" FontSize="24" VerticalAlignment="Center" IsSelected="True"/>
        <ListBoxItem Name="ListBoxItem4" Content="3 Hours" FontSize="24" VerticalAlignment="Center" IsSelected="True"/>
    </ListBox>
    <Button Name="restart" Content="Restart In" HorizontalAlignment="Left" Margin="407,330,0,0" VerticalAlignment="Top" Width="203" Height="40"/>

</Grid>

I hope this makes sense, and i thank you for taking the time to look at my problem.

TAbdiukov
  • 1,185
  • 3
  • 12
  • 25
  • Welcome to SO! Does this answer your question? [Get ListView Visible items](https://stackoverflow.com/questions/11187382/get-listview-visible-items). That particular example uses ListView, but the same code can be used with ListBox. You'll also need [the code for GetVisualChild](https://social.msdn.microsoft.com/Forums/vstudio/en-US/8543eaa7-bd43-41ad-aee8-874d9b30799c/how-to-find-the-rows-listviewitems-in-a-listview-that-are-actually-visible?forum=wpf). Good luck! – Mark Feldman Feb 04 '20 at 00:30
  • I think the issue here is that you assume that the currently visible item is the same as the currently selected item. That is not the case. Perhaps a ComboBox would better suite your needs here. – TheMadTechnician Feb 04 '20 at 02:29
  • @TheMadTechnician i had tried the ComboBox first but it was doing the same thing as the ListBox. I tried reading the links that Mark sent and they just weren't making sense on how to implement them (as I'm so new to this) so I decided to try a ComboBox again and its now working! I am now getting the responses I was expecting! Thank you – Keith Brown Feb 04 '20 at 18:42

1 Answers1

0

At @TheMadTechnician's suggestion I decided to retry using a ComboBox instead of a ListBox which started working. I am not sure on the details behind why, but that is what I did.

This is the code I ended up using.

<ComboBox Name="comboBox" HorizontalAlignment="Left" Margin="142,330,0,0" VerticalAlignment="Top" Width="217" Height="40" FontSize="24" SelectedIndex="1">
    <ListBoxItem Name="ListBoxItem1" Content="Immediately" FontSize="24" VerticalAlignment="Center"/>
    <ListBoxItem Name="ListBoxItem2" Content="1 Hour" FontSize="24" VerticalAlignment="Center"/>
    <ListBoxItem Name="ListBoxItem3" Content="2 Hours" FontSize="24" VerticalAlignment="Center"/>
    <ListBoxItem Name="ListBoxItem4" Content="3 Hours" FontSize="24" VerticalAlignment="Center"/>
</ComboBox>