![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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, arguments, etc... What is the method for a clicked item in a listview? This is what I have $listview.add_click($function nClick); BUTfunction 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 nStart);$button2.add_click($function nStop);$listview.add_click($function nClick);$window.controls.add($listview); $window.Controls.Add($button); $window.Controls.Add($button2); $window.add_shown($function nLoad);$window.ShowDialog(); |
My System Specs![]() |
| | #2 (permalink) |
| | 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($function nClick); 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 nStart);> $button2.add_click($function nStop);> $listview.add_click($function nClick);> $window.controls.add($listview); > $window.Controls.Add($button); > $window.Controls.Add($button2); > $window.add_shown($function nLoad);> $window.ShowDialog(); |
My System Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||