Find 接口
更新:2007 年 11 月
支持文档和文件环境中的常规文本 Find 操作。
命名空间: EnvDTE
程序集: EnvDTE(在 EnvDTE.dll 中)
语法
声明
<GuidAttribute("40D4B9B6-739B-4965-8D65-692AEC692266")> _
Public Interface Find
用法
Dim instance As Find
[GuidAttribute("40D4B9B6-739B-4965-8D65-692AEC692266")]
public interface Find
[GuidAttribute(L"40D4B9B6-739B-4965-8D65-692AEC692266")]
public interface class Find
public interface Find
备注
Find 对象使您得以在支持搜索和替换操作的环境(例如代码编辑器)中搜索并替换文本。
它主要用于宏记录。编辑器的宏记录机制使用 Find 而不使用 TextSelection.FindPattern,这样方便您使用全局查找功能,还有一个原因是,对于“在文件中查找”这种操作来说,使用前者一般比使用 TextSelection 对象更有用。
Visual Studio 环境有一个全局查找状态,可以在提供搜索能力的所有工具中共享。例如,所有 Visual Studio 元素共享会话期间使用的搜索模式的历史记录,还共享关于在打开的文档向前还是向后执行下一个 Find 操作的信息。Find 对象的属性与全局查找状态交互并跟踪全局查找状态。当在 Find 对象上设置属性时,也设置了全局查找状态。如果用户通过该环境执行 Find 操作,则 Find 对象反映它们执行的搜索类型。因为自动化代码与环境的 UI 线程同步运行,所以无需设置某些属性以及让用户执行搜索,就可以调用 Execute。
Execute 方法根据 Find 对象的设置执行 Find 操作。还可以将参数传递给 FindReplace 方法,以执行搜索而不影响全局查找状态。对于自动化客户端,重要的是能在执行搜索时不影响全局查找状态或环境状态的最终用户模型。
示例
Sub FindExample()
Dim objTextDoc As TextDocument
Dim objEditPt As EditPoint
Dim iCtr As Integer
Dim objFind As Find
' Create a new text file.
DTE.ItemOperations.NewFile("General\Text File")
' Get a handle to the new document and create an EditPoint.
objTextDoc = DTE.ActiveDocument.Object("TextDocument")
objEditPt = objTextDoc.StartPoint.CreateEditPoint
objFind = objTextDoc.DTE.Find
' Insert ten lines of text.
For iCtr = 1 To 10
objEditPt.Insert("This is a test." & Chr(13))
Next iCtr
' Set the find options.
objFind.Action = vsFindAction.vsFindActionReplaceAll
objFind.Backwards = False
objFind.FilesOfType = "*.txt"
objFind.FindWhat = "test"
objFind.KeepModifiedDocumentsOpen = True
objFind.MatchCase = False
objFind.MatchInHiddenText = False
objFind.MatchWholeWord = True
objFind.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
objFind.ReplaceWith = "NEW THING"
objFind.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
objFind.SearchPath = "c:\temp"
objFind.SearchSubfolders = False
objFind.Target = vsFindTarget.vsFindTargetCurrentDocument
' Perform the Find operation.
objFind.Execute()
End Sub