The SubsetHelperUI is a utility class to aid in determining the relevant set of parameters to auto-update when set to listen to the events and other shared utilities.
        private void UnWireGraphicsEvents()
        {
            try
            {
                if (m_naWindowEventSource == null)
                {
                    return;
                }

                if (m_graphicsEventSource == null)
                {
                    return;
                }

                ((ESRI.ArcGIS.Carto.IGraphicsContainerEvents_Event)(m_graphicsEventSource)).AllElementsDeleted -= m_AllGraphicsDeleted;
                ((ESRI.ArcGIS.Carto.IGraphicsContainerEvents_Event)(m_graphicsEventSource)).ElementAdded       -= m_GraphicAdded;
                ((ESRI.ArcGIS.Carto.IGraphicsContainerEvents_Event)(m_graphicsEventSource)).ElementDeleted     -= m_GraphicDeleted;
                ((ESRI.ArcGIS.Carto.IGraphicsContainerEvents_Event)(m_graphicsEventSource)).ElementsAdded      -= m_GraphicsAdded;
                ((ESRI.ArcGIS.Carto.IGraphicsContainerEvents_Event)(m_graphicsEventSource)).ElementUpdated     -= m_GraphicUpdated;

                m_graphicsEventSource = null;

                SubsetHelperUI.ClearEIDArrayParameterValues(m_nax, SubsetHelperUI.GraphicsEIDArrayBaseName);
            }
            catch (Exception ex)
            {
                string msg = SubsetHelperUI.GetFullExceptionMessage(ex);
                MessageBox.Show(msg, "UnWire Graphics Events");
            }
        }
        private void UnWireSelectionEvent()
        {
            try
            {
                if (m_naWindowEventSource == null)
                {
                    return;
                }

                if (m_mapEventSource == null)
                {
                    return;
                }

                ((ESRI.ArcGIS.Carto.IActiveViewEvents_Event)(m_mapEventSource)).SelectionChanged -= m_ActiveViewEventsSelectionChanged;
                m_mapEventSource = null;

                SubsetHelperUI.ClearEIDArrayParameterValues(m_nax, SubsetHelperUI.SelectionEIDArrayBaseName);
            }
            catch (Exception ex)
            {
                string msg = SubsetHelperUI.GetFullExceptionMessage(ex);
                MessageBox.Show(msg, "UnWire Selection Event");
            }
        }
        private void WireSelectionEvent()
        {
            try
            {
                if (m_naWindowEventSource == null)
                {
                    return;
                }

                if (m_mapEventSource != null)
                {
                    UnWireSelectionEvent();
                }

                m_mapEventSource = ActiveMap;
                if (m_mapEventSource == null)
                {
                    return;
                }

                UpdateSelectionEIDArrayParameterValues();

                //Create an instance of the delegate, add it to SelectionChanged event
                m_ActiveViewEventsSelectionChanged = new ESRI.ArcGIS.Carto.IActiveViewEvents_SelectionChangedEventHandler(OnActiveViewEventsSelectionChanged);
                ((ESRI.ArcGIS.Carto.IActiveViewEvents_Event)(m_mapEventSource)).SelectionChanged += m_ActiveViewEventsSelectionChanged;
            }
            catch (Exception ex)
            {
                string msg = SubsetHelperUI.GetFullExceptionMessage(ex);
                MessageBox.Show(msg, "Wire Selection Event");
            }
        }
        private void WireEvents()
        {
            try
            {
                if (m_naWindowEventSource != null)
                {
                    UnWireEvents();
                }

                m_naWindowEventSource = ((m_nax != null) ? m_nax.NAWindow : null) as INAWindow;
                if (m_naWindowEventSource == null)
                {
                    return;
                }

                //Create an instance of the delegate, add it to OnActiveAnalysisChanged event
                m_ActiveAnalysisChanged = new ESRI.ArcGIS.NetworkAnalystUI.INAWindowEvents_OnActiveAnalysisChangedEventHandler(OnActiveAnalysisChanged);
                ((ESRI.ArcGIS.NetworkAnalystUI.INAWindowEvents_Event)(m_naWindowEventSource)).OnActiveAnalysisChanged += m_ActiveAnalysisChanged;

                WireSelectionEvent();
                WireGraphicsEvents();
            }
            catch (Exception ex)
            {
                string msg = SubsetHelperUI.GetFullExceptionMessage(ex);
                MessageBox.Show(msg, "Wire Events");
            }
        }
        private void WireGraphicsEvents()
        {
            try
            {
                if (m_naWindowEventSource == null)
                {
                    return;
                }

                if (m_graphicsEventSource != null)
                {
                    UnWireGraphicsEvents();
                }

                IMap           activeMap     = ActiveMap;
                IGraphicsLayer graphicsLayer = null;
                if (activeMap != null)
                {
                    graphicsLayer = activeMap.BasicGraphicsLayer;
                }

                if (graphicsLayer != null)
                {
                    m_graphicsEventSource = (IGraphicsContainer)graphicsLayer;
                }

                if (m_graphicsEventSource == null)
                {
                    return;
                }

                UpdateGraphicsEIDArrayParameterValues();

                //Create an instance of the delegate, add it to AllElementsDeleted event
                m_AllGraphicsDeleted = new ESRI.ArcGIS.Carto.IGraphicsContainerEvents_AllElementsDeletedEventHandler(OnAllGraphicsDeleted);
                ((ESRI.ArcGIS.Carto.IGraphicsContainerEvents_Event)(m_graphicsEventSource)).AllElementsDeleted += m_AllGraphicsDeleted;

                //Create an instance of the delegate, add it to ElementAdded event
                m_GraphicAdded = new ESRI.ArcGIS.Carto.IGraphicsContainerEvents_ElementAddedEventHandler(OnGraphicAdded);
                ((ESRI.ArcGIS.Carto.IGraphicsContainerEvents_Event)(m_graphicsEventSource)).ElementAdded += m_GraphicAdded;

                //Create an instance of the delegate, add it to ElementDeleted event
                m_GraphicDeleted = new ESRI.ArcGIS.Carto.IGraphicsContainerEvents_ElementDeletedEventHandler(OnGraphicDeleted);
                ((ESRI.ArcGIS.Carto.IGraphicsContainerEvents_Event)(m_graphicsEventSource)).ElementDeleted += m_GraphicDeleted;

                //Create an instance of the delegate, add it to ElementsAdded event
                m_GraphicsAdded = new ESRI.ArcGIS.Carto.IGraphicsContainerEvents_ElementsAddedEventHandler(OnGraphicsAdded);
                ((ESRI.ArcGIS.Carto.IGraphicsContainerEvents_Event)(m_graphicsEventSource)).ElementsAdded += m_GraphicsAdded;

                //Create an instance of the delegate, add it to ElementUpdated event
                m_GraphicUpdated = new ESRI.ArcGIS.Carto.IGraphicsContainerEvents_ElementUpdatedEventHandler(OnGraphicUpdated);
                ((ESRI.ArcGIS.Carto.IGraphicsContainerEvents_Event)(m_graphicsEventSource)).ElementUpdated += m_GraphicUpdated;
            }
            catch (Exception ex)
            {
                string msg = SubsetHelperUI.GetFullExceptionMessage(ex);
                MessageBox.Show(msg, "Wire Graphics Events");
            }
        }
        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (hook == null)
            {
                return;
            }

            m_application = hook as IApplication;
            m_nax         = SubsetHelperUI.GetNAXConfiguration(m_application) as INetworkAnalystExtension;
        }
        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (hook == null)
            {
                return;
            }

            m_application = hook as IApplication;

            m_nax = null;
            m_naWindowEventSource = null;
            m_mapEventSource      = null;
            m_graphicsEventSource = null;

            m_nax = SubsetHelperUI.GetNAXConfiguration(m_application) as INetworkAnalystExtension;
        }
        private void UnWireEvents()
        {
            try
            {
                if (m_naWindowEventSource == null)
                {
                    return;
                }

                UnWireSelectionEvent();
                UnWireGraphicsEvents();

                ((ESRI.ArcGIS.NetworkAnalystUI.INAWindowEvents_Event)(m_naWindowEventSource)).OnActiveAnalysisChanged -= m_ActiveAnalysisChanged;
                m_naWindowEventSource = null;
            }
            catch (Exception ex)
            {
                string msg = SubsetHelperUI.GetFullExceptionMessage(ex);
                MessageBox.Show(msg, "UnWire Events");
            }
        }
        private void UpdateGraphicsEIDArrayParameterValues()
        {
            IGraphicsContainer graphics = ActiveGraphics;

            if (graphics == null)
            {
                return;
            }

            INAWindow       naWindow  = m_nax.NAWindow;
            INALayer        naLayer   = null;
            INAContext      naContext = null;
            INetworkDataset nds       = null;

            naLayer = naWindow.ActiveAnalysis;
            if (naLayer != null)
            {
                naContext = naLayer.Context;
            }

            if (naContext != null)
            {
                nds = naContext.NetworkDataset;
            }

            if (nds == null)
            {
                return;
            }

            string  baseName = SubsetHelperUI.GraphicsEIDArrayBaseName;
            VarType vt       = SubsetHelperUI.GetEIDArrayParameterType();

            List <string> sourceNames    = SubsetHelperUI.FindParameterizedSourceNames(nds, baseName, vt);
            IGeometry     searchGeometry = SubsetHelperUI.GetSearchGeometryFromGraphics(graphics);

            SubsetHelperUI.UpdateEIDArrayParameterValuesFromGeometry(m_nax, searchGeometry, baseName);
        }
        private void UpdateSelectionEIDArrayParameterValues()
        {
            IMap map = ActiveMap;

            if (map == null)
            {
                return;
            }

            INAWindow       naWindow  = m_nax.NAWindow;
            INALayer        naLayer   = null;
            INAContext      naContext = null;
            INetworkDataset nds       = null;

            naLayer = naWindow.ActiveAnalysis;
            if (naLayer != null)
            {
                naContext = naLayer.Context;
            }

            if (naContext != null)
            {
                nds = naContext.NetworkDataset;
            }

            if (nds == null)
            {
                return;
            }

            string  baseName = SubsetHelperUI.SelectionEIDArrayBaseName;
            VarType vt       = SubsetHelperUI.GetEIDArrayParameterType();

            List <string> sourceNames = SubsetHelperUI.FindParameterizedSourceNames(nds, baseName, vt);
            Dictionary <string, ILongArray> oidArraysBySourceName = SubsetHelperUI.GetOIDArraysBySourceNameFromMapSelection(map, sourceNames);

            SubsetHelperUI.UpdateEIDArrayParameterValuesFromOIDArrays(m_nax, oidArraysBySourceName, baseName);
        }
        public static void ClearEIDArrayParameterValues(INetworkAnalystExtension nax, string baseName)
        {
            try
            {
                INAWindow       naWindow  = nax.NAWindow;
                INALayer        naLayer   = null;
                INAContext      naContext = null;
                INetworkDataset nds       = null;

                naLayer = naWindow.ActiveAnalysis;
                if (naLayer != null)
                {
                    naContext = naLayer.Context;
                }

                if (naContext != null)
                {
                    nds = naContext.NetworkDataset;
                }

                if (nds == null)
                {
                    return;
                }

                VarType       vt          = SubsetHelperUI.GetEIDArrayParameterType();
                List <string> sourceNames = SubsetHelperUI.FindParameterizedSourceNames(nds, baseName, vt);

                SubsetHelperUI.ClearEIDArrayParameterValues(nax, sourceNames, baseName);
                SubsetHelperUI.PushParameterValuesToNetwork(nax);
            }
            catch (Exception ex)
            {
                string msg = SubsetHelperUI.GetFullExceptionMessage(ex);
                MessageBox.Show(msg, "Clear Network Element Array Parameters");
            }
        }