[英]How to return specifi error level by clicking a button in a form of GUI in Powershell?
I am going to use GUI with a button click to return an error level with a specific number. 我将通过单击按钮使用GUI来返回带有特定数字的错误级别。 It returns the error level value just for $Auto_Button, and the GUI disappears when the script executed.
它仅返回$ Auto_Button的错误级别值,并且在执行脚本时GUI消失。
Add-Type -AssemblyName System.Windows.Forms $Font = New-Object System.Drawing.Font("Times New Roman",13) Add-Type -AssemblyName System.Windows.Forms $ Font =新对象System.Drawing.Font(“ Times New Roman”,13)
$MainForm = New-Object System.Windows.Forms.Form
$MainForm.Text = "Process"
$MainForm.Width = 500
$MainForm.Height = 200
$MainForm.StartPosition = "CenterScreen"
$MainForm.BackColor = "#e2e2e2"
$Title = New-Object System.Windows.Forms.Label
$Title.Font = $Font
$Title.Text = "Which process do you want to choose?"
$Title.Location = New-Object System.Drawing.Size(100,30)
$Title.Size = New-Object System.Drawing.Size(500,30)
$MainForm.Controls.Add($Title)
$Auto_Button = {$MainForm.Close()}
Exit 10
$Manual_Button = $MainForm.Close()}
Exit 30
$Automatic = New-Object System.Windows.Forms.Button
$Automatic.Location = New-Object System.Drawing.Size(110,80)
$Automatic.Size = New-Object System.Drawing.Size(100,30)
$Automatic.Text = "Automatically"
$Automatic.Font = 'Microsoft Sans Serif,10'
$Automatic.BackColor = "#e47104"
$Automatic.Add_Click($Auto_Button)
$MainForm.Controls.Add($Automatic)
$Manual = New-Object System.Windows.Forms.Button
$Manual.Location = New-Object System.Drawing.Size(270,80)
$Manual.Size = New-Object System.Drawing.Size(100,30)
$Manual.Text = "Manually"
$Manual.Font = 'Microsoft Sans Serif,10'
$Manual.BackColor = "#e47104"
$Manual.Add_Click($Manual_Button)
$MainForm.Controls.Add($Manual)
$MainForm.ShowDialog()
Use a global variable for storing a result. 使用全局变量存储结果。
$Auto_Button = ({ $global:result=10
$MainForm.Close() })
$Manual_Button = ({ $global:result=20
$MainForm.Close() })
...
$result=0
$MainForm.ShowDialog()
$result | Out-File .\exit.txt
exit $result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.