Results 1 to 13 of 13

Thread: [RESOLVED] Highlight text in WebBrowser

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Resolved [RESOLVED] Highlight text in WebBrowser

    I'm using a WebBrowser control and I'd like to highlight text that the user enters. It's supposed to be kind of like what you get from Ctrl+F in Firefox. I initially tried this:

    Me.webHelp.DocumentText = Me.strOriginal.Replace(Me.txtSearch.Text, "<span style=""background-color:#44F; color:#FFF"">" & Me.txtSearch.Text & "</span>")

    However, if the user types "body" it replaces every instance of "body", even ones inside tags, which hoses the HTML. How could I separate the page text from the tags(and everything else) and just do a replace on that while maintaining the integrity of the HTML?

    Thanks
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Highlight text in WebBrowser

    You may have to use regular expressions to pull the text between html tags and do a replace on them, then replace the old tag with the new one in the document.
    Try something like this:
    Code:
            Dim txt2highlight As String = "Yahoo" '<<< this is the text you want to search and highlight in the document"
    
            Dim pattern As String = "<(?<tag>\w*)>[\n\s]*(?<text>.*" & txt2highlight & ".*)[\n\s]*</\k<tag>>"
            Dim htmlsrc As String = WebBrowser1.DocumentText()
            Dim txt As String = String.Empty
            Dim tag As String = String.Empty
            For Each m As Match In Regex.Matches(htmlsrc, pattern)
                txt = m.Groups("text").Value.Replace(txt2highlight, "<span style=""background-color:#44F; color:#FFF"">" & txt2highlight & "</span>")
                tag = m.Value.Replace(m.Groups("text").Value, txt)
                htmlsrc = htmlsrc.Replace(m.Value, tag)
            Next
            WebBrowser1.DocumentText = htmlsrc
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Highlight text in WebBrowser

    I've been working on other problems, so I've just come back to this one now. Your code almost works. The problem is the last line. When I change the WebBrowser.DocumentText, I get an error on the page. The simplest example I can think of is this. It's just a page with a centered image. When I reset the DocumentText, the image no longer shows(I get a broken link image). Using a msgbox I printed WebBrowser1.DocumentText before doing anything, htmlsrc after the for loop, and WebBrowser1.DocumentText again after it was set. They all displayed exactly the same way and I also printed the String length of each and they all matched up.

    Here is the html:
    <html>
    <head>
    <title>This is the title</title>
    </head>

    <body style="background-color:#FFFFFF;">
    <div align="center">
    <center>
    <table border="0" height="100&#37;">
    <tr>

    <td>

    <br />
    <img src="placeholder.png">
    </td>
    </tr>
    </table>
    </center>
    </div>
    </body>
    </html>

    Like I said, that displays fine, then when I replace .DocumentText with the exact same string, it breaks.

    Any thoughts?

    Thanks
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Highlight text in WebBrowser

    The link in my signature about manipulating HTML with the browser control has a "highlight text" feature. You may want to take a look at that and see if you can adapt it to your code. It uses the COM browser though.

    Not sure which version of .NET you are using since your sig says 1.1/2003 but your thread title says 2008 which would imply .NET 2.0-3.5

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Highlight text in WebBrowser

    kleinma,
    I knew something was wrong when I went to download that and the link was purple. I've downloaded that before, but it doesn't work with VS 2008, which I've been using for the past year or so(fixed my sig, thanks).
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Highlight text in WebBrowser

    What problem did you have with 2008? I have used it fine. If you can give me some more info, I can probably tell you what needs to be done to get it working.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Highlight text in WebBrowser

    It doesn't open. Or rather, when it does there isn't anything. I get this warning upon opening.

    The application for project
    'C:\Users\me\Desktop\WebpageManipulation\WebpageManipulation\WebpageManipulation.vbproj' is not installed.

    Make sure the application for the project type (.vbproj) is installed.


    I've never encountered this error message before. I just chalked it up to incompatibility. I've had problems with some of my older projects converting to 2008, and I figured that was the case.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Highlight text in WebBrowser

    There should be a 'sln' file you should be opening, not a vbproj file.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Highlight text in WebBrowser

    I'm trying to open the sln. I attached a screenshot.
    Last edited by 18experience; Mar 23rd, 2010 at 09:37 AM.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Highlight text in WebBrowser

    I was testing this using a very simply html page that had a centered image and a title. Trying to replace text on the source produced an identical page(so it should look exactly the same). However, the image wouldn't display(just a small empty box).

    The image was displayed like this:
    <img src="placeholder.png">

    So, I got the idea that perhaps when I was setting the DocumentText, it was accidentally throwing away WebBrowser1.Document.Domain. So, I checked the domain right after setting the html, and it was fine. The program then jumped to the Navigating event of the webbrowser. It was still fine. However, once it jumped to the Navigated event, trying to check the Domain in the Immediate Window resulted in this error:

    Error HRESULT E_FAIL has been returned from a call to a COM component.

    So, I feel like I might be onto something, but I don't know what to do to fix it. Any thoughts anybody?

    Thanks
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Highlight text in WebBrowser

    I'm still having zero luck with this.

    Kleinma,
    Could you possibly open your project and paste the pertinent code? I don't wanna be a burden, but I can't get that project to open. I just get an error and a blank screen.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Highlight text in WebBrowser

    I'm getting closer!

    I changed the last line of Stanav's code to read:
    WebBrowser1.Document.Body.InnerHtml = htmlsrc

    and that did the trick.

    However, I think there is a problem with the RegEx string now. It seems that if I type single characters that exist in tags, it performs the operation on them. So, if I type 'a' it'll pick up the 'a' in 'span', and I get some garbage on the screen. If I continue with 'ab' it snaps back to the original page perfectly formatted, but now the 'ab's are highlighted. The RegEx string doesn't seem to like single characters. Anybody know how to fix it?

    Thanks
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Highlight text in WebBrowser

    A co-worker of mine decided to take a crack at it and he decided to ditch RegEx and go with a loop. I edited it after he sent it to me. It's case insensitive, though it wouldn't require much to make it case sensitive. I'm thinking of giving them that option and also giving a "Match Whole Word" option as well. I think that will be pretty easy. Here's the code. Would anyone mind looking over this just to see if I have any glaring mistakes? It seems safe and flaw free from the little bit of testing I've done. Thanks to everyone. Every post helped push me a little closer to the correct answer.
    Code:
       Public Sub HighlightSearch()
            Dim NextOpen As Integer = 0
            Dim CurrPos As Integer = 0
            Dim HTMLPart As String
            Dim NewHTML As String = String.Empty
            Dim HTML As String = String.Empty
            Dim Index As Integer
            Dim strSearchTerm As String = Me.txtSearch.Text
    
            If Me.strOriginal = "" Then Me.strOriginal = Me.webHelp.DocumentText
            HTML = Me.strOriginal
    
            If strSearchTerm <> String.Empty Then
                While CurrPos < HTML.Length
                    If HTML.Substring(CurrPos, 6) = "<head>" Then
                        NewHTML &= HTML.Substring(CurrPos, HTML.IndexOf("</head>", CurrPos) - CurrPos + 7)
                        CurrPos = HTML.IndexOf("</head>", CurrPos) + 7
                    ElseIf HTML.Substring(CurrPos, 1) = "<" Then
                        NewHTML &= HTML.Substring(CurrPos, HTML.IndexOf(">", CurrPos) - CurrPos + 1)
                        CurrPos = HTML.IndexOf(">", CurrPos) + 1
                    Else
                        If HTML.IndexOf("<", CurrPos) <> -1 Then
                            HTMLPart = HTML.Substring(CurrPos, HTML.IndexOf("<", CurrPos) - CurrPos)
                        Else
                            HTMLPart = HTML.Substring(CurrPos)
                        End If
                        CurrPos += HTMLPart.Length
    
                        While HTMLPart <> String.Empty
                            If HTMLPart.ToLower.IndexOf(strSearchTerm.ToLower) > -1 Then
                                Index = HTMLPart.ToLower.IndexOf(strSearchTerm.ToLower)
    
                                NewHTML &= HTMLPart.Substring(0, Index) & _
                                           "<span style=""background-color:#44F; color:#FFF"">" & _
                                           HTMLPart.Substring(Index, strSearchTerm.Length) & _
                                           "</span>"
                                HTMLPart = HTMLPart.Substring(Index + strSearchTerm.Length)
                            Else
                                NewHTML &= HTMLPart
                                HTMLPart = String.Empty
                            End If
                        End While
                    End If
                End While
            Else
                NewHTML = HTML
            End If
    
            Me.webHelp.Document.Body.InnerHtml = NewHTML
        End Sub
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width