How is Get/Set-UIValue supposed to work?

Jul 5, 2011 at 5:45 PM

I have made decent progress putting together an input form, but can't get the data out of the form into the next script.

The control layout is follows:

$my_input = New-Window {
Grid {
 Grid {
  Label
  ComboBox { Items... }
  Label
  CheckBox
  Label
  CheckBox
 }
 ListView { Items... }
 Button on_click { Get-ParentControl | Set-UIValue -passThru | Close-Control }
 }
}

The Window has an on_loaded scriptblock which populates the ListView based on the default selectedindex of the ComboBox. The ComboBox also has an on_selectionchanged scriptblock that updates the listview. The intention is for the user to choose an item from the combobox, optionally select a subset of the items in the listview and check the appropriate boxes, and then click the button to run the script. However, as it's written above all the interaction works but no values are passed back into the $my_input variable. What I was hoping was that I'd have the selecteditem of the combobox, the checkbox states and the selecteditems from the listview in $my_input once the button is clicked.

What am I missing here and how should Get/Set-UIValue work in this scenario?

Developer
Jul 6, 2011 at 11:16 PM

Hey Paul could you please post a more detailed repro case?

Thanks

Doug

Jul 7, 2011 at 5:12 PM

Sure, here's a full example. It borrows heavily from your example app in your recent MSDN article.

$environments = @{ 'Staging' = ` ("staging1","webapps"), ("staging2","webapps"), ("staging3","webapps"), ("staging4","webapps"), ("staging5","webapps"), ("staging6","webapps"), ("staging7","webapps"), ("staging8","webapps"), ("staging9","webdb,logdb"), ("staging10","webdb,logdb") 'Production' = ` ("prod1","webapps"), ("prod2","webapps"), ("prod3","webapps"), ("prod4","webapps"), ("prod5","webapps"), ("prod6","webapps"), ("prod7","webapps"), ("prod8","webapps"), ("prod9","webdb,logdb"), ("prod10","webdb,logdb")} function Load-Environment ($env) { $environments.$env | %{ New-Object PSObject -Property @{Name=$_[0];Role=$_[1]} } } function Show-Environment ($window, $servers) { $e = Get-ChildControl -ByName "Environment" $servers.DataContext = Load-Environment $e.SelectedItem.Content } $my_input = New-Window -Title "Deployment Runner" -WindowStartupLocation CenterScreen -SizeToContent WidthAndHeight -Show -On_Loaded { Show-Environment $window $servers } { $labelStyle = @{ Margin = 5 HorizontalAlignment = "Right" } Grid -Columns 1 -Rows Auto, Auto, 55 { Grid -Name TestX -Row 0 -Columns 100, 100 -Rows 35, 35, 35 { Label -Content "Environment" -Row 0 -Column 0 @labelStyle New-ComboBox -Name "Environment" -Row 0 -Column 1 -Margin 5 -HorizontalAlignment "Right" -On_SelectionChanged { Show-Environment $window $servers } { $environments.Keys | %{ New-ComboBoxItem -Name $_ -Content $_ } } -SelectedIndex 0 # TextBox -Name Environment -Text Staging -Row 0 -Column 1 -Margin 5 Label -Content "Deploy Web" -Row 1 -Column 0 @labelStyle CheckBox -Name do_apps -IsChecked $true -Row 1 -Column 1 @labelstyle Label -Content "Deploy DB" -Row 2 -Column 0 @labelStyle CheckBox -Name do_dbs -IsChecked $true -Row 2 -Column 1 @labelstyle } ListView -Name Servers -Row 1 -Margin 5 -Column 0 -DataBinding @{ ItemsSource = New-Binding } -View { New-GridView -Columns { New-GridViewColumn Name New-GridViewColumn Role } } Button -Row 2 -Margin 15 _Run -IsDefault -On_Click { Get-ParentControl | Set-UIValue -passThru | Close-Control } } } $my_input

Coordinator
Jul 9, 2011 at 3:45 AM

I dono ... that seems to work for me with the fixes in the July release (ShowUI 1.1)

Developer
Jul 9, 2011 at 10:49 AM

I get an error and I also get a form plus output when I click run. I am using the latest bits.

Error in On_SelectionChanged Event Handler on Environment (ComboBox) 

At line:27 char:27+     $environments.$env | % <<<< { New-Object PSObject -Property @{Name=$_[0];Role=$_[1]} }Cannot index into a null array.

Jul 9, 2011 at 5:36 PM

Well it does work with ShowUI 1.1! I had installed it but started playing with Select-ViaUI and Get-Input so I hadn't yet tried this with the new version.

Here's the updated code that eliminates the error and works as expected. Thanks!

function Show-Environment ($window, $servers)
{
    $e = Get-ChildControl -ByName "Environment"
    $servers.DataContext = Load-Environment $e.SelectedItem
}

$my_input = New-Window -Title "Deployment Runner" -WindowStartupLocation CenterScreen -SizeToContent WidthAndHeight -Show -On_Loaded {
    $environment.DataContext = $environments.Keys
} {
    $labelStyle = @{
        Margin = 5
        HorizontalAlignment = "Right"
    }

    Grid -Columns 1 -Rows Auto, Auto, 55 {
        Grid -Name TestX -Row 0 -Columns 100, 100 -Rows 35, 35, 35 {
        
            Label   -Content "Environment" -Row 0 -Column 0 @labelStyle
            New-ComboBox -Name "Environment" -Row 0 -Column 1 -Margin 5 -HorizontalAlignment "Right" -DataBinding @{ ItemsSource = New-Binding } -On_SelectionChanged {
                Show-Environment $window $servers
            } -SelectedIndex 0
                       
           # TextBox -Name Environment -Text Staging -Row 0 -Column 1 -Margin 5

            Label   -Content "Deploy Web" -Row 1 -Column 0 @labelStyle
            CheckBox -Name do_apps -IsChecked $true -Row 1 -Column 1 @labelstyle

            Label   -Content "Deploy DB"  -Row 2 -Column 0 @labelStyle
            CheckBox -Name do_dbs -IsChecked $true -Row 2 -Column 1 @labelstyle

        }

        ListView -Name Servers -Row 1 -Margin 5 -Column 0 -DataBinding @{ ItemsSource = New-Binding } -View {
            New-GridView -Columns {
				New-GridViewColumn Name
				New-GridViewColumn Role
			}
        }

        Button -Row 2 -Margin 15 _Run -IsDefault -On_Click {
            Get-ParentControl | Set-UIValue -passThru | Close-Control
        }
    }
}