Powershell - 函数调用在 add_click 方法中不起作用。请指教
我试图在单击事件中调用函数 OKButtonEvent 但每当我单击“确定”按钮时,什么也没有发生。我已经创建了复选框,并在选定的复选框上调用了不同的 powershell 脚本,但似乎不起作用。我尝试过不同的 -2 方法,但没有任何效果。有一个问题,如果我确实选择了 3 个复选框,那么在“确定”按钮的单击事件期间,基于 3 个选定复选框的所有条件都将执行?
下面是我的脚本:
function Test-AnyButtonChecked
{
if ($checkbox1.Checked -or $checkbox2.Checked -or $checkbox3.Checked -or $checkbox4.Checked)
{
$OKButton.Enabled = $true
}
else
{
$OKButton.Enabled = $false
}
}
function OKButtonEvent
{
if ($checkbox1.Checked)
{
echo "hi CB1"
& ((Split-Path $MyInvocation.InvocationName)+"\abc.ps1")
}
if ($checkbox2.Checked)
{
write-host "hi CB2"
& ((Split-Path $MyInvocation.InvocationName)+"\bcd.ps1")
}
if ($checkbox3.Checked)
{
& ((Split-Path $MyInvocation.InvocationName)+"\cde.ps1")
}
if ($checkbox4.Checked)
{
& ((Split-Path $MyInvocation.InvocationName)+"\def.ps1")
}
}
function checkbox_test
{
[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 = 1000
$Form.height = 700
$Form.Text = ”ABC Scan”
# 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 your checkbox
$checkbox1 = new-object System.Windows.Forms.checkbox
$checkbox1.Location = new-object System.Drawing.Size(60,30)
$checkbox1.Size = new-object System.Drawing.Size(250,50)
$checkbox1.Text = "OS Scan"
$checkbox1.Checked = $true
$Form.Controls.Add($checkbox1)
# create your checkbox
$checkbox2 = new-object System.Windows.Forms.checkbox
$checkbox2.Location = new-object System.Drawing.Size(60,90)
$checkbox2.Size = new-object System.Drawing.Size(250,50)
$checkbox2.Text = "ARP Scan - Publisher"
$checkbox2.Checked = $true
$Form.Controls.Add($checkbox2)
# create your checkbox
$checkbox3 = new-object System.Windows.Forms.checkbox
$checkbox3.Location = new-object System.Drawing.Size(60,150)
$checkbox3.Size = new-object System.Drawing.Size(350,50)
$checkbox3.Text = "ARP Scan - Display Name"
$checkbox3.Checked = $true
$Form.Controls.Add($checkbox3)
# create your checkbox
$checkbox4 = new-object System.Windows.Forms.checkbox
$checkbox4.Location = new-object System.Drawing.Size(60,210)
$checkbox4.Size = new-object System.Drawing.Size(250,50)
$checkbox4.Text = "File Scan GCI"
$checkbox4.Checked = $true
$Form.Controls.Add($checkbox4)
# Add an OK button
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = new-object System.Drawing.Size(400,550)
$OKButton.Size = new-object System.Drawing.Size(100,40)
$OKButton.Text = "OK"
$OKButton.add_Click({$button_click_1})
$form.Controls.Add($OKButton)
#$form.AcceptButton = $OKButton
#Add a cancel button
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Location = new-object System.Drawing.Size(600,550)
$CancelButton.Size = new-object System.Drawing.Size(100,40)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$Form.Close()})
$form.Controls.Add($CancelButton)
########### This is the important piece ##############
# #
# Do something when the state of the checkbox changes #
#######################################################
$checkbox1.add_CheckedChanged( { Test-AnyButtonChecked })
$checkbox2.add_CheckedChanged( { Test-AnyButtonChecked })
$checkbox3.add_CheckedChanged( { Test-AnyButtonChecked })
$checkbox4.add_CheckedChanged( { Test-AnyButtonChecked })
$button_click_1={OKButtonEvent}
# Activate the form
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
}
#Call the function
checkbox_test
I am trying to call function OKButtonEvent inside the on click event but whenever I am clicking on OK button nothing happens. I have created checkboxes and on selected checkboxes, I am calling different-different powershell scripts but seems not working. I have tried different -2 things but nothing works. One question if I do select 3 checkboxes then during the on click event of OK button all conditions based on 3 selected checkboxes will execute?
Below is my script:
function Test-AnyButtonChecked
{
if ($checkbox1.Checked -or $checkbox2.Checked -or $checkbox3.Checked -or $checkbox4.Checked)
{
$OKButton.Enabled = $true
}
else
{
$OKButton.Enabled = $false
}
}
function OKButtonEvent
{
if ($checkbox1.Checked)
{
echo "hi CB1"
& ((Split-Path $MyInvocation.InvocationName)+"\abc.ps1")
}
if ($checkbox2.Checked)
{
write-host "hi CB2"
& ((Split-Path $MyInvocation.InvocationName)+"\bcd.ps1")
}
if ($checkbox3.Checked)
{
& ((Split-Path $MyInvocation.InvocationName)+"\cde.ps1")
}
if ($checkbox4.Checked)
{
& ((Split-Path $MyInvocation.InvocationName)+"\def.ps1")
}
}
function checkbox_test
{
[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 = 1000
$Form.height = 700
$Form.Text = ”ABC Scan”
# 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 your checkbox
$checkbox1 = new-object System.Windows.Forms.checkbox
$checkbox1.Location = new-object System.Drawing.Size(60,30)
$checkbox1.Size = new-object System.Drawing.Size(250,50)
$checkbox1.Text = "OS Scan"
$checkbox1.Checked = $true
$Form.Controls.Add($checkbox1)
# create your checkbox
$checkbox2 = new-object System.Windows.Forms.checkbox
$checkbox2.Location = new-object System.Drawing.Size(60,90)
$checkbox2.Size = new-object System.Drawing.Size(250,50)
$checkbox2.Text = "ARP Scan - Publisher"
$checkbox2.Checked = $true
$Form.Controls.Add($checkbox2)
# create your checkbox
$checkbox3 = new-object System.Windows.Forms.checkbox
$checkbox3.Location = new-object System.Drawing.Size(60,150)
$checkbox3.Size = new-object System.Drawing.Size(350,50)
$checkbox3.Text = "ARP Scan - Display Name"
$checkbox3.Checked = $true
$Form.Controls.Add($checkbox3)
# create your checkbox
$checkbox4 = new-object System.Windows.Forms.checkbox
$checkbox4.Location = new-object System.Drawing.Size(60,210)
$checkbox4.Size = new-object System.Drawing.Size(250,50)
$checkbox4.Text = "File Scan GCI"
$checkbox4.Checked = $true
$Form.Controls.Add($checkbox4)
# Add an OK button
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = new-object System.Drawing.Size(400,550)
$OKButton.Size = new-object System.Drawing.Size(100,40)
$OKButton.Text = "OK"
$OKButton.add_Click({$button_click_1})
$form.Controls.Add($OKButton)
#$form.AcceptButton = $OKButton
#Add a cancel button
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Location = new-object System.Drawing.Size(600,550)
$CancelButton.Size = new-object System.Drawing.Size(100,40)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$Form.Close()})
$form.Controls.Add($CancelButton)
########### This is the important piece ##############
# #
# Do something when the state of the checkbox changes #
#######################################################
$checkbox1.add_CheckedChanged( { Test-AnyButtonChecked })
$checkbox2.add_CheckedChanged( { Test-AnyButtonChecked })
$checkbox3.add_CheckedChanged( { Test-AnyButtonChecked })
$checkbox4.add_CheckedChanged( { Test-AnyButtonChecked })
$button_click_1={OKButtonEvent}
# Activate the form
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
}
#Call the function
checkbox_test
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以我为工作中的扫描仪制作了这个应用程序。它读取条形码并列出它们的列表。然后,当您想要打印标签时...应用程序可以自动帮助我们完成此操作,...
尝试一下...在其中输入一些数字、单词或字母...
或者只是看看这个...
在你的类中
#end of class
并使用类似的工作流程来控制整个表单及其属性变量。
由于您正在使用复选框,因此您应该只查找这些复选框并遵循类似的工作流程。 检查构造函数、属性、本页左侧栏上的方法和事件,以便更深入地了解操作系统使用的项目和对象。
So I made this app for a scanner at work. It reads barcodes and makes a list of them. And then when you want the labels printed out....app helps us out by doing that automatically,...
Try it... put some numbers or words or letters in there...
Or just look at this...
in your class
#end of class
And use a similar workflow to control the entire form and its property variables.
Since you are working with checkboxes you should just look those up and follow a similar workflow. Examine the Constructors, Properties, Methods, and Events on the left bar on this page in order to get a deeper grip of the items and objects used by your operating system.