![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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($function nClick)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 Specs![]() |
| | #6 (permalink) |
| | 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($function nClick);$button.Left = 100; $window.Controls.add($textbox); $window.Controls.Add($button); $window.ShowDialog(); |
My System Specs![]() |
![]() |
| 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 | |||