• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

events, arguments, etc...

F

fixitchris

#1
What is the method for a clicked item in a listview?
This is what I have $listview.add_click($function:onClick); 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($function:onStart);
$button2.add_click($function:onStop);
$listview.add_click($function:onClick);
$window.controls.add($listview);
$window.Controls.Add($button);
$window.Controls.Add($button2);
$window.add_shown($function:onLoad);
$window.ShowDialog();
 

My Computer

B

Bruce Payette [MSFT]

#2
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($function:onClick); 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($function:onStart);
> $button2.add_click($function:onStop);
> $listview.add_click($function:onClick);
> $window.controls.add($listview);
> $window.Controls.Add($button);
> $window.Controls.Add($button2);
> $window.add_shown($function:onLoad);
> $window.ShowDialog();
 

My Computer

F

fixitchris

#4
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 Computer

F

fixitchris

#5
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 Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)