0

我正在使用 Powershell 创建一个 GUI 日历,并且我需要能够为用户选择它们时选择的日期数分配一个变量。我想我将不得不使用一个事件,但不知道如何进行。基本上,如果用户选择超过 10 天的范围,我需要在同一个表单上的复选框进行检查。我正在用列表框做类似的事情,但我似乎无法弄清楚如何对日历做同样的事情。谢谢。

列表框代码:

$Listbox2 = New-Object System.Windows.Forms.ListBox 
$Listbox2.Location = New-Object System.Drawing.Size(240,80) 
$Listbox2.Size = New-Object System.Drawing.Size(140,210) 
$Listbox2.Height = 210
$Listbox2.SelectionMode = "MultiExtended"
$Listbox2Content | ForEach-Object {[void] $Listbox2.Items.Add($_)}
$Listbox2.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",11,0,3,1)
$Listbox2.Add_SelectedValueChanged({
    If (($Listbox2.SelectedItems).Count -ge 10) {$Checkbox2.Checked = $True}
    If (($Listbox2.SelectedItems).Count -lt 10) {$Checkbox2.Checked = $False}
    })

日历代码:

$Calendar = New-Object System.Windows.Forms.MonthCalendar
$Calendar.Location = New-Object System.Drawing.Size(12,80)
$Calendar.ShowTodayCircle = $False
$Calendar.ShowToday = $True
$Calendar.MaxDate = (Get-Date).AddDays(1)
$Calendar.MinDate = $OldestLog
$Calendar.MaxSelectionCount = "$CalendarDateRange"
$MenuBox.Controls.Add($Calendar)
4

1 回答 1

1

脚本块打印所选日期的$data_selected数量。我将它附加到DataSelected事件中,以便在您单击日期或进行范围选择时触发。

Add-Type -AssemblyName System.Windows.Forms
$date_selected = {
    write-host (($_.End - $_.Start).Days + 1)
}

$form = New-Object Windows.Forms.Form   
$form.text = "Calendar"
$form.FormBorderStyle = 'FixedDialog'
$form.Size = New-Object Drawing.Size(190,190) 

$cal = New-Object System.Windows.Forms.MonthCalendar   
$cal.add_DateSelected($date_selected)

$form.Controls.Add($cal)
$form.Add_Shown($form.Activate())  
$form.showdialog() | Out-Null  
于 2013-03-26T03:06:48.557 回答