【发布时间】:2019-08-27 23:58:45
【问题描述】:
我在 PowerShell 中创建了一个带有单选按钮的表单(仍在进行中),我想在屏幕上显示一段时间,如果该时间已过,它将发送一封电子邮件并关闭形式。我不清楚如何将计时器添加到表单中,其余的我可以弄清楚。
$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"
}
【问题讨论】:
-
我已经修改了答案以更改调用
$timer.Dispose()的位置,以确保即使在 user_close 表单时,在计时器触发之前也已释放计时器。
标签: winforms powershell timer