Jak: automatizovat Text Hledat a nahradit
Visual Studioumožňuje hledání a nahrazení textu v dokumentech, které jsou otevřeny v integrované vývojové prostředí (IDE) a v souborech v systému.Je hlavním způsobem dosáhnout pomocí FindReplace a Execute metod Find objektu.TextSelection a EditPoint objekty nabízejí také FindPattern metoda.Další informace naleznete FindPattern metoda v Postup: ovládání editoru kódu (Visual Basic).
[!POZNÁMKA]
VsFindOptionsMatchInHiddenTex[t] konstantní hodnotu [vsFindOptions] výčtu se nevztahuje na FindPattern metoda protože prohledá celý text, včetně skrytý text.
Verze Find v EnvDTE80 s názvem oboru názvů Find2.Je stejný jako Find objekt, ale nabízí nová vlastnost s názvem WaitForFindToComplete.Pokud je nastavena Tato booleovská vlastnost True, operace hledání neuzavře dokud všechny vybrané dokumenty byly prohledány.
Jestliže například bylo hledání slova v dokumentech 100, zobrazí neúplné výsledky pokud jste buď WaitForFindToComplete vlastnost nebo zpracováno FindDone událostí.Metody práce, ale nastavení WaitForFindToComplete vlastnost je kratší a snazší způsob, jak zajistit, aby před zobrazením výsledků hledání jsou prohledány všechny dokumenty.
[!POZNÁMKA]
Dialogová okna a příkazy v nabídkách menu, které vidíte, se mohou lišit od těch popsaných v nápovědě, v závislosti na vašich aktivních nastaveních nebo edici.Tyto postupy byly vyvinuty s aktivní Obecné nastavení pro vývoj.Chcete-li změnit nastavení, zvolte Import a ExportNastavení na Nástroje nabídce.Další informace naleznete v tématu Nastavení aplikace Visual Studio.
Příklad
Následující příklady ukazují, jak odkazovat a použití různých členů automatizace modelu najít.Tento příklad vytvoří textový dokument se část textu a poté vyhledá a nahradí text pomocí různých metod.Chcete-li spustit příklad nahradit OnConnection metoda jednoduchá doplněk s následující kód.Chcete-li spustit různé oddíly Tento příklad z komentáře příslušný kód.Před spuštěním tohoto kódu, ujistěte se, že vlastnost "typů Interop Vložit" sestavení EnvDTE odkazovat na hodnotu False.
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()
}
Viz také
Úkoly
Jak: kompilace a spuštění příkladů kódu automatizace objektu modelu
Postup: ovládání editoru kódu (Visual Basic)
Názorný postup: Vytvoření Průvodce
Koncepty
Automatizační objekt modelu grafu