none
how to find a word in a web page

    Question

  • 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

    Tuesday, March 06, 2012 2:19 AM

Answers

  • 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:

    1.      Add a reference to Microsoft.mshtml , on my PC its location is “C:\Program Files (x86)\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll”
    2.      Imports mshtml and System.Text
    3.      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

    Wednesday, March 07, 2012 8:55 AM
    Moderator

All replies

  • Hello there! I've been handling this for quite a long time now. Hope someone in there could help.

    hex head cap screws

    Tuesday, March 06, 2012 2:55 AM
  • 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:

    1.      Add a reference to Microsoft.mshtml , on my PC its location is “C:\Program Files (x86)\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll”
    2.      Imports mshtml and System.Text
    3.      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

    Wednesday, March 07, 2012 8:55 AM
    Moderator