[GIS] Changing Names in TOC Doesn’t Update Legend Properly

add-inarcmaparcobjectslegend

This is related to an ArcMap add-in I coded up this past week.

I'm trying to rename a layer in ArcMap and have the legend refresh properly. The renaming part is working fine, however I cannot get the legend in the layout to update properly.

I call CurrentContents.Refresh() to refresh the name in the table of contents, which works.

If I search through the map surrounds and call .Refresh() on the legend, then do an ActiveView.PartialRefresh() on it, the legend gets all out of whack. This is why I am trying to simply trigger the same event as when the user manually changes the name of a layer.

The API documentation seems to say that MxDocument.UpdateContents() will trigger the events needed to refresh the legend. However, this does not seem to be the case.
In addition to the above methods I have tried ActiveView.ContentsChanged() to no avail.

Edit: I am writing this in C# but can read VB if you feel more comfortable writing in that.

Any suggestions are welcome.

Thanks,

Mat

Best Answer

I believe you have to refresh via IMapSurround. See code below.

            Dim pMxDoc As IMxDocument
            Dim pPageLayout As IPageLayout
            Dim pGC As IGraphicsContainer
            Dim pElem As IElement
            Dim pMSF As IMapSurroundFrame
            Dim pMS As IMapSurround

            Set pMxDoc = ThisDocument
            Set pPageLayout = pMxDoc.PageLayout
            Set pGC = pPageLayout
            pGC.Reset

            Set pElem = pGC.Next
                Do Until pElem Is Nothing
                If TypeOf pElem Is IMapSurroundFrame Then
                    Set pMSF = pElem
                    Set pMS = pMSF.MapSurround
                pMS.Refresh
                pMxDoc.ActiveView.Refresh

                End If
                Set pElem = pGC.Next

                Loop