共用方式為


TextSelection.Text 屬性

設定或取得選取文字。

命名空間:  EnvDTE
組件:  EnvDTE (在 EnvDTE.dll 中)

語法

'宣告
Default Property Text As String
string this { get; set; }
property String^ default {
    String^ get ();
    void set (String^ value);
}
abstract Text : string with get, set
function get Text () : String
function set Text (value : String)

屬性值

型別:System.String
表示所選取文字的字串。

備註

Text 屬性是 TextSelection 物件的預設屬性。

當讀取 Text 屬性時,會傳回選取文字中的文字,而選取範圍本身則不會改變。

當設定 Text 屬性時,Text 的值會插入至選取文字的前面,然後摺疊起來,與文字貼入文件中的情況類似。 請注意,這個屬性的行為就像是在編輯器的插入 (意即 nonovertype) 模式中。 任何超過第 128 個字元的文字將被截斷。

如果 TextSelection 物件的 Mode 屬性設定為 Column,那麼設定 Text 屬性則會產生錯誤。

範例

此範例只適用於 Visual Studio .NET 2003。 如需詳細資訊,請參閱 Migrating Code that Creates Projects by Using Templates

Sub TextExample(ByVal dte As DTE)

    ' NOTE: This examples 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 TextExample(DTE dte)
{
    // NOTE: This examples 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 安全性

請參閱

參考

TextSelection 介面

EnvDTE 命名空間

其他資源

HOW TO:編譯和執行 Automation 物件模型程式碼範例