Author Posts

October 17, 2013 at 1:06 pm

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?

Thanks

October 17, 2013 at 2:09 pm

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

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

October 17, 2013 at 4:21 pm

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

October 17, 2013 at 4:24 pm

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.

October 17, 2013 at 5:05 pm

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):

`
$Button.Add_click({
    If ([int]$Script:counter -eq 0) {
        Write-Verbose $counter
        $SomeScript
        $Script:counter++
    } Else {
        Write-Verbose $counter
        $SomeOtherScript
        $Script:counter--
    }
})
`

October 18, 2013 at 3:14 am

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 = {
blah-blah-blah
}
$Add-User = {
blah-blah-blah
}
$Remove-User ={
blah-blah-blah
}

Now I create a button and add the following:

$Button.Add_Click($Create-Group)

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:

$Button.Set_Click($Add-User)

or

$Button.Clear_Click()
$Button.Add_Click($Add-User)

October 18, 2013 at 7:46 am

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.

October 18, 2013 at 9:51 am

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?

October 18, 2013 at 10:25 am

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.