Windows Vista Forums

events, arguments, etc...

  1. #1


    fixitchris Guest

    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

  2. #2


    Bruce Payette [MSFT] Guest

    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

  3. #3


    fixitchris Guest

    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

  4. #4


    fixitchris Guest

    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

  5. #5


    fixitchris Guest

    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


events, arguments, etc...
Similar Threads
Thread Forum
Online calendar events showing up as Private events? Live Mail
How To Clear Administrative Events Log - Events Viewer Vista General
getting arguments from pipe PowerShell
Getting arguments from STDIN when command line arguments are missing PowerShell
Fire events from Xaml files.. events in Pre compiled Dll's Avalon