powershell Start new button by clicking button I have two scripts. The first contains my functions. The second script uses the functions from the first script.
Everything works well, except for the button click. Then I get the following error.

  
*
  
*The variable '$ClickButton' cannot be retrieved because it has not been set.
  At D:\Scripts\Test\MyFunctions\MyFunctions.ps1:31 char:24
  
*$Button.Add_Click({$ClickButton})
  
*
  
*CategoryInfo: InvalidOperation: (ClickButton:String) [], RuntimeExceptio
  
*FullyQualifiedErrorId : VariableIsUndefined*
  

Every parameter is passed on to the function, but the $clickButton parameter is not being accepted.
Is this not possible, or should I change something?
First script (MyFunctions.ps1):
Function Add-Button {
param(
    [int]$ButtonX,
    [int]$ButtonY,
    [int]$ButtonWidth,
    [int]$ButtonHeight,
    [string]$ButtonName,
    [string]$ClickButton
    )

$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size($ButtonX,$ButtonY)
$Button.Size = New-Object System.Drawing.Size($ButtonWidth,$ButtonHeight)
$Button.Text = $ButtonName
$Button.Add_Click({$ClickButton})
$objForm.Controls.Add($Button)
}

Second script:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  


. .\MyFunctions\MyFunctions.ps1




Function MainForm {

$MainForm = New-Object System.Windows.Forms.Form    
$MainForm.Size = New-Object System.Drawing.Size(700,500)  
$MainForm.Text= "Apply RetentionPolicy"
$objform=$MainForm

Add-Button -ButtonX 20 -ButtonY 20 -ButtonWidth 250 -ButtonHeight 40 -ButtonName "Apply to User" -ClickButton ApplyToUser


$MainForm.Add_Shown({$MainForm.Activate()})
[void] $MainForm.ShowDialog()
}

Function ApplyToUser {

$testform = New-Object System.Windows.Forms.Form    
$testform.Size = New-Object System.Drawing.Size(700,500)  
$testform.Text= "test"
Add-TextLabel -LabelX 330 -LabelY 30 -LabelWidth 450 -LabelHeigth 40 -Text "test test test"
$testform.Add_Shown({$testform.Activate()})
[void] $testform.ShowDialog()
}


Mainform


Change your $ClickButton parameter to a ScriptBlock instead of a string and then change how you are calling the Add-Button procedure.
Function Add-Button {
param(
    [int]$ButtonX,
    [int]$ButtonY,
    [int]$ButtonWidth,
    [int]$ButtonHeight,
    [string]$ButtonName,
    [ScriptBlock]$ClickButton  #<-- Change from [String] To [ScriptBlock]
    )

$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size($ButtonX,$ButtonY)
$Button.Size = New-Object System.Drawing.Size($ButtonWidth,$ButtonHeight)
$Button.Text = $ButtonName
$Button.Add_Click($ClickButton) #<-- Remove the curly braces {} around variable
$objForm.Controls.Add($Button)
}


Add-Button -ButtonX 20 -ButtonY 20 -ButtonWidth 250 -ButtonHeight 40 `
   -ButtonName "Apply to User" `
   -ClickButton {ApplyToUser} #<-- Add curly braces around your function reference