Powershell - 函数调用在 add_click 方法中不起作用。请指教

问题描述 投票:0回答:1

我试图在单击事件中调用函数 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
function powershell button method-call
1个回答
-1
投票

所以我为工作中的扫描仪制作了这个应用程序。它读取条形码并列出它们的列表。然后,当您想要打印代码的标签时,您可以将代码粘贴到带有输入框的 html 页面中。 (至少我是这样的……) 这个应用程序通过自动执行此操作来帮助我们,因此我不必在厨房里走来走去进行单次扫描。我只需将它们一次全部扫描到我的文本框中,然后点击打印并瞄准。

尝试一下...将一些数字或单词或字母放在那里,用选项卡分隔,就像扫描仪自动放置的那样。


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

或者看看这个...

$this.autoButton is of a type [object] in a powershell class
think $this.autoButtonX ... or rather $this.SautoButtonX is the X size while $this.PautoButtonX is the X point on the 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())

}

并使用类似的工作流程来控制整个表单及其属性变量。

© www.soinside.com 2019 - 2024. All rights reserved.