This documentation is archived and is not being maintained.

How to: Automate an Incremental Search

Visual Studio 2008

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.


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 Export Settings 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.

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 void OnConnection(object application, ext_ConnectMode 
connectMode, object addInInst, ref Array custom)
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;

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

    // Create a TextPoint at the start of the new document.
    TextDocument doc = 
    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.
    // Sets up the incremental search pattern: 
    // first "a," then "b," then "c."
    System.Windows.Forms.MessageBox.Show("Pattern to search: " + 
    // Perform the search for the specified letters.        
    // Remove the last character ("c") from the search pattern.
    System.Windows.Forms.MessageBox.Show("New pattern to search: " + 
    if (txtPane.IncrementalSearch.IncrementalSearchModeOn)
        System.Windows.Forms.MessageBox.Show("Deactivating Incremental 
          Search mode...");