Aracılığıyla paylaş


Nasıl yapılır: metin otomatik arama ve değiştirme

Visual Studiotümleşik geliştirme ortamı (IDE) açık ve dosya sisteminde bulunan belgelerdeki metin arama ve değiştirme olanağı verir. Bunu gerçekleştirmek için birincil yolu kullanmaktır FindReplace ve Execute yöntemleri Find nesne. TextSelection Ve EditPoint nesneleri de sunan FindPattern yöntemi. Daha fazla bilgi için bkz: FindPattern yönteminde Nasıl yapılır: Code Editor (Visual Basic) kontrol.

Not

VsFindOptionsMatchInHiddenTex[t] sabit değeri [vsFindOptions] numaralandırma için geçerli değildir FindPattern yöntemi tüm metni arar çünkü gizli metin dahil.

Sürümü Find , EnvDTE80 ad adlı Find2. Aynı olan Find adında yeni bir özellik sunar nesne, ancak WaitForFindToComplete. Bu boolean özelliği ayarlandığında True, Bul işlemi tüm seçili kadar belgeleri Aranan sonuçlandırmak değil.

Örneğin, 100 belgelerde bir sözcüğü aramak gerekseydi, ya da kullandığınız sürece tamamlanmamış sonuçlar alabilirsiniz WaitForFindToComplete özelliği veya ele FindDone olay. Her iki çalışma yöntemleri ancak ayarı WaitForFindToComplete özelliği olan arama sonuçlarını görüntülemeden önce tüm belgeler aranır sağlamak için daha kısa ve daha kolay bir yol.

Not

Gördüğünüz iletişim kutuları ve menü komutları, etkin ayarlarınıza ve ürün sürümüne bağlı olarak Yardım menüsünde açıklanana göre farklılık gösterebilir.Bu yordamlar, genel geliştirme ayarları ile etkin geliştirilmiştir.Ayarlarınızı değiştirmek için Al ve Verayarları üzerinde araçları menü.Daha fazla bilgi için bkz. Visual Studio ayarları.

Örnek

Aşağıdaki örnekler, nasıl başvuru ve Bul otomasyon modeli çeşitli üyeleri göstermektedir. Bu örnek bazı metin bir metin belgesi oluşturur ve sonra arar ve farklı yöntemler kullanarak metnin yerine geçer. Bu örneği çalıştırmak için yerini OnConnection bir basit eklenti aşağıdaki kod ile yöntemi. Bu örnekte farklı bölümlerini çalıştırmak için uygun kodu yorumsuz. Bu kodu çalıştırmadan önce EnvDTE montajý "Birlikte çalışabilirlik tipleri gömmek" özelliğini False olarak başvuru emin olun.

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)
    searchReplace(_applicationObject)
End Sub

Public Sub searchReplace(ByVal dte As DTE2)
    Dim findWin As Find2
    Dim doc As Document
    Dim textDoc As TextDocument
    Dim textSel As TextSelection
    Dim iCtr As Integer

    ' Create a new text file.
    dte.ItemOperations.NewFile("General\Text File")

    ' Set up references for the text document, Find object, and
    ' TextSelection object.
    doc = dte.ActiveDocument
    textDoc = CType(doc.Object("TextDocument"), TextDocument)
    textSel = textDoc.Selection
    findWin = CType(dte.Find, Find2)
    ' Make sure all docs are searched before displaying results.
    findWin.WaitForFindToComplete = True

    ' Insert ten lines of text.
    For iCtr = 1 To 10
        textDoc.Selection.Text = "This is a test" & vbCr
    Next iCtr
    textDoc.Selection.Text = "This is a different word"

    ' Uses FindReplace to find all occurrences of the word, test, in 
    ' the document.
    MsgBox("Now changing all occurrences of 'test' to 'replacement'.")
    findWin.FindReplace(vsFindAction.vsFindActionReplaceAll, "test", _
      vsFindOptions.vsFindOptionsMatchCase, "replacement", _
      vsFindTarget.vsFindTargetCurrentDocument, , , _
      vsFindResultsLocation.vsFindResultsNone)

    ' Uses Find2.Execute to find the word, different, in the document.
    ' findWin.FindWhat = "different"
    ' findWin.MatchCase = True
    ' findWin.Execute()

    ' Uses Find2.Execute to replace all occurrences of the word, Test, 
    ' with the word, replacement.
    ' findWin.FindWhat = "test"
    ' findWin.ReplaceWith = "replacement"
    ' findWin.Action = vsFindAction.vsFindActionReplaceAll
    ' findWin.Execute()
End Sub
public void OnConnection(object application, ext_ConnectMode 
  connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    searchReplace(_applicationObject);
}

public void searchReplace(DTE2 dte)
{
    Find2 findWin;
    Document doc;
    TextDocument textDoc;
    TextSelection textSel;
    int iCtr;

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

    // Set up references for the text document, Find object, and
    // TextSelection object.
    doc = dte.ActiveDocument;
    textDoc = (TextDocument) doc.Object("TextDocument");
    textSel = textDoc.Selection;
    findWin = (Find2) dte.Find;
    // Make sure all docs are searched before displaying results.
    findWin.WaitForFindToComplete = true;

    // Insert ten lines of text.
    for(iCtr=1; iCtr<=10; iCtr++)
    {
        textDoc.Selection.Text = "This is a test"+Environment.NewLine;
    }
    textDoc.Selection.Text = "This is a different word";

    // Uses FindReplace to find all occurrences of the word, test, in 
    // the document.
   System.Windows.Forms.MessageBox.Show(
     "Now changing all occurrences of 'test' to 'replacement'.");
   findWin.FindReplace(vsFindAction.vsFindActionReplaceAll, "test", 
     (int) vsFindOptions.vsFindOptionsFromStart, "replacement", 
     vsFindTarget.vsFindTargetCurrentDocument, "", 
     "",vsFindResultsLocation.vsFindResultsNone);

   // Uses Find2.Execute to find the word, different, in the document.
   // findWin.FindWhat = "different"
   // findWin.MatchCase = True
   // findWin.Execute()

   // Uses Find2.Execute to replace all occurrences of the word, Test, 
   // with the word, replacement.
   // findWin.FindWhat = "test"
   // findWin.ReplaceWith = "replacement"
   // findWin.Action = vsFindAction.vsFindActionReplaceAll
   // findWin.Execute()
}

Ayrıca bkz.

Görevler

Nasıl yapılır: derlemek ve Otomasyon nesne modeli kod örneklerini çalıştırmak

Nasıl yapılır: Code Editor (Visual Basic) kontrol

Nasıl yapılır: bir eklenti oluşturmak

İzlenecek yol: bir sihirbaz oluşturma

Kavramlar

Otomasyon nesne modeli şeması

Diğer Kaynaklar

Oluşturma ve ortam Windows denetleme

Eklentiler ve sihirbazlar oluşturma

Otomasyon ve Genişletilebilirlik Başvurusu