We recommend using Visual Studio 2017
This documentation is archived and is not being maintained.

TextPoint.CodeElement Property

Returns the code element at the TextPoint location.

Namespace:  EnvDTE
Assembly:  EnvDTE (in EnvDTE.dll)

CodeElement this[
	vsCMElement Scope
] { get; }


Type: EnvDTE.vsCMElement
Required. A vsCMElement constant indicating the code element with the specified kind that contains the editor location.

Property Value

Type: EnvDTE.CodeElement
A CodeElement object.

If the specified code element type is not at the TextPoint location, then CodeElement returns Nothing. CodeElement is a shortcut for TextPoint.Parent.Parent.ProjectItem.CodeModel.CodeElementFromPoint(TextPoint, <scope>).

public void CodeElementExample(DTE2 dte)
    // Before running this example, open a code document from a project
    // and place the insertion point anywhere inside the source code.
        TextSelection sel = 
        TextPoint pnt = (TextPoint)sel.ActivePoint;

        // Discover every code element containing the insertion point.
        string elems = "";
        vsCMElement scopes = 0;

        foreach (vsCMElement scope in Enum.GetValues(scopes.GetType()))
            CodeElement elem = pnt.get_CodeElement(scope);

            if (elem != null)
                elems += elem.Name + 
                    " (" + scope.ToString() + ")\n";

            "The following elements contain the insertion point:\n\n" 
            + elems);
    catch (Exception ex)