Visual Studio Automation and Extensibility Reference
FileCodeModel..::.CodeElementFromPoint Method

Returns a code element at a specific location in a source file.

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

Visual Basic (Declaration)
Function CodeElementFromPoint ( _
    Point As TextPoint, _
    Scope As vsCMElement _
) As CodeElement
Visual Basic (Usage)
Dim instance As FileCodeModel
Dim Point As TextPoint
Dim Scope As vsCMElement
Dim returnValue As CodeElement

returnValue = instance.CodeElementFromPoint(Point, _
    Scope)
C#
CodeElement CodeElementFromPoint(
    TextPoint Point,
    vsCMElement Scope
)
Visual C++
CodeElement^ CodeElementFromPoint(
    TextPoint^ Point, 
    vsCMElement Scope
)
JScript
function CodeElementFromPoint(
    Point : TextPoint, 
    Scope : vsCMElement
) : CodeElement

Parameters

Point
Type: EnvDTE..::.TextPoint
Required. A TextPoint object representing the editor location for which you want a code element.
Scope
Type: EnvDTE..::.vsCMElement
Required. A vsCMElement value representing the code element of the specified type that contains the editor location.

Return Value

Type: EnvDTE..::.CodeElement
A CodeElement object.
Remarks

CodeElementFromPoint returns the code element associated with the TextPoint based on the specified scope or granularity of containment. If no code element of the specified type contains the editor location, then this method fails.

Examples

Visual Basic
Sub CodeElementFromPointExample(ByVal dte As DTE2)

    ' Before running this example, open a code document from a project
    ' and place the insertion point anywhere inside the source code.
    Try
        Dim sel As TextSelection = _
            CType(dte.ActiveDocument.Selection, TextSelection)
        Dim pnt As TextPoint = CType(sel.ActivePoint, TextPoint)

        ' Discover every code element containing the insertion point.
        Dim fcm As FileCodeModel = _
            dte.ActiveDocument.ProjectItem.FileCodeModel
        Dim elems As String
        Dim elem As CodeElement
        Dim scope As vsCMElement
        For Each scope In [Enum].GetValues(scope.GetType())
            elem = fcm.CodeElementFromPoint(pnt, scope)
            If IsNothing(elem) = False Then
                elems &= elem.Name & " (" & scope.ToString() & ")" & _
                    vbCrLf
            End If
        Next

        MsgBox("The following elements contain the insertion point:" _
            & vbCrLf & vbCrLf & elems)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub
C#
public void CodeElementFromPointExample(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.
        FileCodeModel fcm = 
            dte.ActiveDocument.ProjectItem.FileCodeModel;
        string elems = "";
        vsCMElement scopes = 0;

        foreach (vsCMElement scope in Enum.GetValues(scopes.GetType()))
        {
            CodeElement elem = fcm.CodeElementFromPoint(pnt, 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);
    }
}
Permissions

See Also

Reference

Other Resources

Page view tracker