rad_
rad_

Reputation: 301

add_Click does not execute code within block

When trying to execute code or update values within a variable inside of the add_Click block, it does not work. I'm not sure if I am doing something incorrectly. Moreover, the statement I have following the code sees the variable as nonexistent.

$Button1.add_Click({$authEvent = $true
    $Form.Close() })
$Button2.add_Click({$authEvent = $false
    $Form.Close()
                  })

This should update the $authEvent variable depending on which button is clicked. As well as closing the form.

Upvotes: 0

Views: 246

Answers (2)

Mark Harwood
Mark Harwood

Reputation: 2415

Check where your $authEvent variable is coming from and assign it as a global variable if needed ($Global:authEvent)

Note that you only need to use the $Global: option when first declaring the variable. So it should be used like this:

$global:authEvent = $null

$Button1.add_Click({
    $authEvent = $true
    $Form.Close() 
})
$Button2.add_Click({
    $authEvent = $false
    $Form.Close()
})

Upvotes: 2

rad_
rad_

Reputation: 301

Thanks to I.T Delinquent I've figured out this issue. It appears that the block actually does require a global variable(not sure why it does.) Therefore, I declared the global var at the beginning of the script and from there we had success.

$global:authEvent = $null

$Button1.add_Click({$global:authEvent = $true
$Form.Close() })
$Button2.add_Click({$global:authEvent = $false
$Form.Close()
})

Upvotes: 1

Related Questions