PowerShell In GUI Blog

PowerShell GUI Box Just In A Few Clicks

How to use and How not to use eventhandlers

leave a comment »

Today I noticed that sometimes I used eventhandlers in a wrong way.

There are two ways to use standard .NET (also known as ‘CLR’ eventhandlers in WPF) eventhandlers. The first way is very obvious and well-known for everyone used SharpDevelop, Visual Studio and so on. This way consists of two parts: declaring an eventhandler and attaching it to a control.

$handler = {some code;}

$button.add_Click($handler);

The same example can be rewritten in a one-statement way:

$button.add_Click(([System.EventHandler]$handler = {some code;}));

The second way is to use Register-ObjectEvent commandlet:

Register-ObjectEvent -InputObject $button -EventName Click -Action $handler;

or, using a single statement:

Register-ObjectEvent -InputObject $button -EventName Click -Action {some code};

However, things are not so good as they seemed to be. The second way produces delayed events, results of their firing can be seen by using the sample below. The sample is workable in PowerGUI as well as in native powershell.

On clicking buttons 1, 2 or 3, we’ll see both Write-Host message and MessageBox. After clicking 4, 5 or 6, we need to close the form, after that events fire. Eventually, if you click 4, 5, 6 and, for example, 1, events will shot one by one without closing the form.

So that be careful choosing the way whereby you use eventhandlers. 😉

#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.8.0
# Generated On: 14.07.2010 18:37
# Generated By: apetrov1
########################################################################

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$button1 = New-Object System.Windows.Forms.Button
$script:button2 = New-Object System.Windows.Forms.Button
$global:button3 = New-Object System.Windows.Forms.Button
$button4 = New-Object System.Windows.Forms.Button
$script:button5 = New-Object System.Windows.Forms.Button
$global:button6 = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
    #region handler for buttons
$handler_button_Click=
{
#TODO: Place custom script here
    [string]$output = $this;
    $output += "`r`n";
    $output += $global:this;
    Write-Host $output;
    [System.Windows.Forms.MessageBox]::Show($output);
}
    #endregion handler for buttons
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
    $form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
##region Generated Form Code
    #region $form1
$form1.Text = "Primal Form"
$form1.Name = "form1"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 108
$System_Drawing_Size.Height = 291
$form1.ClientSize = $System_Drawing_Size
    #endregion $form1

    #region $button1
$button1.TabIndex = 0
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 80
$System_Drawing_Size.Height = 30
$button1.Size = $System_Drawing_Size
$button1.UseVisualStyleBackColor = $True
$button1.Text = "button1"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 15
$System_Drawing_Point.Y = 15
$button1.Location = $System_Drawing_Point
$button1.DataBindings.DefaultDataSourceUpdateMode = 0
$button1.add_Click($handler_button_Click)
$form1.Controls.Add($button1)
    #endregion $button1
    #region $script:button2
$script:button2.TabIndex = 1
$script:button2.Name = "button2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 80
$System_Drawing_Size.Height = 30
$script:button2.Size = $System_Drawing_Size
$script:button2.UseVisualStyleBackColor = $True
$script:button2.Text = "button2"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 15
$System_Drawing_Point.Y = 60
$script:button2.Location = $System_Drawing_Point
$script:button2.DataBindings.DefaultDataSourceUpdateMode = 0
$script:button2.add_Click($handler_button_Click)
$form1.Controls.Add($script:button2)
    #endregion $script:button2
    #region $global:button3
$global:button3.TabIndex = 2
$global:button3.Name = "button3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 80
$System_Drawing_Size.Height = 30
$global:button3.Size = $System_Drawing_Size
$global:button3.UseVisualStyleBackColor = $True
$global:button3.Text = "button3"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 15
$System_Drawing_Point.Y = 105
$global:button3.Location = $System_Drawing_Point
$global:button3.DataBindings.DefaultDataSourceUpdateMode = 0
$global:button3.add_Click($handler_button_Click)
$form1.Controls.Add($global:button3)
    #endregion $global:button3
    #region $button4
$button4.TabIndex = 3
$button4.Name = "button4"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 80
$System_Drawing_Size.Height = 30
$button4.Size = $System_Drawing_Size
$button4.UseVisualStyleBackColor = $True
$button4.Text = "button4"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 15
$System_Drawing_Point.Y = 150
$button4.Location = $System_Drawing_Point
$button4.DataBindings.DefaultDataSourceUpdateMode = 0
#$button4.add_Click($handler_button4_Click)
$null = Register-ObjectEvent -InputObject $button4 `
    -EventName Click `
    -Action $handler_button_Click;
$form1.Controls.Add($button4)
    #endregion $button4
    #region $script:button5
$script:button5.TabIndex = 4
$script:button5.Name = "button5"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 80
$System_Drawing_Size.Height = 30
$script:button5.Size = $System_Drawing_Size
$script:button5.UseVisualStyleBackColor = $True
$script:button5.Text = "button5"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 15
$System_Drawing_Point.Y = 195
$script:button5.Location = $System_Drawing_Point
$script:button5.DataBindings.DefaultDataSourceUpdateMode = 0
#$script:button5.add_Click($handler_button5_Click)
$null = Register-ObjectEvent -InputObject $script:button5 `
    -EventName Click `
    -Action $handler_button_Click;
$form1.Controls.Add($script:button5)
    #endregion $script:button5
    #region $global:button6
$global:button6.TabIndex = 5
$global:button6.Name = "button6"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 80
$System_Drawing_Size.Height = 30
$global:button6.Size = $System_Drawing_Size
$global:button6.UseVisualStyleBackColor = $True
$global:button6.Text = "button6"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 15
$System_Drawing_Point.Y = 240
$global:button6.Location = $System_Drawing_Point
$global:button6.DataBindings.DefaultDataSourceUpdateMode = 0
#$global:button6.add_Click($handler_button6_Click)
$null = Register-ObjectEvent -InputObject $global:button6 `
    -EventName Click `
    -Action $handler_button_Click;
$form1.Controls.Add($global:button6)
    #endregion $global:button6
##endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm

This is my first post done in Firefox as a result of a series of irritating crashes of both my Chrome 6.0.453.1 and 6.0.458.1. I’m currently using Firefox 4.0 Beta 1 from http://portableapps.com/news/2010-07-06_-_firefox_portable_4.0_beta_1 . How it is said here, ‘the flight is good’ or ‘flying well’.

Advertisements

Written by Alexander Petrovskiy

July 14, 2010 at 4:22 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: