Changing a Click Event

This topic contains 8 replies, has 3 voices, and was last updated by  Don Jones 4 years ago.

  • Author
    Posts
  • #10876

    graybin
    Participant

    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

  • #10878

    Don Jones
    Keymaster

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

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

  • #10879

    graybin
    Participant

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

  • #10880

    Don Jones
    Keymaster

    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.

  • #10881

    Boe Prox
    Participant

    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--
        }
    })
    `
  • #10884

    graybin
    Participant

    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)
  • #10893

    Boe Prox
    Participant

    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.

  • #10897

    graybin
    Participant

    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?

  • #10898

    Don Jones
    Keymaster

    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.

You must be logged in to reply to this topic.