how to find a word in a web page
-
martedì 6 marzo 2012 02:19
Hello, I am wondering how to search a web browser document and highlight all occurrences of that word. I did come across a way of searching for the document ....Function SearchURL(ByVal URL As String, ByVal phrase As String) As String Dim wc As New Net.WebClient Dim WebString As String Dim Found As String = "Not Found" Dim idx As Integer Try WebString = wc.DownloadString(URL) idx = WebString.ToLower.IndexOf(phrase.ToLower) If idx > -1 Then Found = WebString.Substring(idx, phrase.Length) End If Catch ex As Exception MessageBox.Show(ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try wc.Dispose() Return Found End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Label1.Text = SearchURL("http://www.google.com", "Google")which i could easily manipulate to say like "we found this many occurrences of the word such and such in the web page." But i would just like to know how to highlight those words. thanks in advance (:"We are what we repeatedly do. Excellence, therefore, is not an act but a habit." Aristotle
Tutte le risposte
-
martedì 6 marzo 2012 02:55Hello there! I've been handling this for quite a long time now. Hope someone in there could help.
hex head cap screws
-
mercoledì 7 marzo 2012 08:55Moderatore
Hi Foster,
Welcome to the MSDN forum.
According to your description, you’d like to highlights words in WebBrowser Control. One way to achieve your goal is get HTML codes of web pages and then search the specified words, and then change the CSS of the words. Please try the following steps:
- Add a reference to Microsoft.mshtml , on my PC its location is “C:\Program Files (x86)\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll”
- Imports mshtml and System.Text
- Add a button and a WebBrowser Control and paste the following codes
Public Class Form1 Private Sub webBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted Dim doc2 As IHTMLDocument2 = TryCast(WebBrowser1.Document.DomDocument, IHTMLDocument2) Dim html As New StringBuilder(doc2.body.outerHTML) Dim words = New String() {"Visual Basic", "minutes"} For Each key As String In words Dim substitution As String = "<span style='background-color: rgb(255, 255, 0);'>" & key & "</span>" html.Replace(key, substitution) Next doc2.body.innerHTML = html.ToString() End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click WebBrowser1.Navigate("http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/threads") AddHandler WebBrowser1.DocumentCompleted, AddressOf webBrowser1_DocumentCompleted End Sub End Class
If anything is unclear, please feel free and feedback.
Best regards,
Shanks Zen
MSDN Community Support | Feedback to us
- Contrassegnato come risposta Foster Tippins giovedì 8 marzo 2012 01:03

