PowerShell日历弹出窗口

时间:2014-03-05 20:41:36

标签: winforms powershell

我正在尝试使用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"
    }

3 个答案:

答案 0 :(得分:2)

$dtmDate在if语句的上下文中没有值,因为该代码未在您的回车键上执行... Add_KeyDown上下文中的块是。尝试执行类似的操作:

[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按键上执行。

答案 1 :(得分:0)

你必须添加:

New-Variable dtmDate -Option AllScope

答案 2 :(得分:0)

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

New-Variable dtmDate -Option AllScope
$dtmDate = $null

$objForm = New-Object Windows.Forms.Form 
$objForm.Text = "Select a Date" 
$objForm.Size = New-Object Drawing.Size @(190,190) 
$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" -ForegroundColor Green}

该行" New-Variable dtmDate -Option AllScope"允许变量被带到表单范围之外,因此您可以在脚本中进一步使用它。

我省略了为写主机创建的功能。如果要以正确的方式执行此操作,请创建整个脚本块的功能。 上面的脚本将以您希望的方式工作。它为我工作!

相关问题