AnsweredAssumed Answered

Removing Exisiting Text Element After mxd is Saved and Reopened??

Question asked by douglas.robbie on Aug 23, 2012
Latest reply on Aug 24, 2012 by douglas.robbie
Hi,

I am creating an ArcMap Add-In that has a custom look up for schools, populates a datagridview that when a cell is clicked the map zooms to the selected school and associated features, and text elements with the school name and address are added to the layout view. All this works fine, but if I save the mxd and reopen it and go search for a new school and click on the cell a new element is placed on top of the one that was saved. The element that is added refreshes with the new info each time a new cell is selected, but the ones that were from the previous saved ArcMap session remain. Also of interest, when I select the saved element and delete it, it does not delete the first time. I have to select and delete multiple times (like 2 or 3) to get it to disappear, while deleting a new element is done the first time. Each element property is given a name (see code below), and I would expect to be able to call that element's name and delete it or make its text empty, but I can't delete it with GraphicContainer.DeleteElement() or replace the text with TextElement.Text. I have different variations of this with different results, but I never get the results I expect (deleting the saved element by its name). It is as if the element that is in the mxd when it loads is not really an element that can be accessed using its name. Is there a way around this? Is it a persistence issue? Can I define the persistence onStartUp or onShutdown? Can I "activate" the element that was saved in the mxd somehow before I make the new elements? Or is this a bug? Or did I write this wrong or am I missing something (most likely case)?

#region Populate Layout View Elements
        IPageLayout docPageLayout;
        private static IActiveView view = ArcMap.Document.ActiveView as IActiveView;
        IGraphicsContainer GContainer = view as IGraphicsContainer;


        static TextElement elem = new TextElementClass();
        static IElement elem1 = new TextElementClass();

        IElementProperties elemProp = (IElementProperties)elem;


        private void clearElems(IActiveView docActiveView)
        {
            docPageLayout = new PageLayoutClass();
            docPageLayout = docActiveView as IPageLayout;

            IElementProperties elemProp1 = (IElementProperties)elem1;


            if (docActiveView is IPageLayout)
            {

                docPageLayout = docActiveView as IPageLayout;

                GContainer.Reset();

                IArray array = get_ElemArray(GContainer);
                MessageBox.Show(Convert.ToString(array.Count));
                for (int i = 0; array.Count > i; i++)
                {
                    elem1 = (TextElement)array.get_Element(i);

                    if (elemProp1.Name == "Map_Title")
                    {
                        GContainer.DeleteElement(elem1);
                        return;
                    }
                }
            }
        }

        public IArray get_ElemArray(IGraphicsContainer GContainer)
        {
            IActiveView view = ArcMap.Document.ActiveView as IActiveView;
            IGraphicsContainer graphicsContainer = view as IGraphicsContainer;
            graphicsContainer.Reset();

            IArray array = new ArrayClass();
            elem1 = (TextElement)graphicsContainer.Next();

            while (elem1 != null)
            {
                array.Add(elem1);
                elem1 = (TextElement)graphicsContainer.Next();
            }

            return array;
        }

        private void populateMapTitle(string MapTitle, IActiveView docActiveView)
        {
          
            ITextElement textElem = elem as ITextElement;
            IPoint point = new PointClass();
            docPageLayout = new PageLayoutClass();
            IElementProperties elemProp = (IElementProperties)elem;

            if (docActiveView is IPageLayout)
            {
                docPageLayout = docActiveView as IPageLayout;
                GContainer = docPageLayout as IGraphicsContainer;
                GContainer.Reset();

                point.X = 4.45;
                point.Y = 7.85;

                elem.Geometry = point;

                textElem.Text = MapTitle;

                elemProp.Name = "Map_Title";

                GContainer.AddElement(elem, 0);

                docActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

            }
        }


Thanks,
Robert Douglas

Outcomes