次の方法で共有


方法 : インクリメンタル検索を自動化する

更新 : 2007 年 11 月

Visual Studio では、コード ドキュメントおよびテキスト ドキュメントをインクリメント方式で検索する機能が用意されています。つまり、1 つ以上の文字を入力するたびに、入力した文字を対話形式的に検索できます。詳細については、「方法 : ドキュメントのインクリメンタル検索を実行する」を参照してください。

IncrementalSearch オブジェクトを使用すると、プログラムによってインクリメンタル検索を実行できます。

ms165640.alert_note(ja-jp,VS.90).gifメモ :

使用している設定またはエディションによっては、表示されるダイアログ ボックスやメニュー コマンドがヘルプに記載されている内容と異なる場合があります。ここに記載されている手順は、全般的な開発設定が適用されているものとして記述されています。設定を変更するには、[ツール] メニューの [設定のインポートとエクスポート] をクリックします。詳細については、「Visual Studio の設定」を参照してください。

インクリメンタル検索パターンを作成するには

  1. 検索対象のドキュメントを開きます。

  2. IncrementalSearch プロパティを使用して IncrementalSearch オブジェクトにアクセスします。このプロパティは DTE.ActiveDocument.ActivePane.Object.ActivePane によって取得されます。

  3. 検索する方向を選択します。StartForward または StartBackward をそれぞれ使用して、カーソル位置から前方または後方を検索します。

  4. AppendCharAndSearch プロパティを使用して、インクリメンタル検索パターンの各文字を設定します。

    DeleteCharAndBackup を使用して、パターンの最後の文字を消去し、カーソルを検索位置の最後に配置します。

    Pattern プロパティを使用して、現在の検索パターンを表示します。

  5. 次のいずれかの検索メソッドを呼び出します。

  6. Exit メソッドを呼び出し、検索を終了します。

使用例

次の例では、複数行のテキストを含むテキスト ドキュメントを作成し、インクリメンタル検索パターンを作成します。その後、下方向へインクリメンタル検索を実行します。このサンプル コードをアドインの一部として実行する方法の詳細については、「方法 : オートメーション オブジェクト モデルのコード例をコンパイルおよび実行する」を参照してください。

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();
    }
}

参照

処理手順

方法 : オートメーション オブジェクト モデルのコード例をコンパイルおよび実行する

参照

Find2