Using EditPoint to Select Code Without Disturbing the Selected TextManipulating Code in the EditBuffer | | How do I get all of the code from a CodeWindow without losing the current TextSelection text? The technique is simple.
The Text Editor in the Visual Studio .NET IDE has Visible Code Windows. Behind each code window is an Edit Buffer. The visible code can be manipulated by using the TextSelection object. However, the Edit Buffer (hidden copy of the code window contents) can be selected, etc., behind the scenes, without disturbing the selection in the visible window, by the use of the EditPoint objects. When the user selects a block of code in the window, that code can be referenced with the following code.
Dim ts As TextSelection = _
appObj.ActiveDocument.Selection
ts.SelectAll()
Dim s As String = ts.Text
|
Now the text window will look as shown in Figure 1.
Figure 1 - TextSelection in Code Window.

I would like to be able to retrieve all of the code in the active code window, without disturbing the TextSelection. The code to accomplish this is shown below.
Dim ts As TextSelection = oVB.ActiveDocument.Selection
' grab the code of the whole window, without destroying
' the selected text in the ts object
Dim ep As EditPoint = ts.ActivePoint.CreateEditPoint
ep.StartOfDocument()
Dim bp As EditPoint = ts.ActivePoint.CreateEditPoint
bp.EndOfDocument()
Dim sAll As String = ep.GetLines(1, bp.Line)
|
The variable "sAll" will now contain all of the code in the active window and the contents of TextSelection (user selected block of code) is still intact.
|