我有一个powershell日历,允许选择日期范围。MaxDate属性设置为当天。我遇到的问题是,我可以选择今天的日期,但不能作为范围的一部分。我可以选择多个日期,但是一旦我在选择中包含了今天的日期,它就只选择今天的日期。问题可能出在MaxDate属性上,因为如果删除它,我可以选择今天的日期作为范围的一部分,但我不想这样做,因为这将允许在未来的几天内进行选择。有没有办法添加今天的日期,让它成为所选范围的一部分?代码如下。谢谢。
$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) 发布于 2013-02-21 06:50:13
似乎MaxDate的值在某个范围内不可用。这可能是有原因的,但让我们称其为bug。一种解决方法是将第二天用作MaxDate,并手动处理未来日期的选择,如下所示:
#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)https://stackoverflow.com/questions/14990843
复制相似问题