This documentation is archived and is not being maintained.

Text Property (General Extensibility)

Visual Studio .NET 2003

Sets or returns the selected text.

[Visual Basic .NET]
Public Property Text() As String
[Visual Basic 6]
Property Get Text() As String
Property Let Text( _
   ByVal NewValue As String _
) 
[C++]
HRESULT __stdcall get_Text(
   /* [out, retval] */ BSTR* retVal
);
HRESULT __stdcall put_Text(
   /* [in] */ BSTR NewValue
);
[C#]
string Text {get; set;}
[JScript .NET]
public function get Text() : String
public function set Text(
   NewValue : String
)

Parameters

NewValue
Required. A string representing the text to insert.

Remarks

The Text property is the default property for the StatusBar and TextSelection objects.

When the Text property is read, the text in the selected text is returned, and the selection itself is unchanged.

When the Text property is set, the value of Text is inserted in front of the selected text, and then collapsed, similar to what happens when text is pasted into a document. Note that this property behaves just as when typing when the editor is in insert (that is, non-overtype) mode. Any text after the 128th character is truncated.

If the Mode Property of the TextSelection Object is set to Column, then setting the Text property generates an error.

Example

Sub TextExample()
    ' Before running this example, open a text document.
    Dim objSel As TextSelection = DTE.ActiveDocument.Selection
    If objSel.IsEmpty Then
        ' If there is no text selected, swap the words before and after 
        ' the insertion point. We begin by selecting the word before the 
        ' insertion point.
        objSel.WordLeft(True)
        If Not objSel.IsEmpty Then
            ' We can continue only if the selection was not already at the 
            ' beginning of the document.
            Dim strBefore As String = objSel.Text

            ' The text is saved in strBefore; now delete it and move past 
            ' the following word.
            objSel.Delete()
            objSel.WordRight(True)
            If objSel.Text.StartsWith(" ") Or objSel.Text.StartsWith(Microsoft.VisualBasic.ControlChars.Tab) Then
                ' The previous call to WordRight may have skipped some 
                ' white space instead of an actual word. In that case, we 
                ' should call it again.
                objSel.WordRight(True)
            End If

            ' Insert the new text at the end of the selection.
            objSel.Insert(strBefore, vsInsertFlags.vsInsertFlagsInsertAtEnd)
        End If
    Else
        ' If some text is selected, replace the following word with the 
        ' selected text.
        Dim strSelected As String = objSel.Text

        objSel.MoveToPoint(objSel.BottomPoint)
        objSel.WordRight(True)
        If objSel.Text.StartsWith(" ") Or objSel.Text.StartsWith(Microsoft.VisualBasic.ControlChars.Tab) Then
            ' The previous call to WordRight may have skipped some 
            ' white space instead of an actual word. In that case, we 
            ' should call it again.
            objSel.WordRight(True)
        End If

        ' Insert the text, overwriting the existing text and leaving the 
        ' selection containing the inserted text.
        objSel.Insert(strSelected, vsInsertFlags.vsInsertFlagsContainNewText)
    End If
End Sub

See Also

Applies To: StatusBar Object | TextSelection Object

Show: