gpt4 book ai didi

winforms - PowerShell 日历弹出窗口

转载 作者:行者123 更新时间:2023-12-02 23:45:52 26 4
gpt4 key购买 nike

我正在尝试使用 http://technet.microsoft.com/en-us/library/ff730942.aspx 中的 PowerShell 脚本

选择日期并按 Enter 后,变量 $dtmDate 没有数据。请帮忙。是否可以添加确定/取消按钮?

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

$objForm = New-Object Windows.Forms.Form

$objForm.Text = "Select a Date"
$objForm.Size = New-Object Drawing.Size @(243,200)
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True

$objForm.Add_KeyDown({
if ($_.KeyCode -eq "Enter")
{
$dtmDate=$objCalendar.SelectionStart
$objForm.Close()
}
})

$objForm.Add_KeyDown({
if ($_.KeyCode -eq "Escape")
{
$objForm.Close()
}
})

$objCalendar = New-Object System.Windows.Forms.MonthCalendar
$objCalendar.ShowTodayCircle = $False
$objCalendar.MaxSelectionCount = 1
$objForm.Controls.Add($objCalendar)

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

if ($dtmDate)
{
Write-Host "Date selected: $dtmDate"
}

最佳答案

$dtmDate在您的 if 语句的上下文中没有任何值(value),因为该代码没有在您的 enter 按键上执行... Add_KeyDown 上下文中的 block 是。尝试执行类似这样的操作:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

$dtmDate = $null

# I've moved your Date printing logic into this function that can be called elsewhere.
Function Do-Work
{
if ($dtmDate -ne $null)
{
Write-Host "Date selected: $dtmDate"
}
}

$objForm = New-Object Windows.Forms.Form

$objForm.Text = "Select a Date"
$objForm.Size = New-Object Drawing.Size @(243,200)
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True

$objForm.Add_KeyDown({
if ($_.KeyCode -eq "Enter")
{
$dtmDate = $objCalendar.SelectionStart
$objForm.Close()

Do-Work
}
})

$objForm.Add_KeyDown({
if ($_.KeyCode -eq "Escape")
{
$objForm.Close()
}
})

$objCalendar = New-Object System.Windows.Forms.MonthCalendar
$objCalendar.ShowTodayCircle = $False
$objCalendar.MaxSelectionCount = 1
$objForm.Controls.Add($objCalendar)

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

新功能 Do-Work定义在 Add_KeyDown 范围之外,但由于我们在那里调用它,它将在您的 Enter 按键上执行。

关于winforms - PowerShell 日历弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22208751/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com