It seems that the value of MaxDate
is not available in a range. There may be a reason for it, but let's call it a bug. A workaround would be to use the next day as the MaxDate
and handle selection of future days manually, like this:
#Handler to check and save selected date
$handler_Calendar_DateChanged=
{
Write-Host "$Calendar.SelectionRange"
if ($Calendar.SelectionRange.End -gt (Get-Date)) {
[System.Windows.Forms.MessageBox]::Show("You can't select a date in the future.", "Invalid date", [System.Windows.Forms.MessageBoxButtons]::OK ,[System.Windows.Forms.MessageBoxIcon]::Error)
#Select todays date
$Calendar.SetDate((Get-Date))
} else {
#Store selected daterange
$global:daterange = $Calendar.SelectionRange
}
}
#Later when you specify the calendar object
$Calendar.MaxDate = (Get-Date).AddDays(1)
$Calendar.add_DateChanged($handler_Calendar_DateChanged)