The TextSelection Object
The TextSelection object pulls double duty as a representation of the caret in the editor window as well as a representation of the currently selected text. (You can think of the caret as a zero-length selection.) Because there can be only one selection in an editor window, there can be only one TextSelection object per document. Figure 11-3 breaks down a TextSelection into its constituent parts.
Figure 11-3. Anatomy of a TextSelection

As you can see in Figure 11-3, four properties delineate a TextSelection: TopPoint, BottomPoint, AnchorPoint, and ActivePoint. Each of these properties returns a VirtualPoint object from one of the ends of the selected range. The TopPoint and BottomPoint properties always refer to the upper-left and bottom-right of the selection, respectively. The AnchorPoint and ActivePoint properties refer to the equivalent of the start and end points of a mouse-drag selection; for example, the top selection in Figure 11-3 would result from dragging the mouse from the beginning of using Extensibility; to the end of using EnvDTE;. You can determine the orientation of a TextSelection by checking its IsActiveEndGreater property, which returns True when ActivePoint equals BottomPoint. If the orientation isn't to your liking, you can flip it by calling the TextSelection.SwapAnchor method, which exchanges the positions of AnchorPoint and ActivePoint.The TextSelection.IsEmpty property lets you know whether there's a selection, and you can retrieve the selected text from the Text property. If there's no selection, Text always returns an empty string. The converse doesn't hold, however, because Text returns an empty string for a virtual space selection. When a selection spans multiple lines, the TextRanges property holds a collection of TextRange objects, one for each line of the selection.Table 11-6 lists the different ways you can get a TextSelection object.
Property | Applies To |
---|---|
Selection | Document TextDocument TextPane TextWindow Window |
A Comparison of TextSelection and EditPoint
The TextSelection and EditPoint objects offer a bewildering array of editing methods, which are listed in Table 11-7. Looking at the table, you'll see that TextSelection and EditPoint share the majority of their methods and have only a few seemingly minor differences, which makes choosing one over the other akin to choosing between the 52-feature Swiss army knife that comes with scissors and the 52-feature Swiss army knife that comes with a saw. In most circumstances, either knife will do just fineit's only in those particular moments when you need to gather firewood or do a little personal grooming that you suddenly realize that you can't cut down branches with scissors and you can't trim nose hairs with a saw. Using the editing objects is much the same in that you won't know whether you've chosen the right one for the job until it fails you.
Task | Methods in Common | TextSelection Only | EditPoint Only |
---|---|---|---|
Moving the insertion point | CharLeft, CharRight, EndOfDocument, EndOfLine, LineDown, LineUp, MoveToAbsoluteOffset, MoveToLineAndOffset, MoveToPoint, StartOfDocument, StartOfLine, WordLeft, WordRight | Collapse, GoToLine, MoveToDisplayColumn, PageDown, PageUp | |
Finding and retrieving text | FindPattern | FindText | GetLines, GetText |
Selecting text | SelectAll, SelectLine | ||
Modifying text | ChangeCase, Copy, Cut, Delete, DeleteWhitespace, Indent, Insert, InsertFromFile, PadToColumn, Paste, ReplacePattern, SmartFormat, Unindent | DeleteLeft, DestructiveInsert, NewLine, Tabify, Untabify | ReplaceText |
Managing bookmarks | ClearBookmark, NextBookmark, PreviousBookmark, SetBookmark | ||
Miscellaneous | OutlineSection | SwapAnchor | ReadOnly |