Selecting Text in a Window That is not Open in the IDE | | If you use the CodeModel and FileCodeModel to manipulate your way through a project, How do you open a code window and move to a desired line in the code window?
Assuming that you know the name of the Solution, Project, and Code Window to open and have the line number that you want to move to, you can call the DisplayCodeSelection method, shown below, to perform the desired functionality. The calling sequence is shown below.
Public Sub test()
DisplayCodeSelection("NetCommander\NetCommander\CWindows.vb", 252)
End Sub
|
This method will open the passed window named in the first parameter and move to the line number specified by the second paramater. I have commented the code, at every step, so that no further explanation is necessary. This code is written for an add-in, and assumes that the application object is available in an object named oVB. This code will work in a macro if you simply change all referenes to oVB to DTE.
Public Sub DisplayCodeSelection(ByVal rsWinName As String, _
ByVal riLine As Integer)
' Open the specified window, move to specified line
' and select the line.
' This method can be called by features such as search
' program, project browser, and track changes.
' rsWinName="SlnName\PrjName\modulename.vb"
Dim s As String
Try
' Activate the project explorer
oVB.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate()
' select the desired code window in the explorer
oVB.ActiveWindow.Object.GetItem(rsWinName). _
Select(vsUISelectionType.vsUISelectionTypeSelect)
' ask the IDE to open the code window
oVB.ExecuteCommand("View.ViewCode")
System.Windows.Forms.Application.DoEvents()
' create a TextSelection object so we can select the desired line
Dim objTextDoc As TextSelection = _
oVB.ActiveDocument.Selection
' move to the top of the document
objTextDoc.StartOfDocument()
' now, move to the desired line
objTextDoc.MoveToLineAndOffset(riLine, 1, False)
' move down a line, causing the previous line to be highlighted
objTextDoc.MoveToLineAndOffset(riLine + 1, 1, True)
Catch ex As System.Exception
End Try
End Sub
| |