Export (0) Print
Expand All

TextPoint.CodeElement Property

Returns the code element at the TextPoint location.

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

CodeElement this[
	vsCMElement Scope
] { get; }

Parameters

Scope
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.
    try
    {
        TextSelection sel = 
            (TextSelection)dte.ActiveDocument.Selection;
        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";
        }

        MessageBox.Show(
            "The following elements contain the insertion point:\n\n" 
            + elems);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Community Additions

ADD
Show:
© 2014 Microsoft