Help with finding TextElement in PageLayout?

1183
11
06-09-2011 07:51 AM
AnnCrystal
New Contributor II
Hi Guys:
This may sound simple to you. But I am stuck. I am trying to find whether there is a disclaimer text in the pagelayout. If not, I want to add one. Here is the code:

if (enumElement != null)
                    {
                    findElement = enumElement.Next();
                    while (findElement != null)
                        {
                        if (findElement is IParagraphTextElement)
                            {
                            findTextElement = findElement as ITextElement;
                          
                            if (findTextElement.Text.EndsWith("></CLR>"))
                                {
                                MessageBox.Show("The Disclaimer Text, which autoupdates the time and date, is already placed in the map.");
                                }
                            else
                                {
                                graphicsContainer.AddElement(element, 0);
                                graphicsContainerSelect.UnselectAllElements();
                                activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
                                }
                               
                            }
                        findElement = enumElement.Next();
                        }

                    }


My else part is not working. Where am I wrong? How can I write that if there are no ParagraphtextElements, then add my text?

Thanks in advance for your help.
Ann
0 Kudos
11 Replies
JoelFerreyra
New Contributor II
Ann, Jeff, please could put code in Visual Basic Net, it happens that I can not do the equivalent = (.. I'm still very puppy programming on these issues.

Greetings!
0 Kudos
MichaelRobb
Occasional Contributor III
Ann, Jeff, please could put code in Visual Basic Net, it happens that I can not do the equivalent = (.. I'm still very puppy programming on these issues.

Greetings!


You have to, of course, declair ArcMap.

 Public Sub PerformTest()
        Const myParaTextID As String = "MyDisclaimerTextID"
        Dim paraText As String = "Sample disclaimer paragraph text"

        Dim graphicsContainer As ESRI.ArcGIS.Carto.IGraphicsContainer = TryCast(ArcMap.Document.PageLayout, ESRI.ArcGIS.Carto.IGraphicsContainer)
        Dim element As ESRI.ArcGIS.Carto.IElement
        Dim elementProperties As ESRI.ArcGIS.Carto.IElementProperties


        ' Check if the disclaimer text element already exists in layout
        graphicsContainer.Reset()
        element = graphicsContainer.Next()
        While element IsNot Nothing
            If TypeOf element Is ESRI.ArcGIS.Carto.IParagraphTextElement Then
                elementProperties = TryCast(element, ESRI.ArcGIS.Carto.IElementProperties)
                If elementProperties.Name = myParaTextID Then
                    MessageBox.Show("The Disclaimer Text, which autoupdates " & "the time and date, is already placed in the map.")

                    Return
                End If
            End If
            element = graphicsContainer.Next()
        End While


        ' No disclaimer found, so make one

        Dim paraTextElement As ESRI.ArcGIS.Carto.IParagraphTextElement = New ESRI.ArcGIS.Carto.ParagraphTextElementClass()
        Dim textElement As ESRI.ArcGIS.Carto.ITextElement = TryCast(paraTextElement, ESRI.ArcGIS.Carto.ITextElement)
        elementProperties = TryCast(paraTextElement, ESRI.ArcGIS.Carto.IElementProperties)
        element = TryCast(paraTextElement, ESRI.ArcGIS.Carto.IElement)

        ' Set this so it can be found if this method called again
        elementProperties.Name = myParaTextID

        ' Set the actual text to display
        textElement.Text = paraText

        ' Set the size and location of the text box
        Dim envelope As ESRI.ArcGIS.Geometry.IEnvelope = New esri.ArcGIS.Geometry.EnvelopeClass()
        envelope.PutCoords(0, 0, 3, 1)
        element.Geometry = envelope

        ' Add the text box to the layout
        graphicsContainer.AddElement(element, 0)

        ArcMap.Document.ActiveView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGraphics, Nothing, Nothing)
    End Sub
0 Kudos