Export (0) Print
Expand All

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.

NoteNote

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.

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 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

Community Additions

ADD
Show:
© 2014 Microsoft