I have some code from 9.3 that would check the CurrentContentsView.ContextItem to ensure that the TOC was in Display (or Drawing Order) view. If not it would set it to Display via:

If Not pDoc.CurrentContentsView.ContextItem = 1 Then
    Set pDoc.CurrentContentsView.ContextItem = 1
    pDoc.ContentsView(1).Refresh Nothing
End If
pDoc.ContentsView(1).Refresh Nothing

Now that the TOC is a DockableWindow (which I can access) and there are several IContentsViews, this no longer works. It's surprising that this is impossible to find in the forums and the VBA SDK as as brief and cryptic as could be.

How do I access the ContextItem (which seems stuck at NULL) to set it? Also, is the fact that "currentContentsView" automatically reverts to a lower-case "c" when the object apparently shouldhave an upper-case "C" significant?

Any help would be MUCH appreciated. For some reason, my code runs 3x faster in Display View and I want to automatically set it if the user forgets (or doesn't know).

Oh...and yes, I AM aware of VBA's demise and the existence of Python. Noted.

Thanks in advance.


This add-in code example (not in VBA format) will check contents view and change to display view (List By Drawling Order).

   Protected Overrides Sub OnClick()


         My.ArcMap.Application.CurrentTool = Nothing
    End Sub

    Private Sub ChangeArcMapTOC_ContentsView(ByVal newContentsViewName As String)

        Dim app As IApplication = CType(Hook, IApplication)
        Dim mxdoc As IMxDocument = CType(My.ArcMap.Application.Document, IMxDocument)

        If mxdoc.CurrentContentsView.Name = newContentsViewName Then

            Exit Sub

        End If

        ' loop thru all ContentsViews to see if newContentsViewName exists.

        Dim i As Integer

        Dim pContentView As IContentsView

        Dim bFound As Boolean

        For i = 0 To mxdoc.ContentsViewCount - 1

            pContentView = mxdoc.ContentsView(i)

            If pContentView.Name = newContentsViewName Then

                bFound = True

                Exit For

            End If


        If bFound = False Then

            MessageBox.Show("Contents view '" & newContentsViewName & "' does not exist", "Could not change contents view")

            Exit Sub


            mxdoc.CurrentContentsView = pContentView

        End If


        Exit Sub

    End Sub
| improve this answer | |
  • Hmm...as ususal. – Dan Shaffer Feb 27 '12 at 18:28
  • OK that auto-submit on Enter is killing me! As usual...I missed the simplest possible way to do something! Thanks for getting me pointed back in the right direction. On another note...do I use the ContentsView3 to update the selected button (List by Drawing Order vs. Source)? This script doesn't do that. But it was immensely helpful. Thanks! – Dan Shaffer Feb 27 '12 at 18:31
  • 1
    Interesting...never mind. When inserted into my code and run, the button DOES update after a second or two...maybe I needed a few refreshes or something. Again...many thanks! – Dan Shaffer Feb 27 '12 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.