Document.Selection Property

 

Gets an object representing the current selection on the Document.

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

object Selection { get; }

Property Value

Type: System.Object

An object representing the current selection on the Document.

Selection returns the current selection in the active window associated with this document (or the topmost window of the document if none is active).

This example works only in Visual Studio .NET 2003. For more information, see .a0b479e4-3c83-4407-986c-1d7353d6a406

public void SelectionExample(DTE dte)
{
    // NOTE: This example requires a reference to the 
    //       VSLangProj namespace.

    // Create a new solution.
    Solution soln = dte.Solution;
    string solnName = "NewSolution.sln";
    string tempPath = System.IO.Path.GetTempPath();
    soln.Create(tempPath, solnName);

    // Create a new C# Console Application project.
    string templatePath = 
        dte.Solution.get_TemplatePath(PrjKind.prjKindCSharpProject);
    templatePath += "CSharpConsole.vsz";
    string projName = "Project1";
    soln.AddFromTemplate(templatePath, tempPath + projName, 
        projName, false);
    Project proj = soln.Item(1);

    // Add a comment to Class1.cs.
    ProjectItem item = proj.ProjectItems.Item("Class1.cs");
    TextSelection sel = (TextSelection)item.Document.Selection;

    sel.StartOfDocument(false);
    sel.NewLine(1);
    sel.LineUp(false, 1);
    sel.Text = "// New comment\n";
}
Return to top
Show: