Changing a Click Event

In Powershell, I have created a button and added a click event with $Button.Add_click($SomeScript)

I would like to be able to change what script is run. Something like $Button.Edit_click($SomeOtherScript)

If I do another Add_Click, it runs both scripts (which gives some interesting possibilities, but not what I want in this case). I am sure I could just update the “$SomeScript”, but that is not what I am looking for.

I can only find the Add_Event. Is there something to Clear_Event or Update_Event?


You want to change what the button does while the script is running?

Also, are you using something like PowerShell Studio to do this?

Yes, I want to change what the button does while running but I’m not using anything special to write the script

Could you post a brief code sample? It’d be helpful to see where you’re setting it up, so I can give a better answer.

It almost sounds like you want Script1 to run after the first click and then on the second click, run Script2, then revert back to Script1 and so on and so forth? Is this accurate?

Something like this should work (at least I recall it working for me in the past):

    If ([int]$Script:counter -eq 0) {
        Write-Verbose $counter
    } Else {
        Write-Verbose $counter

I understand that I can do it with some sort of switch or if-then statement, but, and maybe this is just academic, I want to know if you can change what the click does. Lets say I have the following functions:

$Create-Group = {
$Add-User = {
$Remove-User ={
Now I create a button and add the following:

Once that group is created, I want to change the Click of the button to run $Add-User. If the user is added, then I want to change the button to $Remove-User, etc. Is there a way to update what the click of the button does? something like:

The closest thing that I have been able to do so far based on what you are saying is by using multiple RoutedEvent handlers and swapping them out with each click.

$Script:e = [System.Windows.RoutedEventHandler]{ $Window.Background = 'Black' $This.Remove_Click($e) $This.Add_Click($d) } $Script:d = [System.Windows.RoutedEventHandler]{ $Window.Background = 'Red' $This.Remove_Click($d) $This.Add_Click($e) } $button.Add_Click($e)

Let me know if this is getting closer to what you are looking for.

Yes, this “Remove_Click” is what I was looking for. That allows me to remove what is associated, and add a new one. Are there any others like the Add_ and the Remove_? Any idea where I can find documentation on it?

In terms of documentation, you’re not really working with PowerShell per se, you’re working with Windows Forms. So the documentation would be on Microsoft’s MSDN Web Site, under the System.Windows.Forms .NET Framework classes.

Trick is, some of what you’re doing here is “under the hood” stuff. Normally, a Visual Studio developer wouldn’t deal with all that stuff, because Visual Studio sort of generates that code for you. Because you’re going more “raw” (and because VS won’t generate PowerShell-flavored code), you’re dealing with something that most developers never bother with.