How to: Automate an Incremental Search

Visual Studio offers the ability to search through code and text documents incrementally. That is, you can search interactively for one or more characters at a time, as you type them. For more information, see How to: Search a Document Incrementally.

The IncrementalSearch object allows you to programmatically perform incremental searches.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and ExportSettings on the Tools menu. For more information, see Visual Studio Settings.

To create an incremental search pattern

  1. Open the document to search.

  2. Access the IncrementalSearch object through the IncrementalSearch property, which is obtained by DTE.ActiveDocument.ActivePane.Object.ActivePane.

  3. Select a direction in which to search, either forward of the insertion point or backward, by using StartForward or StartBackward, respectively.

  4. Use the AppendCharAndSearch property to set up each letter of the incremental search pattern.

    Use the DeleteCharAndBackup to erase the last character in the pattern and place the insertion point at the last search location.

    Use the Pattern property to view the current search pattern.

  5. Call one of the following search methods:

  6. Call the Exit method to stop the search.

Example

The following example creates a text document with some lines of text, creates an incremental search pattern, and then performs a forward incremental search. For more information on how to run the sample code as a part of an Add-in, see How to: Compile and Run the Automation Object Model Code Examples.

Public Sub OnConnection(ByVal application As Object, ByVal _
  connectMode As ext_ConnectMode, ByVal addInInst As Object, ByRef _
  custom As Array) Implements IDTExtensibility2.OnConnection
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    incrementalSearch(_applicationObject)
End Sub

Private Sub incrementalSearch(ByVal dte As DTE2)
    ' Create a new text file.
    dte.ItemOperations.NewFile()

    ' Create a TextPoint at the start of the new document.
    Dim doc As TextDocument = _
    CType(dte.ActiveDocument.Object("TextDocument"), _
    TextDocument)
    Dim txtPoint As TextPoint = doc.StartPoint.CreateEditPoint
    Dim txtPane As EnvDTE80.TextPane2
    Dim i As Integer

    ' Insert a few lines of text.
    For i = 1 To 3
        txtPoint.Insert("abc abc " & vbCrLf)
    Next

    txtPane = CType(dte.ActiveDocument. _
    ActiveWindow.Object.ActivePane, TextPane2)
    ' Search forward of current insertion point.
    txtPane.IncrementalSearch.StartForward()
    ' Sets up the incremental search pattern: 
    ' first "a," then "b," then "c."
    txtPane.IncrementalSearch.AppendCharAndSearch(Asc("a"))
    txtPane.IncrementalSearch.AppendCharAndSearch(Asc("b"))
    txtPane.IncrementalSearch.AppendCharAndSearch(Asc("c"))
    MsgBox("Pattern to search: " & txtPane.IncrementalSearch.Pattern)
    ' Perform the search for the specified letters.        
    txtPane.IncrementalSearch.SearchWithLastPattern()
    ' Remove the last character ("c") from the search pattern.
    txtPane.IncrementalSearch.DeleteCharAndBackup()
    MsgBox("New pattern to search: " & _
    txtPane.IncrementalSearch.Pattern)
    txtPane.IncrementalSearch.SearchForward()
    If txtPane.IncrementalSearch.IncrementalSearchModeOn Then
        MsgBox("Deactivating Incremental Search mode...")
        txtPane.IncrementalSearch.Exit()
    End If
End Sub
public void OnConnection(object application, ext_ConnectMode 
connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    incrementalSearch(_applicationObject);
}

private void incrementalSearch(DTE2 dte)
{
    // Create a new text file.
    dte.ItemOperations.NewFile("General\\Text File","New 
      file",Constants.vsViewKindTextView);

    // Create a TextPoint at the start of the new document.
    TextDocument doc = 
      (TextDocument)dte.ActiveDocument.Object("TextDocument");
    TextWindow tw;
    EditPoint2 edtPoint = (EditPoint2)doc.StartPoint.CreateEditPoint();
    EnvDTE80.TextPane2 txtPane;
    int ctr;

    // Insert a few lines of text.
    for (ctr = 1; ctr <= 3; ctr+)
    {
        edtPoint.Insert("abc abc \r\n");
    }

    tw = (TextWindow)dte.ActiveWindow.Object;
    txtPane = (TextPane2)tw.ActivePane;

    // Search forward of current insertion point.
    txtPane.IncrementalSearch.StartForward();
    // Sets up the incremental search pattern: 
    // first "a," then "b," then "c."
    txtPane.IncrementalSearch.AppendCharAndSearch
      (Convert.ToInt16('a'));
    txtPane.IncrementalSearch.AppendCharAndSearch
      (Convert.ToInt16('b'));
    txtPane.IncrementalSearch.AppendCharAndSearch
      (Convert.ToInt16('c'));    
    System.Windows.Forms.MessageBox.Show("Pattern to search: " + 
      txtPane.IncrementalSearch.Pattern);
    // Perform the search for the specified letters.        
    txtPane.IncrementalSearch.SearchWithLastPattern();
    // Remove the last character ("c") from the search pattern.
    txtPane.IncrementalSearch.DeleteCharAndBackup();
    System.Windows.Forms.MessageBox.Show("New pattern to search: " + 
      txtPane.IncrementalSearch.Pattern);
    txtPane.IncrementalSearch.SearchForward();
    if (txtPane.IncrementalSearch.IncrementalSearchModeOn)
    {
        System.Windows.Forms.MessageBox.Show("Deactivating Incremental 
          Search mode...");
        txtPane.IncrementalSearch.Exit();
    }
}

See Also

Tasks

How to: Compile and Run the Automation Object Model Code Examples

Reference

Find2