Have Combobox Listen to AddLayer Event?

2990
2
Jump to solution
07-16-2015 02:58 PM
MikeMacRae
Occasional Contributor III

I am building an addin combobox. The combobox will populate a list of all the layers that are in the TOC in ArcMap upon initialization of ArcMap. The user will choose a layer and some actions will take place.

What I want to do is have the comobox listen to the Table of Contents and update any time the user adds or removes a layer automatically. Is this possible?

Thanks

0 Kudos
1 Solution

Accepted Solutions
FreddieGibson
Occasional Contributor III

You would need to implement both the ComboBox and an Application Extension. The Application Extension would need to listen for changes in the TOC and you'd need to build a hook to feed the values to the ComboBox. I haven't tested it, but I'm thinking it'd look something like this.

import arcpy
import pythonaddins

class ComboBoxClass(object):
    """Implementation for ComboBox"""
    def __init__(self):
        self.items = list()
        self.editable = self.enabled = True
        self.dropdownWidth = self.width = 'WWWWWW'
        ComboBoxClass._hook = self        

class ExtensionClass(object):
    """Implementation for Application Extension """
    def __init__(self):
        self.enabled = True
        ExtensionClass._hook = self
        
    def itemAdded(self, new_item):
        ComboBoxClass._hook.items.extend([new_item])
        
    def itemDeleted(self, deleted_item):
        ComboBoxClass._hook.items.remove(deleted_item)    

View solution in original post

2 Replies
FreddieGibson
Occasional Contributor III

You would need to implement both the ComboBox and an Application Extension. The Application Extension would need to listen for changes in the TOC and you'd need to build a hook to feed the values to the ComboBox. I haven't tested it, but I'm thinking it'd look something like this.

import arcpy
import pythonaddins

class ComboBoxClass(object):
    """Implementation for ComboBox"""
    def __init__(self):
        self.items = list()
        self.editable = self.enabled = True
        self.dropdownWidth = self.width = 'WWWWWW'
        ComboBoxClass._hook = self        

class ExtensionClass(object):
    """Implementation for Application Extension """
    def __init__(self):
        self.enabled = True
        ExtensionClass._hook = self
        
    def itemAdded(self, new_item):
        ComboBoxClass._hook.items.extend([new_item])
        
    def itemDeleted(self, deleted_item):
        ComboBoxClass._hook.items.remove(deleted_item)    
MikeMacRae
Occasional Contributor III

HeyFreddie. You have me chasing down a better understanding of hooking in python. I've used your example and modified it a bit. This worked well. Thanks for the input.

0 Kudos