Question

I have a powershell calendar that allows a selection range of dates. The MaxDate property is set as the current day. The issue I am having is that I can select today's date but not as part of the range. I can select multiple dates but as soon as I include today's date in the selection it selects only today's date. The issue is likely the MaxDate property because if I remove it I can select today's date as part of the range, but I don't want to do that because that will allow selection for days in the future. Any ideas how to add today's date and allow it to be part of the selected range? The code is below. Thanks.

    $Calendar = New-Object System.Windows.Forms.MonthCalendar 
    $Calendar.Location = New-Object System.Drawing.Size(10,80)
    $Calendar.ShowTodayCircle = $False
    $Calendar.MaxDate = Get-Date
    $Calendar.MinDate = $OldestLog
    $Calendar.MaxSelectionCount = "365"
    $MenuBox.Controls.Add($Calendar) 
Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top