Porady: automatyzowanie wyszukiwanie i zastępowanie tekstu
Dodatki Visual Studio zostały zaniechane w programie Visual Studio 2013.Dodatki należy uaktualniać do pakietu rozszerzenia VSPackage.Aby uzyskać więcej informacji na temat uaktualniania, zobacz Często zadawane pytania: konwertowanie dodatków na rozszerzenia pakietu VSPackage.
Visual Studio daje możliwość wyszukiwania i zamieniania tekstu w dokumentach, które są otwarte w zintegrowanym środowisku programistycznym (IDE) i zawarte w plikach systemu.Podstawowy sposób osiągnięcia tego jest za pomocą FindReplace i Execute metody Find obiektu.TextSelection i EditPoint oferują również obiekty FindPattern metody.Aby uzyskać więcej informacji, zobacz metodę FindPattern w Porady: kontrolowanie edytora kodu (Visual Basic).
[!UWAGA]
VsFindOptionsMatchInHiddenTex[t] wartość stała w [vsFindOptions] wyliczenia nie stosuje się do FindPattern metody ponieważ przeszukuje cały tekst, łącznie z tekstem ukrytym.
Wersja Find w EnvDTE80 przestrzeń nazw o nazwie Find2.Jest taka sama jak obiektu Find, ale oferuje nową właściwość o nazwie WaitForFindToComplete.Kiedy ta właściwość logiczna jest ustawiona na True, operacja wyszukiwania nie zakończy się dopóki wszystkie zaznaczone dokumenty nie został przeszukane.
Gdy, na przykład, chcesz wyszukać słowo w 100 dokumentach, możesz otrzymać niepełne wyniki, chyba, że można wykorzystasz właściwość WaitForFindToComplete lub obsługiwane zdarzenie FindDone.Obie metody działają, ale ustawienie właściwości WaitForFindToComplete jest krótszym i łatwiejszym sposobem zapewnienia, że wszystkie dokumenty są wyszukiwane przed wyświetleniem wyników wyszukiwania.
[!UWAGA]
Polecenia menu i okien dialogowych mogą różnić się od tych opisanych w Pomocy, w zależności od ustawień aktywnych lub wydania.Procedury te zostały opracowane z ogólnych ustawień projektowych active.Aby zmienić swoje ustawienia, wybierz Importuj i eksportujustawienia w menu Narzędzia.Aby uzyskać więcej informacji, zobacz Dostosowywanie ustawień środowiska deweloperskiego w Visual Studio.
Przykład
Poniższy przykład pokazuje, jak odwołać się i używać różnych członków modelu automatyzacji znajdowania.Ten przykład tworzy dokument tekstowy z tekstem, a następnie wyszukuje i zamienia tekst za pomocą różnych metod.Aby uruchomić ten przykład, należy zastąpić OnConnection metodę prostym dodatkiem z poniższym kodem.Aby uruchomić różne sekcje w tym przykładzie, usuń oznaczenie komentarza odpowiedniego kodu.Przed uruchomieniem tego kodu, upewnij się, że właściwość „Osadź typy współdziałania” zestawu EnvDTE odwołuje się do wartości Fałsz.
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()
}
Zobacz też
Zadania
Porady: kompilowanie i uruchamianie kodu modelu obiektów automatyzacji — przykłady
Porady: kontrolowanie edytora kodu (Visual Basic)
Koncepcje
Wykres modelu obiektów automatyzacji
Inne zasoby
Tworzenie i kontrolowanie okien środowiska