Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

Reacting to user input

2010-12-02

Continued from here.

The concept of events is that when a GUI component (a “control”) reacts on something like user actions, the control has the power to inform anyone who wants to know about that action. This is the event system of the .NET Framework. Thanks to delegates, the component can have a PowerShell function invoked. I want to add a button to my form, and then, I want a function to be called when the button is clicked.

To start, before the ShowDialog call, I create a button and add that to the form.

#Addition: Create a button and add it to the form.
$myButton=New-Object Windows.Forms.Button
$myButton.Text="Click me!"
$myButton.Location=New-Object Drawing.Point(200,60)
$myForm.Controls.Add($myButton)

Now, before we add the function that will be called when the button is clicked, we need to add code to register events. In C#, we can add delegates to a list and in Visual Basic, we can call a member function for this. In PowerShell, there are a few steps that must be taken. Get an event reference, create a delegate and connect a function. The MSDN blogger Brett Ernst has written a function for this, that I add to my script.

This function takes the object we want to listen to, the name of the event that will cause our function to be called, and the actual function that will be called.

#Addition: Brett Ernst's function for adding an event handler.
function AddHandler([object]$target,[string]$eventName,$script)
{
$evt = $target.GetType().GetEvent($eventName)
$eh = [EventHandler]$script
$del = [Delegate]::CreateDelegate($evt.EventHandlerType,$eh.Target,$eh.Method)
$evt.AddEventHandler($target,$del)
}

This is my function. It extracts the user input from the text field and displays it in a message box.

#Create the target function.
$myHandler={

#Exctract the user text from the text field.
$userInput=$someField.Text

#Show the text in a messagebox.
[Windows.Forms.MessageBox]::Show($userInput)
}

This is the final row that I add before the existing ShowDialog call. It uses the AddHandler function to connect my function with the Click event of the button.

#Event handling: Connect a function to a control event.
AddHandler $myButton "Click" $myHandler

This is the complete code:

#Remenber: No line break here!
[System.Reflection.Assembly]::Load("System.Windows.Forms, 
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

$myForm=New-Object Windows.Forms.Form
$myForm.Size=New-Object Drawing.Size(360,200)
$myForm.Text="Using Windows GUI from PowerShell"

$someLabel=New-Object Windows.Forms.Label
$someLabel.AutoSize=$true
$someLabel.Text="Enter your name:"
$someLabel.Location=New-Object Drawing.Point(10,10)

$someField=New-Object Windows.Forms.TextBox
$someField.Size=New-Object Drawing.Size(300,16)
$someField.Location=New-Object Drawing.Point(10,30)

$myForm.Controls.Add($someLabel)
$myForm.Controls.Add($someField)

#Addition: Create a button and add it to the form.
$myButton=New-Object Windows.Forms.Button
$myButton.Text="Click me!"
$myButton.Location=New-Object Drawing.Point(200,60)
$myForm.Controls.Add($myButton)

#Addition: Brettern's function for adding an event handler.
function AddHandler([object]$target,[string]$eventName,$script)
{
$evt = $target.GetType().GetEvent($eventName)
$eh = [EventHandler]$script
$del = [Delegate]::CreateDelegate($evt.EventHandlerType,$eh.Target,$eh.Method)
$evt.AddEventHandler($target,$del)
}

#Create the target function.
$myHandler={

#Exctract the user text from the text field.
$userInput=$someField.Text

#Show the text in a messagebox.
[Windows.Forms.MessageBox]::Show($userInput)
}

#Event handling: Connect a function to a control event.
AddHandler $myButton "Click" $myHandler

$myForm.ShowDialog()

And this is the result:

And now I take a two week vacation from blogging. I will have guests from the other side of the planet, and I will work with my podcast during their stay.

Categories: PowerShell

Tags: PowerShell

Leave a Reply

Your email address will not be published. Required fields are marked *



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips