I have an app that displays the source code of a web page in a textbox...how can I..

Joined
Apr 13, 2011
Messages
19
Programming Experience
Beginner
How can I sync my selection when I select someone on the web page to automatically select the location of the element in the source code?

Here is what I have as far as the split screen goes.

VB.NET:
ublic Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Navigate(TextBox2.Text)

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Dim Viewer As String
        Viewer = WebBrowser1.DocumentText
        TextBox1.Text = Viewer



    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        WebBrowser1.GoBack()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        WebBrowser1.GoForward()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        WebBrowser1.GoHome()

    End Sub
End Class


Any ideas on how to select text on at least the web page and show it in the source code?

Thanks
 
This example highlights the source code as you move the mouse cursor over elements:
VB.NET:
Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    If Me.WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        Me.RichTextBox1.Text = Me.WebBrowser1.Document.GetElementsByTagName("html")(0).OuterHtml
        Me.webdoc = Me.WebBrowser1.Document
    End If
End Sub

Private WithEvents webdoc As HtmlDocument

Private Sub webdoc_MouseOver(sender As Object, e As System.Windows.Forms.HtmlElementEventArgs) Handles webdoc.MouseOver
    If e.ToElement Is Nothing Then Return

    Dim s As String = e.ToElement.OuterHtml.Trim
    Dim ix = Me.RichTextBox1.Text.IndexOf(s)
    If ix <> -1 Then
        Me.RichTextBox1.Select(ix, s.Length)
        Me.RichTextBox1.ScrollToCaret()
    End If
End Sub
Also set the RichTextBox HideSelection property to False to see the selections when the RTB doesn't have focus.
 
The example does have one flaw, if there are multiple elements with exactly same OuterHtml string it will only select the first occurence. This is not too common though, and working around from this perspective is a bit complicated; you would have to map all elements in tree to the full plain text source and figure out where current element belong in tree.
 
Back
Top