Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Events with powershell

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 11-10-2006   #1 (permalink)
Roy
Guest


 

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
Guest


 

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
Guest


 

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
Guest


 

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]
Guest


 

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
Guest


 

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
Closed Thread
Update your Vista Drivers Update Your Drivers Now!!

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can Powershell handle COM events? Duncan Smith PowerShell 6 07-27-2007 08:17 AM
New snapin for consuming .NET events in PowerShell Oisin Grehan PowerShell 1 05-10-2007 08:10 AM
Manipulating mouse events through Powershell danielfeichtinger@gmail.com PowerShell 5 04-17-2007 05:19 AM
Fire events from Xaml files.. events in Pre compiled Dll's SureshGubba Avalon 1 09-15-2006 01:56 PM


Vistax64.com 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 2005-2008

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 47 48 49 50 51