How to stop triggering "Add_SelectionChanged" before selection is made

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:

$global:UCM=
@{
    configDir = "C:" + "\UCM"
    labDir = ""
    configList = @()
    configSelection = ""
    targetList = @()
    allTargets = "All Targets"    
    IPlookup=@{}

#-----------------------------------------------------------------------------------------------------
    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 {$_.name}

        #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
        $cboTarget.items.Add($global:UCM.allTargets)
        
        #Add remaining targets in IPlookup hash table
        $TargetItems = $global:UCM.IPlookup.keys
        ForEach ($TargetItem in $TargetItems) {
        $cboTarget.items.add($TargetItem) }
    }  
}

# Select Use Case
$cboUseCase.Add_SelectionChanged({

$configSelect = ($cboUseCase.SelectedItem.toString())

&$global:UCM.setConfigSelection -selection $configSelect
        
})

# Select Target
$cboTarget.Add_SelectionChanged({

$targetSelect = ($cboTarget.SelectedItem.toString())

})

&$global:UCM.getConfigList -lab "MY-Lab"

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.