function button()

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.
Locked
newbi2009
Posts: 6
Last visit: Fri Mar 18, 2022 2:21 am

function button()

Post by newbi2009 »

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

Here is the code I have so far:

  1. function Button{
  2.   param(
  3.     [int]$x,
  4.         [int]$y,
  5.     [string]$Text
  6.       #[string]$Add_Click    #not used, since I connot find a way to get this passed into the function
  7.       )
  8.  
  9. $Button = New-Object System.Windows.Forms.Button    
  10. $Button.Location = New-Object System.Drawing.Size($x,$y)
  11. $Button.Size = New-Object System.Drawing.Size(100,66)
  12. $Button.Text = $Text
  13. $Button.Add_Click({ .$Add_click
  14.                                 $obfForm.close()
  15.                               })
  16. $obfForm.Controls.Add($Button)
  17. }
  18.  
  19. #----------------------- window beginns--------------------------------------
  20.  
  21. $myWindow = New-Object System.Windows.Forms.Form                                        
  22. $myWindow.Size = New-Object System.Drawing.Size(420,230)
  23. $myWindow.Text = "my window"
  24.  
  25. #----------------------- close button 1--------------------------------------
  26.  
  27. $obfForm=$myWindow
  28.  
  29. Button 1 10 "cancel-A"
  30. $Add_Click={
  31.                    write-host "Function >A< ended" ;
  32.                    write-host "******************************************************"
  33.                   }
  34. #----------------------- close button 2--------------------------------------
  35.  
  36.  
  37. Button 200 10 "cancel-B"
  38. $Add_Click={
  39.                    write-host "another ouput should be written, when clicking Button-B";
  40.                    write-host "******************************************************"
  41.                   }  
  42.  
  43.  
  44. [void] $myWindow.ShowDialog()
  45.  
by jvierra » Fri Mar 18, 2022 1:42 am
Here is a simple hint at how this works.

Code: Select all

function Create-Button{
Param(
[string]$Name,
[string]$text,
[int]$x,
[int]$y,
[scriptblock]$AddClick
)

$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size($x, $y)
$Button.Size = New-Object System.Drawing.Size(100, 66)
$Button.Text = $Text
$Button.Add_Click($AddClick)
$Form1.Controls.Add($Button)
}

$Form1 = New-Object System.Windows.Forms.Form
$Form1.Size = New-Object System.Drawing.Size(420, 230)
$Form1.Text = "my window"


Create-Button -Name Button1 -text 'My Button A' -x 10 -y 10 -AddClick {
write-host "Function >A< ended";
write-host "******************************************************"
}

Create-Button -Name Button2 -text 'My Button B' -x 250 -y 10 -AddClick {
write-host "another ouput should be written, when clicking Button-B";
write-host "******************************************************"
}


[void]$Form1.ShowDialog()
Go to full post
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: function button()

Post by jvierra »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: function button()

Post by jvierra »

Here is a simple hint at how this works.
  1. function Create-Button{
  2.     Param(
  3.         [string]$Name,
  4.         [string]$text,
  5.         [int]$x,
  6.         [int]$y,
  7.         [scriptblock]$AddClick
  8.     )
  9.    
  10.     $Button = New-Object System.Windows.Forms.Button
  11.     $Button.Location = New-Object System.Drawing.Size($x, $y)
  12.     $Button.Size = New-Object System.Drawing.Size(100, 66)
  13.     $Button.Text = $Text
  14.     $Button.Add_Click($AddClick)
  15.     $Form1.Controls.Add($Button)
  16. }
  17.  
  18. $Form1 = New-Object System.Windows.Forms.Form
  19. $Form1.Size = New-Object System.Drawing.Size(420, 230)
  20. $Form1.Text = "my window"
  21.  
  22.  
  23. Create-Button -Name Button1 -text 'My Button A' -x 10 -y 10 -AddClick {
  24.                     write-host "Function >A< ended";
  25.                     write-host "******************************************************"
  26.                 }
  27.  
  28. Create-Button -Name Button2 -text 'My Button B' -x 250 -y 10 -AddClick {
  29.                     write-host "another ouput should be written, when clicking Button-B";
  30.                     write-host "******************************************************"
  31.                 }
  32.  
  33.  
  34. [void]$Form1.ShowDialog()
  35.  
newbi2009
Posts: 6
Last visit: Fri Mar 18, 2022 2:21 am

Re: function button()

Post by newbi2009 »

Ahhh - very cool! Thanks for this explanation. This helps a lot!
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.
Locked