实施“查找下一个”在 C# 的 Web 浏览器控件中
我有一个嵌入在表单
中的网络浏览器
控件。表单加载后,它会加载本地存储的 HTML 文件。我已经实现了查找文本功能,以在 Web 浏览器
控件中加载的 HTML 文档中查找特定文本。它正在努力查找指定单词的第一次出现。
但我想一次性突出显示指定单词的所有出现情况,或者更好地实现类似于各种应用程序中的“查找下一个”功能的功能。是否可以对网络浏览器控制执行此操作???
这是当前的代码:
private void toolStripButton1_Click(object sender, EventArgs e)
{
string TextToFind;
TextToFind = toolStripTextBox1.Text;
if (webBrowser1.Document != null)
{
IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
if (doc != null)
{
IHTMLSelectionObject currentSelection = doc.selection;
IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
if (range != null)
{
String search = TextToFind.ToString();
if (range.findText(search, search.Length, 2))
{
range.select();
}
}
}
}
}
谢谢。
I have a web browser
control embedded in a form
. Upon form load it loads a locally stored HTML file. I have implemented a find text functionality to find a particular text in the HTML document loaded in the web browser
control. It is working for finding the first occurrence of the word specified.
But I want to highlight all the occurrences of the specified word all at once or still better implementing something analogous to "Find Next" function found in various applications. Is it possible to do that for web browser control???
Here's the current code:
private void toolStripButton1_Click(object sender, EventArgs e)
{
string TextToFind;
TextToFind = toolStripTextBox1.Text;
if (webBrowser1.Document != null)
{
IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
if (doc != null)
{
IHTMLSelectionObject currentSelection = doc.selection;
IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
if (range != null)
{
String search = TextToFind.ToString();
if (range.findText(search, search.Length, 2))
{
range.select();
}
}
}
}
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在此处找到问题的代码示例 MSDN 论坛:WebBrowser 查找对话框
希望这正是您正在寻找的内容。
You will find the the code sample for your question here MSDN Forums: WebBrowser Find Dialog
Hope that is exactly what you are looking for.
只需在文本框中获取用户的输入[此处为txtNoteSearch],然后按照以下代码即可实现搜索。以下代码演示了搜索和突出显示。
Just take input from the user in a textbox [here txtNoteSearch] and then follow the following code to implement the search. Following code demonstrates the search and highlight.