This document is archived and information here might be outdated.  Recommended version.


Working with map elements (ArcObjects .NET 10.4 SDK)

Working with map elements


About working with map elements

Elements are commonly accessed through the IGraphicsContainer interface implemented by the Map and PageLayout objects. Through this interface, you can add, delete, update, and retrieve individual elements within a Map or PageLayout object. Use the GroupElement object to combine multiple elements into a single unit for manipulation by the user.
IElement is the generic interface implemented by all graphic elements and frames. Most methods that return graphics (various methods and properties of IGraphicsContainer and IGraphicsContainerSelect) return them as generic IElement objects. IElement gives you access to the geometry of the object and contains methods for querying the object and drawing it.
It is your responsibility to determine what type of object is hosting the IElement interface by performing a cast. In C# or VB .NET, you can enumerate and parse the map elements using the following code example:
[C#]
public void CountPolygonGraphicElements(IPageLayout pageLayout)
{
    //The page layout is a graphics container, so it can be enumerated.
    IGraphicsContainer graphicsContainer=(IGraphicsContainer)pageLayout;

    //Reset the container to start from the beginning.
    graphicsContainer.Reset();
    Int32 int32_Counter=0;
    IElement element=graphicsContainer.Next();

    //Loop through the container until the end, denoted by a null value.
    while (element != null)
    {
        //Test the element to see if it's a certain type. This can be ITextElement, IPolygonElement, etc.
        if (element is IPolygonElement)
        {
            //If a match is found, increment the counter.
            int32_Counter=int32_Counter + 1;
        }

        //Step to the next element in the container.
        element=graphicsContainer.Next();
    }

    //Display the results of the count.
    MessageBox.Show("The pagelayout contains " + int32_Counter + 
        " PolygonElement(s)");
}
[VB.NET]
Public Sub CountPolygonGraphicElements(ByVal pageLayout As IPageLayout)
    
    'The page layout is a graphics container, so it can be enumerated.
    Dim graphicsContainer As IGraphicsContainer=CType(pageLayout, IGraphicsContainer)
    
    'Reset the container to start from the beginning.
    graphicsContainer.Reset()
    Dim int32_Counter As Int32=0
    Dim element As IElement=graphicsContainer.Next()
    
    'Loop through the container until the end, denoted by a null value.
    While Not element Is Nothing
        
        'Test the element to see if it's a certain type. This can be ITextElement, IPolygonElement, etc.
        If TypeOf element Is IPolygonElement Then
            'If a match is found, increment the counter.
            int32_Counter=int32_Counter + 1
        End If
        
        'Step to the next element in the container.
        element=graphicsContainer.Next()
        
    End While
    
    'Display the results of the count.
    MessageBox.Show("The pagelayout contains " & int32_Counter & " PolygonElement(s)")
    
End Sub
In the following code example, cast IMap to IGraphicsContainer, then add a text element to the active graphics layer:
[C#]
public void AddTextElement(IMap map)
{
    IGraphicsContainer graphicsContainer=map as IGraphicsContainer;
    IElement element=new TextElementClass();
    ITextElement textElement=element as ITextElement;

    //Create a point as the shape of the element.
    IPoint point=new PointClass();
    point.X=0;
    point.Y=0;
    element.Geometry=point;
    textElement.Text="Hello World";
    graphicsContainer.AddElement(element, 0);

    //Flag the new text to invalidate.
    IActiveView activeView=map as IActiveView;
    activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
[VB.NET]
Public Sub AddTextElement(ByVal map As IMap)
    
    Dim graphicsContainer As IGraphicsContainer=TryCast(map, IGraphicsContainer)
    Dim element As IElement=New TextElementClass()
    Dim textElement As ITextElement=TryCast(element, ITextElement)
    
    'Create a point as the shape of the element.
    Dim point As IPoint=New PointClass()
    point.X=0
    point.Y=0
    element.Geometry=point
    textElement.Text="Hello World"
    graphicsContainer.AddElement(element, 0)
    
    'Flag the new text to invalidate.
    Dim activeView As IActiveView=TryCast(map, IActiveView)
    activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, Nothing, Nothing)
    
End Sub






To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine