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

发布于 2025-01-11 18:26:16 字数 4104 浏览 0 评论 0原文

我试图在单击事件中调用函数 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

近箐 2025-01-18 18:26:17

所以我为工作中的扫描仪制作了这个应用程序。它读取条形码并列出它们的列表。然后,当您想要打印标签时...应用程序可以自动帮助我们完成此操作,...

尝试一下...在其中输入一些数字、单词或字母...


     
     #$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}
}

#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...


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

Or just look at this...

$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. 

in your class

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}
}

#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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文