TextSelection::IsEmpty Property
Visual Studio 2015
Gets whether the anchor point is equal to the active point.
Assembly: EnvDTE (in EnvDTE.dll)
Property Value
Type: System::BooleanA Boolean value indicating True if the anchor point is equal to the active point, False if not.
IsEmpty returns whether the text selection's anchor and active ends are at the same location in the text document.
Sub IsEmptyExample() ' 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 ' text selection. 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
Show: