我正在尝试实现搜索例程:在 Webbrowser 中查找下一个和查找上一个。我知道里面有内置的查找对话框,但我不想使用它。相反,我正在努力编写自己的例程,但我无法通过。以下代码很好地实现了“查找下一个”:
using mshtml;
....
private void toolStripButton5_Click(object sender, EventArgs e)
{
IHTMLDocument2 htmlDocument = WB.Document.DomDocument as IHTMLDocument2;
IHTMLSelectionObject sel = (IHTMLSelectionObject)htmlDocument.selection;
IHTMLTxtRange tr = (IHTMLTxtRange)sel.createRange() as IHTMLTxtRange;
string s = toolStripTextBox1.Text;
tr.collapse(false);
tr.findText(s, 1, 0); // positive value, should search forward, negative backward
tr.select();
}
我完全无助于制作“查找上一个”例程。有天才可以锻造吗? (它可能与上面的非常相似)。
非常感谢
最佳答案
对于“查找上一个”例程,使用相同的代码并进行 2 处修改: 1) 将 tr.collapse 的参数从 false 更改为 true; 2) 将tr.findText的第二个参数从1改为-1。
所以这些行将是:
tr.collapse(true); // use true to move caret at the beginning of the current selection
tr.findText(s, -1, 0); // positive value, should search forward, negative backward
关于c# - Webbrowser 查找上一个(文本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3588350/