مشاركة عبر


كيفية القيام بما يلي: التنفيذ التلقائي لنص بحث و استبدال

Visual Studioيعطيك القدرة تشغيل إلى بحث واستبدال نص في مستندات المفتوحة في بيئة التطوير المتكاملة (IDE) والموجودة في الملفات الموجودة تشغيل النظام. الطريقة الأساسية ل accomplهوh th هو هو باستخدام FindReplaceو Executeالأساليب الخاصة Findالكائن. TextSelectionو EditPointتقدم أيضا للكائنات FindPatternأسلوب. للحصول على مزيد من المعلومات، راجع FindPatternأسلوب في كيفية القيام بما يلي: عنصر تحكم محرر تعليمات برمجية (Visual أساسى).

ملاحظة

vsFindOptionsMatchInHiddenTex [t]القيمة ثابتة في [vsFindOptions]لا يتم تطبيق التعداد إلى FindPatternأسلوب لأنه يبحث في النص بأكمله، متضمناً نص مخفي.

الإصدار Findفي EnvDTE80مساحة الاسم هو المسمى Find2. هو نفس Findالكائن، لكنه يقدم خاصية جديدة تسمى WaitForFindToComplete. عند ترتيب هو خاصية منطقية هو معينة إلى True، عملية بحث لا اعترف حتى الجميع محدد البحث عن مستندات.

إذا، على سبيل المثال، لم إلى البحث عن كلمة في مستندات 100، قد تتلقى نتائج غير كاملة إلا إذا استخدمت أما WaitForFindToCompleteخاصية أو معالجته FindDoneحدث. كل من وظائف العمل، ولكن تعيين WaitForFindToCompleteخاصية هو أقصر وأسهل طريقة للتأكد من أنه يتم بحث عن الجميع مستندات قبل dهوplaying نتائج بحث.

ملاحظة

قد تختلف مربعات الحوار وأوامر القائمة التى تشاهدها الان عن تلك الموصوفة في التعليمات اعتماداً على الإعدادات النشطة أو الإصدار الخاص بك. تم تطوير هذه الإجراءات من خلال "إعدادات تطوير عام" النشط. To change your settings, choose Import and Export Settings on the Tools menu. لمزيد من المعلومات، راجع العمل مع إعدادات.

مثال

تبين الأمثلة التالية كيفية مرجع واستخدام الأعضاء طراز التنفيذ التلقائي بحث مختلفة. يقوم هذا المثال بإنشاء مستند نصي بنص، و ثم يبحث عن و قم باستبدال نص باستخدام وظائف مختلفة. لتشغيل هذا المثال، استبدل OnConnectionالأسلوب في بسيط إضافة-الدخول باستخدام تعليمات برمجية أدناه. إلى تشغيل مقاطع مختلفة من هذا المثال، كرمز المناسبة. قبل تشغيل هذه تعليمات برمجية، تأكد من أن خاصية "يضمّن أنواع Interop" تجميع EnvDTE مرجع إلى خطأ.

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

راجع أيضًا:

المهام

كيفية القيام بما يلي: ترجمة و تشغيل أمثلة تعليمات برمجية طراز كائن للتنفيذ التلقائي

كيفية القيام بما يلي: عنصر تحكم محرر تعليمات برمجية (Visual أساسى)

كيفية القيام بما يلي: قم بإنشاء إضافة-في

الإرشادات التفصيلية: إنشاء معالج

المبادئ

مخطط نموذج كائن تلقائي

موارد أخرى

إنشاء و التحكم في بيئة Windows

إنشاء إضافة-زر 'Ins' ومعالجات

التنفيذ التلقائي والمرجع الامتداد