Document.Selection 属性

获取表示 Document 上当前选定内容的对象。

命名空间:  EnvDTE
程序集:  EnvDTE(在 EnvDTE.dll 中)

语法

声明
ReadOnly Property Selection As Object
    Get
Object Selection { get; }
property Object^ Selection {
    Object^ get ();
}
abstract Selection : Object
function get Selection () : Object

属性值

类型:System.Object
表示 Document 上当前选定内容的对象。

备注

Selection 返回与此文档关联的活动窗口中的当前选定内容(如果没有活动窗口,则返回该文档最顶层窗口的当前选定内容)。

示例

此示例仅适用于 Visual Studio .NET 2003。 有关更多信息,请参见 How to: Migrate Code that Creates Projects by Using Templates

Sub SelectionExample(ByVal dte As DTE)

    ' NOTE: This example requires a reference to the 
    '       VSLangProj namespace.

    ' Create a new solution.
    Dim soln As Solution = dte.Solution
    Dim solnName As String = "NewSolution.sln"
    Dim tempPath As String = System.IO.Path.GetTempPath()
    soln.Create(tempPath, solnName)

    ' Create a new Visual Basic Console Application project.
    Dim templatePath As String = 
        dte.Solution.TemplatePath(PrjKind.prjKindVBProject)
    templatePath &= "ConsoleApplication.vsz"
    Dim projName As String = "NewProject"
    soln.AddFromTemplate(templatePath, tempPath & projName, projName)
    Dim proj As Project = soln.Item(1)

    ' Add a comment to Module1.vb.
    Dim item As ProjectItem = proj.ProjectItems.Item("Module1.vb")
    Dim sel As TextSelection = CType(item.Document.Selection, 
        TextSelection)

    sel.StartOfDocument()
    sel.NewLine()
    sel.LineUp()
    sel.Text = "' New comment" & vbCrLf

End Sub
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";
}

.NET Framework 安全性

请参见

参考

Document 接口

EnvDTE 命名空间

其他资源

如何:编译和运行自动化对象模型代码示例