How to access Hash table after output?

Dec 23, 2012 at 2:37 PM

Hi,

Little question, how can I access the values from the created hash table after using Set-UiValue -passthr? I tried with $window to access the keys but that doesn't do the trick..

Many thanks

Jan 10, 2013 at 1:55 PM

Anyone can help?

Coordinator
Jan 10, 2013 at 3:23 PM

Hey Kurt, I'm not sure what you're trying to do, exactly. If you look in the "Examples" folder in the module, there are several examples, usually what we're doing is Set-UIValue on the parent window and then Close the window, at which point the hashtable would be returned, but there is also a Get-UIValue.

Here's one example:

$hashtable = uniformgrid -ControlName "Get-FirstNameLastNameAndAge" -Columns 2 {
    Label "Age";        TextBox -Name "Age" 18
    Label "First Name"; TextBox -Name "FirstName" John 
    Label "Last Name";  TextBox -Name "LastName" Smith
    
    button "Ok" -IsDefault -On_Click {
        Get-ParentControl | 
            Set-UIValue -passThru | 
            Close-Control
    }
} -show

You see, when you click OK this window would close and return a hashtable of the inputs.

Jan 10, 2013 at 4:57 PM

Hi Jaykul,

Thanks for your quick response. You've made a point clear to me. I was able to produce a hash table but didn't know how to access its values. In fact it is as simple as to put everything in a variable as you're showing, great!

How would I use Get-UIValue...just poll every control for its value like $variable.value?

Many thanks

Coordinator
Jan 16, 2013 at 10:02 PM

Ugh. I just wrote a great response here, and then when I hit submit, my login had expired and the response is now lost in the ethernet. Let me try again, but with a lot less patience this time:

You can call Get-UIValue, but don't call it on every control -- it's designed to recurse the child controls and collect the values in a hashtable, so you normally just want to call it once, on the container control. In fact, when you call Set-UIValue without passing a -Value parameter, it actually calls Get-UIValue to calculate the value and then sets it. NOTE: once you've explicitly Set-UIValue, future calls to Get-UIValue on that control will ALWAYS return whatever you Set, rather than calculating the value.

Simply put: you can use Get-UIValue if you want to know the value within the context of the event handler (it returns the value), but in order for the main function that you called -Show on to return the value when the window closes, you need to call Set-UIValue at some point.  In my example I called Set-UIValue instead of Get-UIValue because I want to return the hashtable as output from the New-UniformGrid function, not use it within the click handler (and because Set-UIValue can -passthru so that I can pipe to Close-Control).

I'll try to write a blog post or documentation page this week about creating CustomControls for ShowUI, but in the meantime, you could also have a look at the Select-Date function in the ShowUI\CommonControls folder, it will show you how to set the [OutputType()] to indicate both the control type and the object type that the control creates when you call Get-UIValue on it, and will show you a simple example of how and when to update the value using Set-UIValue.

Jan 17, 2013 at 7:17 PM

Ok thank you for your great response!

I looked at the Select-Date function. It's a nice piece of code but goes beyond my current knowledge of Powershell & ShowUI. ;-)

Am I right that $window and Get-ParentControl can both be used as 'parent'?

Coordinator
Jan 18, 2013 at 7:52 AM

Well, $window is always the containing window.  The $window is the root, it contains everything else.

Get-ParentControl just returns the immediate container. That means it doesn't generally return the window, but rather a panel that's somewhere inside that window. It returns the parent of the control that's calling Get-ParentControl -- the immediate container that's being used for layout purposes (usually a Grid, StackPanel, WrapPanel, UniformGrid, etc.).  In WPF, most of the time you have a lot of nested controls to get the UI the way you want it... In ShowUI it's always pretty clear who the parent is, because each parent will have a scriptblock where it's children (or "content") are defined.

Specifically, in the example above, you're defining a UniformGrid -- ShowUI creates a Window for you when you put the -Show parameter on.  Get-ParentControl is going to return the UniformGrid, whereas $window is returning the outer window. If you called Get-ParentControl | Get-ParentControl, then you'd get the Window that contains the UniformGrid...

NOTE: Normally, it wouldn't matter which one you used in a simple example like this.  However, since you put a -ControlName on your UniformGrid, you've isolated it (we will now treat it as a single control, rather than as a container, and Get-UIValue won't recurse through it). So if you had used Set-UIValue $window -passthru in the example instead of Get-ParentControl | Set-UIValue -passthru  ... the window wouldn't return anything when it closed, because the window would have no value set on it, and the item inside the window (the $Window.Content) would have no UIValue set either.

I hope that helps...

Jan 18, 2013 at 9:42 AM

Ok think I got it! Briefly, $window is the parent of all (child) parents unless I isolate a child parent by giving it a controlname..

Many thanks for your great explanation!