none
Clearing a grouplist on exit of a form RRS feed

  • Question

  • Hello,

    I have a grouplist box on a form that is successfully working in it's basic purpose.

    The issue I have is that when I close the form, the data still remains in the grouplist variable, so when the form is opened again, it is showing the data from the previous run.

    So all I want to be able to do is clear this data when the form closes.

    I have found a gazillion pages that all say ~listBox.Items.Clear() should work, but I can't get it to work.

    For completeness here are the relevant parts of my code:

    $script:adgrouplist = (Get-ADPrincipalGroupMembership (Get-ADComputer $Script:Machine) | select-object name)


    function checkADGroups
    {
        foreach ($gn in $script:adgrouplist) {
            [void]$ADgroupslistBox.Items.Add($gn.Name)
        }
    }

    So all good so far.

    $Closebutton.Add_MouseClick({deleteADGlist})


    function deleteADGlist
    {
        $ADgroupslistBox.Items.Clear()
        $adgrouplist.Items.Clear()
          
    } 

    So neither of these lines do anything, I have also had a go at a foreach type of thing, but none of them seemed to work.

    What am I missing or doing wrong ? 

    Please help me ! this supposedly simple thing is driving me mad !

    Many thanks

    Damian

    Saturday, July 28, 2018 4:49 PM

Answers

  • The following would be better:

    $form.Add_FormClosing({
        Write-Host 'Closing called' -Fore green
        $ADgroupslistBox.Items.Clear()
        $adgrouplist.Clear()
    })


    \_(ツ)_/

    • Marked as answer by DamianBz Sunday, August 19, 2018 4:12 PM
    Saturday, July 28, 2018 6:37 PM

All replies

  • Place a write-Host in the code to see if the code is actually being called.

    $Closebutton
    .Add_MouseClick({
        Write-Host 'Mouse clicked' -Fore green
        $ADgroupslistBox.Items.Clear()
        $adgrouplist.Items.Clear()
    })
    


    \_(ツ)_/




    • Edited by jrv Saturday, July 28, 2018 5:16 PM
    Saturday, July 28, 2018 4:57 PM
  • Yep definitely being called...

     Mouse clicked
    Cancel
    PS C:\Users\Damian> $adgrouplist

    name
    ----
    Domain Computers
    Backup Operators
    DnsAdmins

    But as you can see the list is not being cleared :(

    Cheers

    Damian

    Saturday, July 28, 2018 6:23 PM
  • What kind of object is $adGroupList?

    \_(ツ)_/

    Saturday, July 28, 2018 6:32 PM
  • The following would be better:

    $form.Add_FormClosing({
        Write-Host 'Closing called' -Fore green
        $ADgroupslistBox.Items.Clear()
        $adgrouplist.Clear()
    })


    \_(ツ)_/

    • Marked as answer by DamianBz Sunday, August 19, 2018 4:12 PM
    Saturday, July 28, 2018 6:37 PM
  • Ahhhhhh  ....

    That did the trick !

    Thanks jrv

    So why does this work but not on a the form close button ?

    Cheers

    Damian

    Saturday, July 28, 2018 6:49 PM
  • It should work on both with this: $adgrouplist.Clear()

    Notice what I changed.


    \_(ツ)_/


    • Edited by jrv Saturday, July 28, 2018 6:57 PM
    Saturday, July 28, 2018 6:57 PM
  • Ah yes the removal of "Items" ... i wasn't sure as i'd wiped my original code !

    So the removal of Items removed my items !  it's a strange old world :)

    • Edited by DamianBz Saturday, July 28, 2018 7:11 PM
    Saturday, July 28, 2018 7:09 PM
  • Controls have "Items".  Collections don't.  When calling "Items",  Items is a collection.


    \_(ツ)_/

    Saturday, July 28, 2018 7:12 PM
  • Got it ... understood.

    Thanks jrv

    Saturday, July 28, 2018 7:28 PM