c# - Webbrowser 查找上一个(文本)

标签 c# browser

我正在尝试实现搜索例程:在 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/

相关文章:

c# - 为什么在 .Net 4.7.2 项目中引用 .Net 标准 nuget 包会导入大量 .Net core lib?

c# - 在C#中读取带有特殊字符的Excel工作表的问题

html - 导航菜单仅在 Safari 中显示为同时按下。想法?

javascript - 浏览器缩放未对齐的解决方法

browser - 检测 Applescript 中的默认浏览器

c# - 使用 res ://protocol with System. Windows.Forms.WebBrowser

android - 使用 Chrome 浏览器在 Android 上搜索输入文本字段

c# - 如何在 C# 中执行 "interrupt"while 循环并返回到同一位置(不使用任何线程)

c# - TimeSpan.ParseExact ASP.NET 4 C#

c# - 获取和设置 SQL 超时?