What you talkin’ ’bout, class view?

Every once in a while I get mail asking how to find what is selected within the class
view window. For a while I was telling everybody who asked that it was not possible
because there is not an automation model on the class view window. But recently I
stumbled across a way of finding out what is selected through another part of the
object model. This works for C# and VC projects, but not for VB or J# (we will see
why in a minute).

When an item is selected in the class view tool window, and the project that item
is contained within is a C# or VC project, either the code model element or the Project
object for that item is placed into the properties window. Since a System.Object/IDispatch
can be retrieved for what is displayed within the properties window with
the DTE.SelectedItems.SelectionContainer property, you can write code such as shown
in the following macro to inspect the selected item to find out more information about
what is selected:

    Sub "urn:schemas-microsoft-com:office:office" /> classwindow()

        Dim cc As CodeClass

        Dim cn As CodeNamespace

        Dim cp As CodeProperty

        Dim cf As CodeAttribute

        Dim proj As Project

        Dim obj As Object

= DTE.SelectedItems.SelectionContainer.Item(1)


= obj

        Catch ex1 As System.Exception


= obj

            Catch ex2 As System.Exception


= obj

                Catch ex3 As System.Exception


= obj

                    Catch ex4 As System.Exception


= obj

                        Catch ex5 As System.Exception


                        End Try

                    End Try

                End Try

            End Try

        End Try

    End Sub

As I said, this only works for C# and VC projects, but not VB or J# ones. Why? Because
those two languages don’t
push an item into the class view window, and without that item in the properties window,
there is nothing to retrieve.

Comments (3)

  1. Perhaps this isn’t exactly the correct place to ask this, but is there any way to use the use the VCResourceHelper objects when using the IDE for normal C++ projects that aren’t wizard projects? With exception to your book (which is excellent), there aren’t that many good sources on the object models.

    One quick historical question, that I’ve heard asked loads on google, but never seen an answer for, why aren’t project files (.vcproj) correct XML? (i.e. not depending on attribute order)

  2. Craig says:

    While I am not too familiar with the VCResourceHelper, but when you write code for a VS .NET wizard, all the objects available to that wizard are also available in the object model and can be called from Macros and Add-ins. Unless the VC team has some special functionality that they expose only to Wizards, then it should work.

    I am not sure about this one either, I will try to track down some information.


  3. Ady says:


    In VS2005 this option is also no longer valid as even for C# and VC projects no item is placed in the properties window.

    Is there any workaround for VS2005?

    Any help would be appreciated.