Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Events with powershell

Reply
 
Old 11-10-2006   #1 (permalink)
Roy


 
 

Events with powershell

Does powershell know how to deal with events?

For e.g.:

I create a windows form with a button, how do i know that the button was
pressed in power shell?

My System SpecsSystem Spec
Old 11-10-2006   #2 (permalink)
Adam Milazzo


 
 

Re: Events with powershell

Roy wrote:
> Does powershell know how to deal with events?
>
> For e.g.:
>
> I create a windows form with a button, how do i know that the button was
> pressed in power shell?


It /should/ be something like this:

Create a function with the right signature:

function onPress($sender,$e) { ... }

then:

$button = new-object Windows.Forms.Button
$button.add_Click(onPress)


But I don't have PowerShell here, so somebody correct me if I'm wrong. :-)
My System SpecsSystem Spec
Old 11-10-2006   #3 (permalink)
klumsy@xtra.co.nz


 
 

Re: Events with powershell

add_click takes a scriptblock

so
$button.add_click({write-host "it was clicked" })

or i suppose in your example

$button.add_click({onPress})

My System SpecsSystem Spec
Old 11-10-2006   #4 (permalink)
Adam Milazzo


 
 

Re: Events with powershell

klumsy@xtra.co.nz wrote:
> add_click takes a scriptblock
>
> so
> $button.add_click({write-host "it was clicked" })
>
> or i suppose in your example
>
> $button.add_click({onPress})


Given that scriptblocks are basically inline functions, it's a shame
they can't be used interchangeably.
My System SpecsSystem Spec
Old 11-12-2006   #5 (permalink)
Bruce Payette [MSFT]


 
 

Re: Events with powershell

Functions and scriptblocks can be used interchangably but not quite the way
you expect. If you have a function onClick, then you can bind it to an event
handler by doing

$button.add_Click($functionnClick)

You can't simply say onClick because that looks like a command invocation
rather than getting a reference to the function itself. (In fact if you just
specify the name of the command by itself, you'll get an error. You have to
enclose a command invocation in parens to invoke it. This is one area where
we require you to be explicit about your intent.)

-bruce

--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

Visit the Windows PowerShell Team blog at:
http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:
http://www.microsoft.com/technet/scr.../hubs/msh.mspx
My Book: http://manning.com/powershell
"Adam Milazzo" <adamm@san.rr.com> wrote in message
news:u3T6PkTBHHA.204@TK2MSFTNGP04.phx.gbl...
> klumsy@xtra.co.nz wrote:
>> add_click takes a scriptblock
>>
>> so
>> $button.add_click({write-host "it was clicked" })
>>
>> or i suppose in your example
>>
>> $button.add_click({onPress})

>
> Given that scriptblocks are basically inline functions, it's a shame they
> can't be used interchangeably.



My System SpecsSystem Spec
Old 11-13-2006   #6 (permalink)
fixitchris


 
 

RE: Events with powershell

Cool stuff!

function onClick($sender,$e) {write-Host $textbox.text};

[reflection.assembly]::loadwithpartialname("system.windows.forms");
$window = New-Object windows.forms.form;
$window.Size = New-Object system.drawing.size @(200,200);
$textbox = new-Object windows.Forms.TextBox;
$button = new-Object windows.Forms.Button;
$button.add_click($functionnClick);
$button.Left = 100;
$window.Controls.add($textbox);
$window.Controls.Add($button);
$window.ShowDialog();
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Can Powershell handle COM events? PowerShell
COM events? PowerShell
New snapin for consuming .NET events in PowerShell PowerShell
Manipulating mouse events through Powershell PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46