0

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.

2
  • 1
    Where is the $authEvent variable coming from? Does it need to be a global variable? Are you getting any errors? Commented Jun 12, 2019 at 15:29
  • No errors. It's coming from within the function that this code is in, so it shouldn't need to be a global variable should it? I tested the variable with an if statement and it looks like the block is not updating the variables. It's fulfilling the $Form.Close(), but not the variables. I'll try it with global vars and let you know. Commented Jun 12, 2019 at 15:32

2 Answers 2

2

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()
})
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.