Cannot display graphic elements through Desktop Automation application

Discussion created by findome on Jan 7, 2013
Latest reply on Jan 8, 2013 by brenth
I'm using the Desktop Automation sample code for C# for ArcGIS 10.1 running on Windows 7. I have a main form with buttons for opening a ArcMap MXD, closing it and displaying number of selected features in the first TOC layer. I also have a button on the main form that creates a simple graphic my sample code I'm trying to display a graphic text element "Hello World" at the centre of the view extent.

The issue I have is that the text element never gets displayed after its created. However after saving and closing the MXD and re-opening it I see the graphic text displayed. Is there anything I'm missing in the code that prevents the graphic element from being displayed after it is created? This is happening to any graphic element I create through custom code.

Below is the sample code:

        private void button3_Click(object sender, EventArgs e)
            IGraphicsContainer graphicsContainer = m_MxDoc.FocusMap as IGraphicsContainer;
            IElement element = new TextElementClass();
            ITextElement textElement = element as ITextElement;
            IActiveView activeView = m_MxDoc.FocusMap as IActiveView;

            //Create a point as the shape of the element.
            IPoint point = new PointClass();
            point.X = activeView.Extent.XMin + (activeView.Extent.Width / 2);
            point.Y = activeView.Extent.YMin + (activeView.Extent.Height / 2);
            element.Geometry = point;
            textElement.Text = "Hello World";
            graphicsContainer.AddElement(element, 0);

            //Flag the new text to invalidate.
            activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

Any help will be appreciated.