//this method is executed when the user clicks the clear vertices button
        private void btnClearVertices_Click(object sender, EventArgs e)
        {
            try
            {
                //show busy mouse
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;

                //get access to the graphics layer, in order to delete the vertices
                ICompositeGraphicsLayer2 pCompositeGraphicsLayer2 = pMap.BasicGraphicsLayer as ICompositeGraphicsLayer2;
                ICompositeLayer          pCompositeLayer          = pCompositeGraphicsLayer2 as ICompositeLayer;
                ILayer pLayer = null;

                for (int i = 0; i < pCompositeLayer.Count; i++)
                {
                    pLayer = pCompositeLayer.get_Layer(i);
                    if (pLayer.Name == "MissingVertices")
                    {
                        pCompositeGraphicsLayer2.DeleteLayer("MissingVertices");
                        break;
                    }
                }

                //clear the x,y report list box
                lstMissingVertices.Items.Clear();

                //refresh the map
                pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
                pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Message: " + Environment.NewLine + ex.Message + Environment.NewLine + Environment.NewLine +
                                "Error Source: " + Environment.NewLine + ex.Source + Environment.NewLine + Environment.NewLine +
                                "Error Location:" + Environment.NewLine + ex.StackTrace,
                                "AGRC Custom Tools ArcMap Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            try
            {
                //get access to the document and the active view
                pMxDocument = (IMxDocument)clsAgrcArcMapExtension.m_application.Document;
                pMap        = pMxDocument.FocusMap;
                pActiveView = pMxDocument.ActiveView;                  //pActiveView = (IActiveView)pMap;

                //get the map's graphics layer
                ICompositeGraphicsLayer2 pComGraphicsLayer = pMap.BasicGraphicsLayer as ICompositeGraphicsLayer2;
                ICompositeLayer          pCompositeLayer   = pComGraphicsLayer as ICompositeLayer;
                ILayer pLayer;

                //loop through all graphic layers in the map and check for the 'PolyVertices' layer, if found, delete it, in order to start fresh
                for (int i = 0; i < pCompositeLayer.Count; i++)
                {
                    pLayer = pCompositeLayer.get_Layer(i);
                    if (pLayer.Name == "PolyVertices")
                    {
                        pComGraphicsLayer.DeleteLayer("PolyVertices");
                        break;
                    }
                }


                if (bolVerticesOn == false)
                {
                    IGraphicsLayer pGraphicsLayer = pComGraphicsLayer.AddLayer("PolyVertices", null);
                    pMap.ActiveGraphicsLayer = (ILayer)pGraphicsLayer;
                    IGraphicsContainer pGraphicsContainer = pComGraphicsLayer.FindLayer("PolyVertices") as IGraphicsContainer;


                    //make sure the user has selected a polygon or polyline layer
                    if (pMxDocument.SelectedLayer == null)
                    {
                        MessageBox.Show("Please select a layer.", "Select Layer", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    if (!(pMxDocument.SelectedLayer is IFeatureLayer))
                    {
                        MessageBox.Show("Please select a polygon or line layer.", "Polygon or Line", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    //cast the selected layer as a feature layer
                    IGeoFeatureLayer pGFlayer = (IGeoFeatureLayer)pMxDocument.SelectedLayer;

                    //check if the feaure layer is a polygon or line layer
                    if (pGFlayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPolygon & pGFlayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPolyline)
                    {
                        MessageBox.Show("Please select a polygon or line layer.", "Polygon or Line", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }


                    //setup marker symbol
                    ISimpleMarkerSymbol pSimpleMarker = new SimpleMarkerSymbol();
                    ISymbol             pSymbolMarker = (ISymbol)pSimpleMarker;
                    IRgbColor           pRgbColor     = new ESRI.ArcGIS.Display.RgbColorClass();
                    pRgbColor.Red       = 223;
                    pRgbColor.Green     = 155;
                    pRgbColor.Blue      = 255;
                    pSimpleMarker.Color = pRgbColor;
                    pSimpleMarker.Style = esriSimpleMarkerStyle.esriSMSDiamond;
                    pSimpleMarker.Size  = 8;

                    //setup line symbol
                    ISimpleLineSymbol pSimpleLineSymbol = new SimpleLineSymbol();
                    ISymbol           pSymbolLine       = (ISymbol)pSimpleLineSymbol;
                    pRgbColor               = new ESRI.ArcGIS.Display.RgbColor();
                    pRgbColor.Red           = 0;
                    pRgbColor.Green         = 255;
                    pRgbColor.Blue          = 0;
                    pSimpleLineSymbol.Color = pRgbColor;
                    pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
                    pSimpleLineSymbol.Width = 1;

                    //setup simplefill symbol
                    ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbol();
                    ISymbol           pSymbolPolygon    = (ISymbol)pSimpleFillSymbol;
                    pRgbColor               = new ESRI.ArcGIS.Display.RgbColor();
                    pRgbColor.Red           = 0;
                    pRgbColor.Green         = 0;
                    pRgbColor.Blue          = 255;
                    pSimpleFillSymbol.Color = pRgbColor;
                    pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;

                    //get all the polygons in the current map extent ina cursor
                    IEnvelope      pMapExtent = pActiveView.Extent;
                    ISpatialFilter pQFilter   = new SpatialFilter();
                    pQFilter.GeometryField = "SHAPE";
                    pQFilter.Geometry      = pMapExtent;
                    pQFilter.SpatialRel    = esriSpatialRelEnum.esriSpatialRelIntersects;
                    IFeatureCursor pFCursor = pGFlayer.Search(pQFilter, true);


                    //draw each polygon and then each polygon's point collection
                    IFeature  pFeature = pFCursor.NextFeature();
                    IGeometry pGeometry;

                    while (pFeature != null)
                    {
                        pGeometry = pFeature.Shape;
                        //draw the polygon
                        //draw each vertex on the polygon
                        IPointCollection pPointCollection = pGeometry as IPointCollection;
                        for (int i = 0; i < pPointCollection.PointCount; i++)
                        {
                            IGeometry pPtGeom  = pPointCollection.get_Point(i);
                            IElement  pElement = new MarkerElement();
                            pElement.Geometry = pPtGeom;
                            IMarkerElement pMarkerElement = pElement as IMarkerElement;
                            pMarkerElement.Symbol = pSimpleMarker;
                            pGraphicsContainer.AddElement(pElement, 0);
                        }
                        pFeature = pFCursor.NextFeature();
                    }

                    bolVerticesOn = true;
                }
                else                 //if (bolVerticesOn == true)
                {
                    bolVerticesOn = false;
                }

                //refresh the map
                pActiveView.Refresh();
                pActiveView.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Message: " + Environment.NewLine + ex.Message + Environment.NewLine + Environment.NewLine +
                                "Error Source: " + Environment.NewLine + ex.Source + Environment.NewLine + Environment.NewLine +
                                "Error Location:" + Environment.NewLine + ex.StackTrace,
                                "AGRC Custom Tools ArcMap Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        //this method is called when the user clicks the "Compare Vertices" button at the bottom of the form
        private void cmdCompare_Click(object sender, EventArgs e)
        {
            try
            {
                if (cboLayer1.SelectedItem.ToString() == "" || cboLayer2.SelectedItem.ToString() == "" || txtOID1.Text == "" || txtOID2.Text == "") //the double pipe is an or operator that "short circuts", meaning that it will bail out early if the condition is true, whereas the single pipe evaluates all conditions
                {
                    MessageBox.Show("Choose polygon layer, or specify OBJECTID.", "Verify Selections...", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                //show busy mouse
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;

                IFeatureLayer pFeatureLayer1 = null;
                IFeatureLayer pFeatureLayer2 = null;

                IPointCollection pPtnCollMissingVertices = new MultipointClass(); //point collection for missing vertices and used for graphics layer
                IPoint           pPointMissing           = new PointClass();      //used for graphics layer

                //get access to polygon layer 1
                for (int i = 0; i < pMap.LayerCount; i++)
                {
                    if (pMap.get_Layer(i) is IFeatureLayer)
                    {
                        if (pMap.get_Layer(i).Name.ToString() == cboLayer1.SelectedItem.ToString())
                        {
                            pFeatureLayer1 = pMap.get_Layer(i) as IFeatureLayer;
                            break;
                        }
                    }
                }

                //get access to polygon layer 2
                for (int i = 0; i < pMap.LayerCount; i++)
                {
                    if (pMap.get_Layer(i) is IFeatureLayer)
                    {
                        if (pMap.get_Layer(i).Name.ToString() == cboLayer2.SelectedItem.ToString())
                        {
                            pFeatureLayer2 = pMap.get_Layer(i) as IFeatureLayer;
                            break;
                        }
                    }
                }

                //clear the x,y report list box
                lstMissingVertices.Items.Clear();


                //get vertices for feature 1
                //set up query filter for feature 1
                IQueryFilter pQueryFilter = new QueryFilter();
                pQueryFilter.WhereClause = "OBJECTID = " + txtOID1.Text;

                //set up feature cursor for feature 1
                IFeatureCursor pFeatureCursor = pFeatureLayer1.Search(pQueryFilter, false);

                //get feature 1
                IFeature pFeature1 = pFeatureCursor.NextFeature();

                //if no objectid is found
                if (pFeature1 == null)
                {
                    MessageBox.Show("The provided OBJECTID was not found for layer: " + cboLayer1.SelectedItem, "ObjectID Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                //get feature 1 as a polygon
                IPolygon pPolygon1 = pFeature1.Shape as IPolygon;

                //get the point collection for feature 1
                IPointCollection pPointColl1 = pPolygon1 as IPointCollection;


                //get vertices for feature 2
                //set up query filter for feature 2
                pQueryFilter             = new QueryFilter();
                pQueryFilter.WhereClause = "OBJECTID = " + txtOID2.Text;

                //set up feature cursor for feature 2
                pFeatureCursor = null; //reuse the feature cursor from above
                pFeatureCursor = pFeatureLayer2.Search(pQueryFilter, false);

                //get feature 2
                IFeature pFeature2 = pFeatureCursor.NextFeature();

                //if no objectid is found
                if (pFeature2 == null)
                {
                    MessageBox.Show("The provided OBJECTID was not found for layer: " + cboLayer1.SelectedItem, "ObjectID Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                //get feature 1 as a polygon
                IPolygon pPolygon2 = pFeature2.Shape as IPolygon;

                //get the point collection for feature 1
                IPointCollection pPointColl2 = pPolygon2 as IPointCollection;


                //loop through point collection and check for missing vertices
                IPoint  pPoint1;
                IPoint  pPoint2;
                Boolean blnFound;


                //loop through each point in the first collection and check for missing
                for (int i = 0; i < pPointColl1.PointCount; i++)
                {
                    pPoint1 = pPointColl1.get_Point(i);

                    //loop through all the points in the second collection and check for the point from the above collection
                    blnFound = false;
                    for (int j = 0; j < pPointColl2.PointCount; j++)
                    {
                        pPoint2 = pPointColl2.get_Point(j);

                        //check for overlapping points
                        if (pPoint1.X == pPoint2.X && pPoint1.Y == pPoint2.Y)
                        {
                            blnFound = true;
                        }
                    }

                    //if no overlapping points were found, report the missing x,y location to the listbox
                    if (blnFound == false)
                    {
                        lstMissingVertices.Items.Add(pPoint1.X + " " + pPoint1.Y + " " + " Feet" + " (missing from polygon 2)");

                        //add the missing point to a new collection for display on map
                        pPtnCollMissingVertices.AddPoint(pPoint1);
                    }
                }


                //loop through each point in the first collection and check for missing
                for (int i = 0; i < pPointColl2.PointCount; i++)
                {
                    pPoint2 = pPointColl2.get_Point(i);

                    //loop through all the points in the second collection and check for the point from the above collection
                    blnFound = false;
                    for (int j = 0; j < pPointColl1.PointCount; j++)
                    {
                        pPoint1 = pPointColl1.get_Point(j);

                        //check for overlapping points
                        if (pPoint2.X == pPoint1.X && pPoint2.Y == pPoint1.Y)
                        {
                            blnFound = true;
                        }
                    }

                    //if no overlapping points were found, report the missing x,y location to the listbox
                    if (blnFound == false)
                    {
                        lstMissingVertices.Items.Add(pPoint2.X + " " + pPoint2.Y + " " + " Feet" + " (missing from polygon 1)");

                        //add the missing point to a new collection for display on map
                        pPtnCollMissingVertices.AddPoint(pPoint2);
                    }
                }


                //if no missing vertices were found, inform the user
                if (lstMissingVertices.Items.Count < 1)
                {
                    lstMissingVertices.Items.Add("No Missing Vertices Found");
                }


                //display vertices if checkbox is checked
                if (chkDisplayVertices.Checked == true)
                {
                    //show busy mouse
                    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;

                    //set up the graphics layer for the display of vertices
                    ICompositeGraphicsLayer2 pCompositeGraphicLayer = pMap.BasicGraphicsLayer as ICompositeGraphicsLayer2;
                    ICompositeLayer          pCompositeLayer        = pCompositeGraphicLayer as ICompositeLayer;
                    ILayer             pLayer;
                    IGraphicsLayer     pGraphicsLayer;
                    IGraphicsContainer pGraphicsContainer;

                    //loop through the graphics layer and check for the missing vertices layer.  if found, delete it
                    for (int i = 0; i < pCompositeLayer.Count; i++)
                    {
                        pLayer = pCompositeLayer.Layer[i];
                        if (pLayer.Name == "MissingVertices")
                        {
                            pCompositeGraphicLayer.DeleteLayer("MissingVertices");
                            break;
                        }
                    }


                    //set up a new 'missing vertices' graphics layer
                    pGraphicsLayer           = pCompositeGraphicLayer.AddLayer("MissingVertices", null);
                    pMap.ActiveGraphicsLayer = pGraphicsLayer as ILayer;
                    pGraphicsContainer       = pCompositeGraphicLayer.FindLayer("MissingVertices") as IGraphicsContainer;

                    //define the color
                    ESRI.ArcGIS.Display.IRgbColor rgbColorCls = new ESRI.ArcGIS.Display.RgbColorClass();
                    rgbColorCls.Red   = 250;
                    rgbColorCls.Green = 0;
                    rgbColorCls.Blue  = 0;

                    //define the font
                    stdole.IFontDisp stdFontCls = new stdole.StdFontClass() as stdole.IFontDisp;
                    stdFontCls.Name = "ESRI Surveyor Marker"; //this one has a thin-line hollow circle
                    stdFontCls.Name = "ESRI Surveyor";        //this one has a bold-line hollow circle
                    stdFontCls.Size = 16;

                    //set the character marker symbol's properties
                    ESRI.ArcGIS.Display.ICharacterMarkerSymbol charMarkerSymb = new ESRI.ArcGIS.Display.CharacterMarkerSymbolClass();
                    charMarkerSymb.Angle          = 0;
                    charMarkerSymb.CharacterIndex = 47;
                    charMarkerSymb.Color          = rgbColorCls;
                    charMarkerSymb.Font           = stdFontCls;
                    charMarkerSymb.Size           = 16;
                    charMarkerSymb.XOffset        = 0;
                    charMarkerSymb.YOffset        = 0;

                    //place the graphics on the map
                    for (int i = 0; i < pPtnCollMissingVertices.PointCount; i++)
                    {
                        pPointMissing = pPtnCollMissingVertices.Point[i];
                        IElement pElement = new MarkerElement();
                        pElement.Geometry = pPointMissing;
                        IMarkerElement pMarkerElement = pElement as IMarkerElement;
                        pMarkerElement.Symbol = charMarkerSymb;
                        pGraphicsContainer.AddElement(pElement, 0);
                    }

                    //refresh the map, in order to see the newly added graphics
                    pActiveView.Refresh();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Message: " + Environment.NewLine + ex.Message + Environment.NewLine + Environment.NewLine +
                                "Error Source: " + Environment.NewLine + ex.Source + Environment.NewLine + Environment.NewLine +
                                "Error Location:" + Environment.NewLine + ex.StackTrace,
                                "AGRC Custom Tools ArcMap Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }