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, arguments, etc...

Reply
 
Old 11-14-2006   #1 (permalink)
fixitchris


 
 

events, arguments, etc...

What is the method for a clicked item in a listview?
This is what I have $listview.add_click($functionnClick); BUT
function onClick($sender,$e) {Write-Host "click"};
$sender and $e seem to be NULL...???

I also tried to do this
$lvsi = New-Object windows.forms.listviewitem.listviewsubitem; BUT
that did not work?

Is there a good resource for dealing with events in PS?

Thanks
Chris



[reflection.assembly]::loadwithpartialname("system.windows.forms");

function onStart($sender,$e) {write-Host "service started"};
function onStop($sender,$e) {write-Host "service stopped"};
function onClick($sender,$e) {Write-Host "click"};

function onLoad($sender,$e) {$listbox.items.clear;get-wmiobject
win32_service |
%{$lv=$listview.items.add($_.name);$lv.subitems.add($_.state)} ;};

$window = New-Object windows.forms.form;
$window.Size = New-Object system.drawing.size @(220,600);
$window.Text = "Windows Services";
$window.FormBorderStyle = "FixedDialog";
$window.MaximizeBox= 0;
$button = new-Object windows.Forms.Button;$button.Text = "Start";
$button2 = new-Object windows.Forms.Button;$button2.Text =
"Stop";$button2.Left = 80;
$listview = New-Object windows.forms.listview;$listview.Top =
25;$listview.Height = 550;$listview.width = 210;$listview.view="Details"
$listview.MultiSelect=0;$listview.FullRowSelect =
1;$listview.Columns.Add("Name",125);$listview.Columns.Add("Status",60);
$lvi = New-Object windows.forms.listviewitem;

$button.add_click($functionnStart);
$button2.add_click($functionnStop);
$listview.add_click($functionnClick);
$window.controls.add($listview);
$window.Controls.Add($button);
$window.Controls.Add($button2);
$window.add_shown($functionnLoad);
$window.ShowDialog();

My System SpecsSystem Spec
Old 11-14-2006   #2 (permalink)
Bruce Payette [MSFT]


 
 

Re: events, arguments, etc...

We special-case this pattern so you don't need to declare any parameters.
When a scriptblock is used as an EventHandler, the sender is available in
$this and the event args are available in $_. Also, unless you also need to
use your handler as something other than as an event handler, there is no
need to define it as a function. Simple assignment to a variable is
sufficient.

$myHandler = {Write-Host "click sender: $this eventArgs: '$_'"}
$listview.add_click( $myHandler );

And to answer your other question, listviewsubitem is a nested class so you
have to use the '+' instead of a dot in the typename. (This is an artifact
of the representation that System.Reflection uses to represent the type
name.) So

$o = new-object System.Windows.Forms.listviewitem+listviewsubitem

works properly.

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

"fixitchris" <fixitchris@discussions.microsoft.com> wrote in message
news:36CE5D46-69DE-4309-8C9B-31A9A3EF8A87@microsoft.com...
> What is the method for a clicked item in a listview?
> This is what I have $listview.add_click($functionnClick); BUT
> function onClick($sender,$e) {Write-Host "click"};
> $sender and $e seem to be NULL...???
>
> I also tried to do this
> $lvsi = New-Object windows.forms.listviewitem.listviewsubitem; BUT
> that did not work?
>
> Is there a good resource for dealing with events in PS?
>
> Thanks
> Chris
>
>
>
> [reflection.assembly]::loadwithpartialname("system.windows.forms");
>
> function onStart($sender,$e) {write-Host "service started"};
> function onStop($sender,$e) {write-Host "service stopped"};
> function onClick($sender,$e) {Write-Host "click"};
>
> function onLoad($sender,$e) {$listbox.items.clear;get-wmiobject
> win32_service |
> %{$lv=$listview.items.add($_.name);$lv.subitems.add($_.state)} ;};
>
> $window = New-Object windows.forms.form;
> $window.Size = New-Object system.drawing.size @(220,600);
> $window.Text = "Windows Services";
> $window.FormBorderStyle = "FixedDialog";
> $window.MaximizeBox= 0;
> $button = new-Object windows.Forms.Button;$button.Text = "Start";
> $button2 = new-Object windows.Forms.Button;$button2.Text =
> "Stop";$button2.Left = 80;
> $listview = New-Object windows.forms.listview;$listview.Top =
> 25;$listview.Height = 550;$listview.width = 210;$listview.view="Details"
> $listview.MultiSelect=0;$listview.FullRowSelect =
> 1;$listview.Columns.Add("Name",125);$listview.Columns.Add("Status",60);
> $lvi = New-Object windows.forms.listviewitem;
>
> $button.add_click($functionnStart);
> $button2.add_click($functionnStop);
> $listview.add_click($functionnClick);
> $window.controls.add($listview);
> $window.Controls.Add($button);
> $window.Controls.Add($button2);
> $window.add_shown($functionnLoad);
> $window.ShowDialog();



My System SpecsSystem Spec
Old 11-14-2006   #3 (permalink)
fixitchris


 
 

Re: events, arguments, etc...

Thanks. I am leaving work now. but I will look this over tonight... Is this
covered in your book?

Chris
My System SpecsSystem Spec
Old 11-14-2006   #4 (permalink)
fixitchris


 
 

Re: events, arguments, etc...

I'm lost bruce....sometimes this works... most of the time it does not. I'm
not sure how to use the get_Item method either. i tried FOREACH through the
$this.items[], and just through $this ... I'm missing something. Thanks

$listview_handler= { $this.SelectedItems.item(0).text };

$listview.add_ItemSelectionChanged($listview_handler);



Exception getting "Item": "Exception calling "get_Item" with "1"
argument(s): "InvalidArgument=Valu
e of '0' is not valid for 'index'.

Parameter name: index""

At line:6 char:47

+ $listview_handler= { $this.SelectedItems.item( <<<< 0).text };

My System SpecsSystem Spec
Old 11-15-2006   #5 (permalink)
fixitchris


 
 

works beautifully

I guess I just needed to sleep on this...and I installed PS 1.0 as well.

$listview_handler = { if ($_.isselected -eq 1) {$lv =
$this.items[$_.itemindex]; Write-Host $lv.text } };
$listview.add_ItemSelectionChanged($listview_handler);



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
getting arguments from pipe PowerShell
Getting arguments from STDIN when command line arguments are missing PowerShell
passing arguments PowerShell
Arguments parsing 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