Webbrowser Search

DICKDDD

New member
Joined
Mar 9, 2007
Messages
3
Programming Experience
10+
Hello,

I have implemented a custom Webbrowser in VB using the WB control. I would like to be able to search a web page for text and highlight it if found. Something similar to the IE 'Find on this page' command. Anyone know how to do this? I found a few things in MSDN about invoking a jscript function in the page to do this but I don't know how to get the jscript function into the page so I can invoke it? This seems like a simple thing but I have made that mistake before, nothing is simple !!

Any ideas? Thanx
 
If user press Ctrl+F while WebBrowser control has focus the standard Find dialog will appear and can be used. This also work by sending same key combination from for example a button, toolstrip button or menustrip item like this:
VB.NET:
        WebBrowser1.Select()
        Application.DoEvents()
        SendKeys.Send("^f")
 
If user press Ctrl+F while WebBrowser control has focus the standard Find dialog will appear and can be used. This also work by sending same key combination from for example a button, toolstrip button or menustrip item like this:
VB.NET:
        WebBrowser1.Select()
        Application.DoEvents()
        SendKeys.Send("^f")

I found this very useful myself but I'd like to take it one step further.

Is there a way to highlight text on the WebBrowser and have it copy to the input box of the Find dialog box when it opens?
 
Last edited:
The first thing I thought of was doing another obtrusion, and it worked :) Copy selection with HtmlElement.ExecCommand method and paste it with SendKeys:
VB.NET:
Dim data As DataObject = Clipboard.GetDataObject 'get current clipboard

Me.WebBrowser1.Document.ExecCommand("Copy", False, Nothing)
WebBrowser1.Select()
SendKeys.SendWait("^f")
SendKeys.SendWait("^v")

Clipboard.SetDataObject(data) 'load previous clipboard back
 
Last edited:
lol works like a charm you so rock.....
 
There are two methods for not interupting the users clipboard here, either preserve the clipboard data object before the copy and restore it, or not use the clipboard and instead use Win32 methods for finding the window handle then textbox handle and inserting the text. Maintaining clipboard is done like this:
VB.NET:
Dim data As DataObject = Clipboard.GetDataObject
'...
Clipboard.SetDataObject(data)
The Win32 code is a little more complex.

Actually doing the DataObject thing interupts the quick execution of the previous code, so for everything to work properly you have to use the SendWait method for Paste instead of just the Send method of SendKeys. I updated the code above to show this.
 
Back
Top