How to stop triggering "Add_SelectionChanged" before selection is made

Welcome Forums General PowerShell Q&A How to stop triggering "Add_SelectionChanged" before selection is made

This topic contains 1 reply, has 2 voices, and was last updated by

1 year, 6 months ago.

  • Author
  • #78220

    Points: 1
    Rank: Member

    I am clearing a combo box using $cboTarget.Items.Clear(). After the items are cleared "Add_SelectionChanged" is triggered but it shouldn't be, it should only be triggered after an item is selected from the combo box. I'm I using "Items.Clear" incorrectly? Thanks for your help.

    This is the error I'm getting which is understandable if there is nothing to select.

    You cannot call a method on a null-valued expression.
    At C:\UCM\UCM.ps1:79 char:1
    + $targetSelect = ($cboTarget.SelectedItem.toString())
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    This is my code:

        configDir = "C:" + "\UCM"
        labDir = ""
        configList = @()
        configSelection = ""
        targetList = @()
        allTargets = "All Targets"    
        getConfigList = { param ($lab)
            # Get the list of available configurations
            $global:UCM.labDir = $global:UCM.configDir + "\" + $lab
            # Creates a list of folders in directory
            $global:UCM.configList = Get-ChildItem $global:UCM.labDir  | foreach {$}
            #Fill combo box with use cases (i.e. list of folder names in labDir)      
            $UseCaseItems = $global:UCM.configList
            ForEach ($UseCaseItem in $UseCaseItems) {
            $cboUseCase.items.add($UseCaseItem) }
        setConfigSelection = { param ($selection)
            $cboTarget.Items.Clear() # Clear target list before selecting UseCase  
            #Selected Use Case
            $global:UCM.configSelection = $selection
            &$global:UCM.getTargetList -config $global:UCM.configSelection
        getTargetList = { param ($config)
            # Get the list of available use case targets
            Write-Host "UCM: Target List:" -ForegroundColor Magenta
            $targetFile = $global:UCM.labDir
            $targetFile += "\$config\UCM-Targets.xml"
            write-debug "targetFile: $targetFile"
            $targetFile =   [xml](Get-Content $targetFile)
            $global:UCM.IPlookup =@{}
            $targetFile.Deployment.Target | Foreach {
            write-host "hostname: " + $_.hostname
            write-host "IP: " + $_.IP
            $global:UCM.IPlookup.Add($_.hostname, $_.IP)
            Write-Host $global:UCM.targetList
            #Fill combo box with target hostnames
            #Add "All Targets" first
            #Add remaining targets in IPlookup hash table
            $TargetItems = $global:UCM.IPlookup.keys
            ForEach ($TargetItem in $TargetItems) {
            $cboTarget.items.add($TargetItem) }
    # Select Use Case
    $configSelect = ($cboUseCase.SelectedItem.toString())
    &$global:UCM.setConfigSelection -selection $configSelect
    # Select Target
    $targetSelect = ($cboTarget.SelectedItem.toString())
    &$global:UCM.getConfigList -lab "MY-Lab"
  • #78235

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    Technically, the event doesn't fire when you select something, it fires whenever the selection changes. Clearing the list "removes" the selection, which is a change – and so the event fires.

    Probably the easiest way to deal with it is, in your handler, to make sure you've got an instance of something before doing anything else. Even proactively de-selecting something is still a "change" that will fire the event.

The topic ‘How to stop triggering "Add_SelectionChanged" before selection is made’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort