如何在WebBrowser控件中搜索文本 [英] How to search the text in WebBrowser Control

查看:78
本文介绍了如何在WebBrowser控件中搜索文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hai,

我们有像在windows7 / vista中那样预览文档的要求

预览窗格并且必须实现Winforms中的搜索功能。为此,

我已将Ms office docuemnts,Excel Sheets和Power Point文档以html格式加载到Webbrowser Control中。现在我们需要在该webbrowser中实现搜索功能控制。



为此,我们使用以下代码实现了搜索功能



Hai,
we have a requirement like previewing the documents as like in windows7/vista
"preview Pane" and have to implement the search functionality in Winforms.For that,
I have loaded the Ms office docuemnts, and Excel Sheets and Power Point documents into Webbrowser Control in html format.Now we need to implement the search functionality in that webbrowser Control.

For that we have implemented the search functionality using the following code

public bool FindNext(string text, WebBrowser webBrowser1)
        {

            IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
   
            IHTMLSelectionObject sel =doc.selection;// (IHTMLSelectionObject)doc.selection;
            IHTMLTxtRange rng = sel.createRange() as IHTMLTxtRange;
         
            rng.collapse(false); // collapse the current selection so we start from the end of the previous range
            if (rng.findText(text, 1000000, 0))
            {
                rng.select();
                return true;
            }
            else
                FindFirst(text, webBrowser1);
            return false;
        }



此代码适用于在 word Document 中搜索所有出现的字符串值。

但是当它来到 Excel和PPt文档时,这段代码无法正常工作。只查找字符串的第一次出现,其他出现的事件没有调试时发现。

我发现,对于word文档IHTMLDocument2对象存储了innerHTML和innerText中的html内容以及一些值。

但是对于Excel,它仅使用框架将文本存储在innerHTML中,并且工作表被引用到本地临时.html文件,并且它没有任何innerText内容显示为null。 br />


请提供搜索加载html内容的webbrowser控件中的文本的解决方案,该内容是从 Excel,PPT 转换为html类型和在webbrowser控件中显示。



如果您有任何疑问,请感受问我。



谢谢。


This code is working fine for searching the string values in word Document for all the occurences.
But when it Comes to Excel and PPt documents this code doesn''t work properly.it finds only the first occurrence of the string, other occurrences doesn''t find.
while debugging i found that,for word Documents "IHTMLDocument2" object stores the html Content in "innerHTML" and "innerText" with some values.
But for Excel it stores the text in "innerHTML" only by using the Frames, and sheets are referenced to local temporary .html files and it didn''t have any "innerText" content it showing as null.

Please provide the solution to search the text in webbrowser control which loaded the html content, that is converted from Excel,PPT to html type and displaying in webbrowser control.

If you have any queries please feel to ask me.

Thank You.

推荐答案

嗨Naidu,



有什么问题?



如果你添加一个IF语句并检查innerText是否为零,如果在innerHTML上搜索,那么这不会解决你的问题吗?



干杯,

Edo
Hi Naidu,

What''s the problem?

What if you add an IF statement and check if the innerText is nothing, search on the innerHTML, wouldn''t that solve your issue?

Cheers,
Edo


这篇关于如何在WebBrowser控件中搜索文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆