'Powershell- Function call is not working in the add_click method. Please advice

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


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source