Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Hi Forum,
because i use quite a lot of buttons in a script, i thought about not having to type the code for every single button, but to outsource this as a function.
I want to have different output on the screen depending on which button is pressed, before the window is closed. Now this works fine as long as I only include ONE button in the script.
If I want to have TWO or more buttons with different outputs/commands on the "Add_click"-event, I always overwrite this variable with the last entered value of the variable $Add_Click
I cannot figure, how to pass the command for the "$Add_click"-event for each button to the function?
thanks for your help
Greetings
newbi
Just create a click event and add it to all buttons. You don't have to use separate scripts for each button.
You can tell which button called the event by inspecting the $this object. It is the button object that generated the event.
There is no need for a function. Event code is a function called formally an "anonymous function" in compiled system. In PowerShell it is just a script block
You should never give a function a name that is the same as a Net Framework "type".
To auto-create controls you can do it in a loop but alter the name of the control being created and its location. Use the control objects method <control>.AddClick($click_script_block)
You can also pass a script block as a function argument and use the "add" method to add the script block to the control click event.
This topic is 2 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.