title: PowerShell button click event scriptblock not working
body: <p>I am working on a GUI with several <code>TabPage</code>s. There are within each <code>TabPage</code> several buttons that should 'do' s.th.</p>
  
  <p>So I defined the <code>$..OnClick={..}</code> which in this example write s.th. - but it doesn't work. Please what's wrong?</p>
  
  <p>Here is a full example that shows the button <code>Check 1</code> in a dialog, but the button isn't working at all:</p>
  
  
  
  <pre class="lang-powershell prettyprint-override"><code>function guibutton ($window,$todo,[string]$name="Butty",[string]$text="Butty",
                      [int]$x=300,[int]$y=200,[int]$w=75,[int]$h=23) {
    $Button = New-Object System.Windows.Forms.Button
    $Button.Location = New-Object System.Drawing.Size($x,$y)
    $Button.Size = New-Object System.Drawing.Size($w,$h)
    $Button.Name = $name
    $Button.Text = $text
    $Button.Add_Click( { $todo } )
    $window.Controls.Add($Button)
  }
  
  
  #Generated Form Function
  function GenerateForm {
  
    #region Import the Assemblies
    [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
    [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
  
    #region Generated Form Objects
    $MainMenu = New-Object System.Windows.Forms.Form
    $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
  
    #Buttons
    $TestTabA = New-Object System.Windows.Forms.Button
    $EXIT = New-Object System.Windows.Forms.Button
    $TabControl = New-Object System.Windows.Forms.TabControl
  
    #Misc Items
    $ProgressBar = New-Object System.Windows.Forms.ProgressBar
    $StatusBar = New-Object System.Windows.Forms.StatusBar
  
    #Tabs
    $TabControl = New-object System.Windows.Forms.TabControl
    $TabTestA = New-Object System.Windows.Forms.TabPage
  
    #----------------------------------------------
    #Generated Event Script Blocks
    #----------------------------------------------
    #Provide Custom Code for events specified in PrimalForms.
    #Unknown
    $handler_MainMenu_Load =
    $OnLoadForm_StateCorrection= { $MainMenu.WindowState = $InitialFormWindowState }
  
    #Buttons
    $TestTabA_OnClick = { $TabControl.SelectTab($TabTestA) }
    $EXIT_OnClick={
       Write-Host "Bye-Bye"
       $MainMenu.close()
    }
  
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 474
    $System_Drawing_Size.Width = 665
  
    $MainMenu.Font = New-Object System.Drawing.Font("Mistral",8.25,0,3,0) # Schriftart festlegen
    $MainMenu.ClientSize = $System_Drawing_Size
    $MainMenu.DataBindings.DefaultDataSourceUpdateMode = 0
    $MainMenu.Name = "MainMenu"
    $MainMenu.Text = "Test GUI"
    $MainMenu.TopMost = $True
    $MainMenu.add_Load($handler_form1_Load)
  
    # == FIX-Trade Button == #
    $TestTabA.Name = "TestTabA"
    $TestTabA.Text = "Test TabA"
    $TestTabA.TabIndex = 2
    $TestTabA.UseVisualStyleBackColor = $True
    $TestTabA.add_Click($TestTabA_OnClick)
    $TestTabA.DataBindings.DefaultDataSourceUpdateMode = 0
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 12
    $System_Drawing_Point.Y = 69
    $TestTabA.Location = $System_Drawing_Point
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 35
    $System_Drawing_Size.Width = 100
    $TestTabA.Size = $System_Drawing_Size
    $MainMenu.Controls.Add($TestTabA)
  
  
    # == EXIT all Button == #
    $EXIT.Name = "EXIT"
    $EXIT.Text = "EXIT"
    $EXIT.TabIndex = 7
    $EXIT.UseVisualStyleBackColor = $True
    $EXIT.add_Click($EXIT_OnClick)
    $EXIT.DataBindings.DefaultDataSourceUpdateMode = 0
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 12
    $System_Drawing_Point.Y = 274
    $EXIT.Location = $System_Drawing_Point
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 35
    $System_Drawing_Size.Width = 100
    $EXIT.Size = $System_Drawing_Size
  
    $MainMenu.Controls.Add($EXIT)
  
    # == Tab Control == #
    $TabControl.Name = "TabControl"
    $TabControl.TabIndex = 4
    $TabControl.SelectedIndex = 0
    $TabControl.DataBindings.DefaultDataSourceUpdateMode = 0
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 118
    $System_Drawing_Point.Y = 70
    $TabControl.Location = $System_Drawing_Point
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 374
    $System_Drawing_Size.Width = 535
    $TabControl.Size = $System_Drawing_Size
  
    # Add the following to the TabControl property-setting section:
  
    $TabSizeMode = New-object System.Windows.Forms.TabSizeMode
    $TabSizeMode = "Fixed"
    $TabControl.SizeMode =$TabSizeMode
    $TabControl.ItemSize = New-Object System.Drawing.Size(0, 1)
    $TabAppearance = New-object System.Windows.Forms.TabAppearance
    $TabAppearance = "Buttons"
    $TabControl.Appearance = $TabAppearance
    $MainMenu.Controls.Add($TabControl)
  
  
    $Check1Button_OnClick  = { Write-Host "Check1-Click, mach was.. " }
    # == Trade Tab == #
    $TabTestA.DataBindings.DefaultDataSourceUpdateMode = 0
    $TabTestA.Name = "TestTabA"
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 4
    $System_Drawing_Point.Y = 22
    $TabTestA.Location = $System_Drawing_Point
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 205
    $System_Drawing_Size.Width = 445
    $TabTestA.Size = $System_Drawing_Size
    $TabTestA.TabIndex = 2
    $TabTestA.Text = "Tab2"
    $TabTestA.UseVisualStyleBackColor = $True
  
    #                         name     Txt        x   y    w  h
    guibutton $TabTestA $Check1Button_OnClick "Check1"   "Check 1"    10  20  75 23
    $TabControl.Controls.Add($TabTestA)
  
    # == Progress Bar == #
    $progressBar.DataBindings.DefaultDataSourceUpdateMode = 0
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 589
    $System_Drawing_Point.Y = 458
    $progressBar.Location = $System_Drawing_Point
    $progressBar.Name = "progressBar"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 15
    $System_Drawing_Size.Width = 75
    $progressBar.Size = $System_Drawing_Size
    $progressBar.TabIndex = 0
  
    $MainMenu.Controls.Add($progressBar)
  
    # == Status bar == #
    $StatusBar.DataBindings.DefaultDataSourceUpdateMode = 0
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 0
    $System_Drawing_Point.Y = 456
    $StatusBar.Location = $System_Drawing_Point
    $StatusBar.Name = "StatusBar"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 18
    $System_Drawing_Size.Width = 665
    $StatusBar.Size = $System_Drawing_Size
    $StatusBar.TabIndex = 1
    $StatusBar.Text = "statusBar - Text..."
    $StatusBar.add_PanelClick($statusBar_PanelClick)
  
    $MainMenu.Controls.Add($StatusBar)
  
    #To show that the tabs are switching correctly, you could add:
    $TabTestA.BackColor = "White"
  
    # == Save and Open Form == #
    $InitialFormWindowState = $MainMenu.WindowState
    $MainMenu.add_Load($OnLoadForm_StateCorrection)
    $MainMenu.ShowDialog()| Out-Null
  
  } #End Function
  
  #Call the Function
  GenerateForm
  </code></pre>
  
  <p>If I start the GUI the dialog appears, but if I click on the button that should <code>write-host ".."</code> nothing happens :(</p>