Question: I'm using a powershell script that also draw a basic interface to simplify some task.
I need to bind a specific click event to a button element, and I'm using this code

```
$button_makeAction.add_Click({
    # my code
})

```

The problem is that, during script execution, I want to bind that button to a different click event. Using the same code it appends another click event and execute both, so I need something like `$button_makeAction.reset_Click` or `$button_makeAction.set_Click` in order to remove the first event code. Is this the correct way?

Answer: 
Use the method `remove_Click` (`remove_MyEvent` in the example below) and a script block stored in a variable. Here is the example:

```
# A static class with a sample event
Add-Type @'
using System;
public static class TestEvents {
    public static event EventHandler MyEvent;
    public static void InvokeEvent()
    {
        if (MyEvent != null) {
            MyEvent(null, null);
        }
    }
}
'@

# Script used as event handler, it prints "In event handler."
$script = {
    Write-Host "In event handler."
}

# Add the script handler
[TestEvents]::add_MyEvent($script)

# Test: it writes "In event handler."
[TestEvents]::InvokeEvent()

# Remove the script handler
[TestEvents]::remove_MyEvent($script)

# Test: it does not write write "In event handler." because the handler has been removed
[TestEvents]::InvokeEvent()

```

Note that in PS V2 CTP3 there was an issue [713174](https://connect.microsoft.com/PowerShell/feedback/details/713174/v3-ctp2-regression-or-breaking-change-script-block-handlers-are-not-removed-from-events-ditto-with-delegates)
but it seems to be resolved, so that casting to `[EventHandler]` is not even needed. But keep this trick in mind, too.
How about adding an IF statement to the event handler and executing code based on a condition?