Simple question - I cannot get the selected date

Apr 6, 2013 at 11:53 AM
Hi,

Why does this simple code doesn't give me the selected date?
Import-Module ShowUI

$Res = StackPanel -ControlName DatumsAuswahl {
 Select-Date -Name After 
 New-Button -Content Fertig -On_Click {
    Get-ParentControl | Set-UIValue -PassThru | Close-Control
 }
} -Show

$Res 
$Res is a hashtable without any elements.

The demo sample doesn't work either:
$getEventInput = StackPanel -ControlName 'Get-EventLogsSinceDate' {            
    New-Label -VisualStyle 'MediumText' "Log Name"            
    New-ComboBox -IsEditable:$false -SelectedIndex 0 -Name LogName @("Application", "Security", "System", "Setup")            
    New-Label -VisualStyle 'MediumText' "Get Event Logs Since..."            
    Select-Date -Name After           
    New-Button "Get Events" -On_Click {            
        Get-ParentControl |            
            Set-UIValue -passThru |             
            Close-Control            
    }            
} -ShowUI
            
Get-EventLog @getEventInput
I'll get an "cannot find toggle button parameter" error because the result contains LogName and ToggleButton (?) as properties.

I am using Show UI 1.4 (Windows Server 2012 and ISE 3.0)

I would like to show some ShowUI samples for a user conference.

regards,
Petr
Coordinator
Apr 6, 2013 at 9:15 PM
For whatever reason (I don't remember why, but I didn't write Select-Date) the Select-Date control does not default to today, it defaults to nothing -- so if you just click "Fertig" without first clicking on the calendar control, it doesn't have any output. You will have the same problem in the second example if you don't click on the date.

The LogName is supposed to be in the output of the second example (it's a parameter for Get-EventLog), but the toggleButton is not. Looking at it, I'm not even sure where toggleButton is coming from.

This brings up some interesting points. The main one which is that if you're on PowerShell 3, then you have WPF 4.0!

Almost all of the examples in ShowUI are written for WPF3 (and will continue working), but there are some new controls (e.g. "Calendar" and "DatePicker") from Microsoft in WPF 4. For instance, in WPF 3 there were no date controls at all built-in from Microsoft, so we wrote Select-Date using a WIndows.Forms.MonthCalendar control inside a WindowsFormsHost so you'd have a built-in way of getting dates. It doesn't look quite as nice as a real WPF control, and is slightly more complicated to use (which is why we wrote Select-Date for you).

Anyway, in PowerShell 3 (or rather, in WPF4, because you can use .Net4 with PowerShell 2) instead of this:
$date = Select-Date -Show
You could write this:
$date = New-DatePicker -Show | Get-Date
Or this:
New-Calendar -On_SelectedDatesChanged { $this.Tag = $this.SelectedDates[0] } -Show
I'll actually add "SelectedDates" to the list of automatically emitted properties so that in the next release you can leave off the event handler, not sure how we missed that ;-)

Of course, you don't have to pipe through Get-Date, but the DatePicker will output a string otherwise ;-)
Apr 7, 2013 at 9:17 PM
Hi,

Thanks for taking your time to look into this.

I tried the sample with Windows XP and PowerShell 2.0 and it works fine (as expected;). So it has something to do with the fact that I using WPF 4.x.

When I try New-Calendar it works but I can't use splatting because the ToggleButton property is still there:
$getEventInput = StackPanel -ControlName 'Get-EventLogsSinceDate' {            
    New-Label -VisualStyle 'MediumText' "Log Name"            
    New-ComboBox -IsEditable:$false -SelectedIndex 0 -Name LogName @("Application", "Security", "System", "Setup")            
    New-Label -VisualStyle 'MediumText' "Get Event Logs Since..."            
    New-Calendar -On_SelectedDatesChanged { $this.Tag = $this.SelectedDates[0] } -Name After
    New-Button "Get Events" -On_Click {            
        Get-ParentControl |            
            Set-UIValue -passThru |             
            Close-Control            
    }            
} -ShowUI
            
Get-EventLog -LogName $geteventInput.LogName -After $getEventInput.After
Regards,
Peter
Coordinator
Apr 8, 2013 at 9:21 PM
I still can't figure out why that's happening, but it's because of the ComboBox ... if you replace it with a ListBox the toggleButton goes away. I don't know why the toggleButton property of the ComboBox is being picked up by the Set-UIValue function, I'll have to look into that. In the meantime, either:
  • Use a ListBox instead of a ComboBox
  • Remove the ToggleButton from the output before you call Get-EventLog: $Null = $getEventInput.Remove("toggleButton")