Winforms 如何在powershell中添加计时器以在一定时间后退出表单
我在PowerShell中创建了一个表单(仍在进行中),该表单具有单选按钮,我希望在屏幕上显示一段时间,如果该时间已过,它将发送电子邮件并关闭表单。我不清楚如何将计时器添加到表单中,剩下的我可以解决Winforms 如何在powershell中添加计时器以在一定时间后退出表单,winforms,powershell,timer,Winforms,Powershell,Timer,我在PowerShell中创建了一个表单(仍在进行中),该表单具有单选按钮,我希望在屏幕上显示一段时间,如果该时间已过,它将发送电子邮件并关闭表单。我不清楚如何将计时器添加到表单中,剩下的我可以解决 $RadioButtonList = @( "Tom", "Dick", "Harry", "John", "Jane" ) $RadioButtonYMargin = 10 $RadioButtonIndex = 0 $RadioButtonX = 20 $RadioButtonY = (10
$RadioButtonList = @( "Tom", "Dick", "Harry", "John", "Jane" )
$RadioButtonYMargin = 10
$RadioButtonIndex = 0
$RadioButtonX = 20
$RadioButtonY = (10 + $RadioButtonYMargin)
$RadioButtonYOffset = 30
$RadioButtonWidth = 350
$RadioButtonHeight = 20
$GroupBoxXMargin = 7
$GroupBoxX = 20
$GroupBoxY = 30
$GroupBoxWidth = 400
$GroupBoxHeight = $RadioButtonY + ( $RadioButtonList.Count * ( $RadioButtonHeight + 9 )) + $RadioButtonYMargin
$ButtonYMargin = 50
$ButtonY = $GroupBoxY + $GroupBoxHeight + $ButtonYMargin
$ButtonWidth = 100
$ButtonHeight = 40
$FormWidth = $GroupBoxWidth + (($GroupBoxX + $GroupBoxXMargin) * 2)
$FormHeight = $GroupBoxY + $GroupBoxHeight + $ButtonHeight + ($ButtonYMargin * 2)
$ButtonXSpacing = 50
$ButtonXMargin = [Int](($FormWidth - (($ButtonWidth * 2) + $ButtonXSpacing)) / 2)
Function RadioButtonClick ( $RadioButtonSelected ) {
$Form.AcceptButton.Enabled = $True
}
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
# Set the size of your form
$Form = New-Object System.Windows.Forms.Form
$Form.Width = $FormWidth
$Form.Height = $FormHeight
$Form.Text = "Operator, acknowledge your presence"
# Set the font of the text to be used within the form
$Font = New-Object System.Drawing.Font("Times New Roman",12)
$Form.Font = $Font
# Create a group that will contain your radio buttons
$GroupBox = New-Object System.Windows.Forms.GroupBox
$GroupBox.Location = New-Object System.Drawing.Size( $GroupBoxX, $GroupBoxY )
$GroupBox.size = New-Object System.Drawing.Size( $GroupBoxWidth, $GroupBoxHeight )
$GroupBox.text = "Please select your name below:"
While ( $RadioButtonIndex -lt $RadioButtonList.Count ) {
$RadioButton = New-Object System.Windows.Forms.RadioButton
$RadioButton.Location = New-Object System.Drawing.Size( $RadioButtonX, $RadioButtonY )
$RadioButton.Size = New-Object System.Drawing.Size( $RadioButtonWidth, $RadioButtonHeight )
$RadioButton.Checked = $False
$RadioButton.Text = $RadioButtonList[ $RadioButtonIndex ]
$RadioButtonY += $RadioButtonYOffset
$RadioButton.Add_Click({ RadioButtonClick $This.Text })
$GroupBox.Controls.Add( $RadioButton )
$RadioButtonIndex += 1
}
# Add an OK button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size( $ButtonXMargin, $ButtonY )
$OKButton.Size = New-Object System.Drawing.Size( $ButtonWidth, $ButtonHeight )
$OKButton.Text = 'OK'
$OKButton.Enabled = $False
$OKButton.DialogResult=[System.Windows.Forms.DialogResult]::OK
# Add a cancel button
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size( ($ButtonXMargin + $ButtonWidth + $ButtonXSpacing), $ButtonY )
$CancelButton.Size = New-Object System.Drawing.Size( $ButtonWidth, $ButtonHeight )
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::Cancel
# Add all the Form controls on one line
$Form.Controls.AddRange(@($GroupBox,$OKButton,$CancelButton))
# Assign the Accept and Cancel options in the form to the corresponding buttons
$Form.AcceptButton = $OKButton
$Form.CancelButton = $CancelButton
# Activate the form
$Form.Add_Shown({$Form.Activate()})
# Get the results from the button click
$dialogResult = $Form.ShowDialog()
# If the OK button is selected
if ($dialogResult -eq "OK") {
$SelectedRadioButton = ($GroupBox.Controls | Where-Object{$_.Checked}).Text
Write-Host "Selection was $SelectedRadioButton"
} Else {
Write-Host "Cancelled"
}
下面是一个简单的示例,演示如何使用计时器在2秒钟后自动关闭WinForms窗体(PSv5+语法): 请注意需要
$form.Dispose()
,因为表单显示为带有.ShowDialog()
(而不是.Show()
)的模式对话框,在这种情况下,关闭它不会隐式地处理它-有关详细信息,请参阅
PSv4-语法(其中使用命名空间的
和::new()
不可用):
下面是一个简单的示例,演示如何使用计时器在2秒钟后自动关闭WinForms窗体(PSv5+语法):
请注意需要$form.Dispose()
,因为表单显示为带有.ShowDialog()
(而不是.Show()
)的模式对话框,在这种情况下,关闭它不会隐式地处理它-有关详细信息,请参阅
PSv4-语法(其中使用命名空间的和::new()
不可用):
我修改了答案,以更改调用$timer.Dispose()
的位置,从而确保即使在用户关闭窗体时,也会在计时器启动之前释放计时器。我修改了答案,以更改调用$timer.Dispose()
的位置,从而确保即使在用户关闭窗体时也会释放计时器,在计时器启动之前。
using namespace System.Windows.Forms
Add-Type -AssemblyName System.Windows.Forms
# Create the form.
$form = [Form]::new()
# Create a timer.
$timer = [Timer]::new()
$timer.Interval = 2000 # 2 seconds
# Set up the event handler for the timer.
$timer.Add_Tick({
# Close the form.
# Note: We dispose (stop) the timer later, right after the form closes;
# with a very short interval, another tick event could fire before
# that happens, but calling .Close() on an already closed form is fine.
$form.Close()
})
# Start the timer.
$timer.Start()
# Show the dialog, which will automatically close
# when the timer fires.
$result = $form.ShowDialog()
# Dispose (stop) the timer. Doing this here instead of in the tick
# event handler ensures that the timer is stopped even when the form
# was closed by the user.
$timer.Dispose()
# Dispose the form and its controls. Skip, if you want to redisplay the form later.
$form.Dispose()
Add-Type -AssemblyName System.Windows.Forms
# Create the form.
$form = New-Object System.Windows.Forms.Form
# Create a timer.
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 2000 # 2 seconds
# Set up the event handler for the timer.
$timer.Add_Tick({
# Close the form.
# Note: We dispose (stop) the timer later, right after the form closes;
# with a very short interval, another tick event could fire before
# that happens, but calling .Close() on an already closed form is fine.
$form.Close()
})
# Start the timer.
$timer.Start()
# Show the dialog, which will automatically close
# when the timer fires.
$result = $form.ShowDialog()
# Dispose (stop) the timer. Doing this here instead of in the tick
# event handler ensures that the timer is stopped even when the form
# was closed by the user.
$timer.Dispose()
# Dispose the form and its controls. Skip, if you want to redisplay the form later.
$form.Dispose()