How do I retrieve all of the code from a Visual Studio .NET Code Window, without disturbing any text selection that may exist in the window? The answer is, "use the EditPoint Object." Using only the TextSelection object of the ActiveDocument object will disturb the current selection of text if any exists.
The Text Editor in the Visual Studio .NET IDE has an edit buffer that shadows the visible text window. The visible text window is manipulated by using the TextSeletion object. The edit buffer, that operates behind the scene, is manipulated by the use of the EditPoint object.
The following line of code is used to call the method that returns the entire contents of the active code window.
| Dim s As String = GetAllCodeFromActiveWindow() |
| Public Function GetAllCodeFromActiveWindow() As String ' Returns the code of the whole window, without destroying ' the selected text in the TextSelection object. Try Dim ts As TextSelection = oVB.ActiveDocument.Selection Dim ep As EditPoint = ts.ActivePoint.CreateEditPoint ep.EndOfDocument() Dim s As String = ep.GetLines(1, ep.Line) Return s Catch ex As System.Exception ' StructuredErrorHandler(ex) End Try End Function |