将日历添加到ComboBox

时间:2019-04-14 20:09:27

标签: powershell combobox calendar

我正在创建一个新用户表格,希望技术人员能够选择AD帐户的结束日期。如果没有选择结束日期,则将过期设置为无限制。我正在尝试将日历添加到组合框,但不会显示。

我尝试改用DateTimePicker,但这不允许我在文本字段中编辑日期,也不允许我完全不选择日期

$box = New-Object System.Windows.Forms.ComboBox
$box.Location = New-Object System.Drawing.Point(130,390)
$box.Size = New-Object System.Drawing.Size(250,30)
$box.DropDownHeight = 200

$calendar = New-Object System.Windows.Forms.MonthCalendar
$calendar.ShowTodayCircle = $false
$calendar.MaxSelectionCount = 1 

$box.Items.Add($calendar)

我想让组合框在单击箭头时在下拉列表中显示日历。

1 个答案:

答案 0 :(得分:0)

有许多示例说明了如何实现此目标。 WPF更加优雅,但是您正在使用WinForms,因此它不那么灵活。

您可以尝试考虑以下其中一项:

Difference between Calendar & Date Picker control in WPF?

Working with WPF Calendar

Drop down Date Range Picker

总而言之,您的查询与此重复,但这是针对WPF的,您正在使用WinForm。...

How do I get DatePicker and TextBox information from XAML into a Powershell variable?

WinForm方法就是这样...

Two Calendar Controls for PowerShell

以上代码示例太长,无法在此处发布。因此,点击它们并根据需要复制粘贴。

或者这个

Hey, Scripting Guy! Can Windows PowerShell Alleviate My Need to Type Dates?

Get-DatePickerFunction.ps1 
Function Get-DatePicker 
{   
    [System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) | 
    Out-Null 

    $WinForm = New-Object Windows.Forms.Form   
    $WinForm.text = “DatePicker Control”   
    $WinForm.Size = New-Object Drawing.Size(205,55) 

    $DatePicker = New-Object System.Windows.Forms.DateTimePicker   
    #$DatePicker.Format = [windows.forms.datetimepickerFormat]::custom 
    #$DatePicker.CustomFormat = “MM-dd-yyyy” 
    $WinForm.Controls.Add($DatePicker)   

    $WinForm.Add_Shown($WinForm.Activate())  
    $WinForm.showdialog() | Out-Null  
    $DatePicker.value 
} #end function Get-DatePicker 

# *** Entry point to script *** 

# Get-EventLog -LogName application -After [datetime](Get-Datepicker) # Windows PowerShell 2.0 call 
# wps 1.0 version of call 
$dte = [datetime](Get-DatePicker) 
Get-EventLog -LogName application | 
Where-Object { $_.timeWritten -gt $dte } 

关于根本不选择日期。将其设置为null除非您强制执行此操作,否则应仅跳过该条目。上面的示例表明,返回值可以是可选的。