考虑以下代码:
$Form = New-Object System.Windows.Forms.Form
$YesButton = New-Object System.Windows.Forms.Button
$YesButton.Text = "Yes"
$YesButton.Location = New-Object System.Drawing.Point( `
(($Form.Width / 2) - ($YesButton.Width)), 25)
$YesButton.Add_Click({$Form.DialogResult=[System.Windows.Forms.DialogResult]::OK})
$NoButton = New-Object System.Windows.Forms.Button
$NoButton.Text = "No"
$NoButton.Location = New-Object System.Drawing.Point( `
($YesButton.Location.X + $YesButton.Width + 10), $YesButton.Location.Y)
$NoButton.Add_Click({$Form.DialogResult=[System.Windows.Forms.DialogResult]::Cancel})
$Form.Controls.Add($YesButton)
$Form.Controls.Add($NoButton)
$Timer = new-object System.Windows.Forms.Timer
$Timer.Add_Tick(
{
$Form.DialogResult = [System.Windows.Forms.DialogResult]::None
$Form.Close()
})
$Timer.Interval = 3000
$Timer.start()
$Result = $Form.ShowDialog()
if ($Result -eq "OK")
{
Write-Output "User selected 'Yes'."
}
elseif ($Result -eq "Cancel")
{
Write-Output "User selected 'No'."
}
elseif ($Result -eq "None")
{
Write-Output "User selected nothing; prompt timed out."
}
如果我运行它并让计时器关闭表单,我会期望它点击elseif ($Result -eq "None")
然后打印User selected nothing; prompt timed out.
,而不是它'点击elseif ($Result -eq "Cancel")
并打印User selected 'No'.
为什么会这样做,如何让它返回我想要的对话结果?
答案 0 :(得分:3)
如果计时器关闭表单,DialogResult将更改为None,但是之后立即调用$ Form.close()。
由于Close()方法将DialogResult更改为Cancel,因此将Close()方法切换到手动更改DialogResult的位置并将新的枚举值更改为Abort将使条件按预期工作。
答案 1 :(得分:0)
如果这就是你想要的一切,我会保持简单,并考虑使用旧时尚WScript.Shell Popup
:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link active" href="#">Item1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Item2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Item3</a>
</li>
</ul>