我正在使用 powershell 开发一个带有图形界面的小工具,我对这个问题感到疯狂......
例如:
我在表单上有一个标签,显示 ping 的“实时”状态。
同时,如果单击标签,则会显示一条弹出消息。
function GoogleStatus ($Label)
{
$Google = "www.google.com"
If (Test-Connection -ComputerName $Google -Count 1 -Quiet)
{Label.Text = "Yes"}
else
{Label.Text = "No"}
}
function HelloMsg
{
[System.Windows.Forms.MessageBox]::Show("Hello","Funny Window",0,32)
}
[void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#Forms
$Form = New-Object System.Windows.Forms.Form
$Form.Size = '150, 220'
$Label = New-Object System.Windows.Forms.Label
$Form.Controls.Add($Label)
$Label.Location = '10, 30'
$Label.Size = '75, 20'
$Label.Add_Click({HelloMsg})
#Timer Init
$Timer = New-Object 'System.Windows.Forms.Timer'
$Timer.Interval = 3000
Timer.Add_Tick({GoogleStatus $Label})
$Timer.Enabled = $True
#Show Interface
$Form.ShowDialog()
在计时器的前 3 秒内,单击标签会正确显示弹出消息。但如果我等待超过 3 秒,点击标签就没有任何效果。
请帮助 :(
我已经删除了代码中的 3 个拼写错误,它似乎可以工作(3 秒后我仍然收到弹出窗口):
function GoogleStatus ($Label) {
$Google = "www.google.com"
if(Test-Connection -ComputerName $Google -Count 1 -Quiet) {
$Label.Text = "Yes" # missing $ sign here
} else {
$Label.Text = "No" # missing $ sign here
}
}
function HelloMsg {
[System.Windows.Forms.MessageBox]::Show("Hello","Funny Window",0,32)
}
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#Forms
$Form = New-Object System.Windows.Forms.Form
$Form.Size = '150, 220'
$Label = New-Object System.Windows.Forms.Label
$Form.Controls.Add($Label)
$Label.Location = '10, 30'
$Label.Size = '75, 20'
$Label.Add_Click({HelloMsg})
#Timer Init
$Timer = New-Object 'System.Windows.Forms.Timer'
$Timer.Interval = 3000
$Timer.Add_Tick({ GoogleStatus $Label }) # missing $ sign here
$Timer.Enabled = $True
#Show Interface
$Form.ShowDialog()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)