Powershell-函数调用在add_click方法中不起作用.请提供建议



我试图在点击事件中调用函数OKButtonEvent,但每当我点击OK按钮时,都不会发生任何事情。我已经创建了复选框,在选中的复选框上,我调用了不同的powershell脚本,但似乎不起作用。我尝试过两种不同的方法,但都无效。一个问题是,如果我确实选择了3个复选框,那么在OK按钮的点击事件中,基于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

所以我为工作中的扫描仪制作了这个应用程序。它读取条形码并列出它们。然后当你想把标签打印出来的时候。。。。应用程序通过自动执行来帮助我们,。。。

试试看…在里面放一些数字、单词或字母。。。



#$aNewSheet = [autoPrintForm]::new() 
$aNewSheet = [autoPrintForm]::new(185)

或者看看这个。。。

$this.autoButton is of a type
[object]$autoButton in a powershell class...think
$this.autoButtonX as
[int]$autoButtonX or as
$this.SautoButtonX
is the X size while
$this.PautoButtonX
is the X point on the form. 

在你的班级

class someFormWithBoxes
{
[object]$OformP
[object]$autoButton
[int]$SautoButtonX
[int]$SautoButtonY
[int]$PautoButtonX
[int]$PautoButtonY
#.....keep going
#you can list as many things as you want...any object in c# 
#usually have some functionality in powershell code as types

[void]startForm()
{...#create a form}
[void]addAutoButton()
{



$this.autoButton = New-Object System.Windows.Forms.Button
$this.autoButton.Location = New-Object System.Drawing.Point($this.autoButtonX,$this.autoButtonY)
$this.autoButton.Size = New-Object System.Drawing.Size($this.RbuttonSizeX,$this.RbuttonSizeY)
$this.autoButton.Text = 'AUTO'
$this.autoButton.backcolor = "indianred"
$this.formP.Controls.Add($this.autoButton)

$thisAUTOBUTTON = $this 
$this.autoButton.add_click({
$thisAUTOBUTTON.autoClick()
}.GetNewClosure())
}
autoclick()
{#your code here referencing the objects defined in the class}
}

#期末

并使用类似的工作流来控制整个表单及其属性变量。由于您使用的是复选框,因此您应该查找这些复选框并遵循类似的工作流程。检查此页面左栏上的构造函数、属性、方法和事件,以便更深入地了解操作系统使用的项和对象。

最新更新