How can i get selected text in web page and put it on textbox?

This is the text that i want to copy.

This the page source of the web page

<div class="output">



<h1 class="titlebar">Calculator Output</h1>



<div style="padding: 1.2ex;">



<pre>Simplifying
-2x + 3y = 8

Solving
-2x + 3y = 8

Solving for variable 'x'.

Move all terms containing x to the left, all other terms to the right.

Add '-3y' to each side of the equation.
-2x + 3y + -3y = 8 + -3y

Combine like terms: 3y + -3y = 0
-2x + 0 = 8 + -3y
-2x = 8 + -3y

Divide each side by '-2'.
x = -4 + 1.5y

Simplifying
x = -4 + 1.5y</pre>

</div>



<div class="status">

Processing time: 1 ms.

26878127 equations since February 08, 2004.

&nbsp; <a href="/calculators/quality.htm">Disclaimer</a>

</div>



</div>

Dani AI

Generated

Quick summary and practical options. wanted the generated result (the red block) or whatever text a user highlights inside a WebBrowser to appear in a TextBox. suggested regex and parsed the HTML with string operations. Both can work, but the more robust approaches are (A) read the element from the DOM when the site wraps the answer in a known tag (like <pre>) or (B) ask the page for the current selection via JavaScript and return it to the WinForms host.

DOM approach (best when the result is in a predictable element):

' run after DocumentCompleted
Dim pres = WebBrowser1.Document.GetElementsByTagName("pre")
If pres.Count > 0 Then
    TextBox2.Text = pres(0).InnerText
End If

Selection-via-JS approach (gets exactly what the user highlighted):

' inject once (DocumentCompleted)
Dim js = "function getSel(){return window.getSelection ? window.getSelection().toString() : (document.selection && document.selection.createRange ? document.selection.createRange().text : '');}"
Dim el As HtmlElement = WebBrowser1.Document.CreateElement("script")
el.SetAttribute("text", js)
WebBrowser1.Document.GetElementsByTagName("head")(0).AppendChild(el)

' later (button click)
Dim o = WebBrowser1.Document.InvokeScript("getSel")
Dim selected As String = If(o IsNot Nothing, o.ToString(), "")
TextBox1.Text = selected

Troubleshooting and notes. Run extraction after DocumentCompleted and after any client-side JS finishes. If the site uses frames/iframes, the selection may live in a frame and you must call the script in that frame's document. Avoid relying on regex to parse HTML for this (it is brittle); prefer DOM methods or a parser like HtmlAgilityPack for offline HTML scraping. 's string-parsing approach works as a fallback, but DOM/JS solutions are simpler and more reliable for the use case described.

Recommended Answers

All 8 Replies

Use regular expresions

Use a .Substring.

Private sMain As String, iSi, iEi As Integer
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        sMain = "abcd<pre>some calculation here</pre>efg" '// your html.source.
        iSi = sMain.IndexOf("<pre>") + 5 '// locate your start.point.
        iEi = sMain.IndexOf("</pre>", iSi) '// locate end.point.
        MsgBox(sMain.Substring(iSi, iEi - iSi)) '// get results.
    End Sub

How about the red highlighted part of that site is I want to display on my webbrowser when i click button 1 or solve button?

This is the picture of the site that i want to display on my webbrowser.
[IMG]http://i1059.photobucket.com/albums/t432/doomhades666/2.png[/IMG]

This is my code

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("http://www.algebrahelp.com/calculators/equation/")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Document.GetElementById("equation").SetAttribute("value", TextBox1.Text)
        WebBrowser1.Document.GetElementById("solvf").SetAttribute("value", ComboBox1.Text)
        WebBrowser1.Document.GetElementById("equationForm").InvokeMember("submit")

    End Sub
End Class

If you just need to change the content of the WebBrowser with yours Then check out this post.

Thanks codeorder for the replies but i want to display that highlighted one in my webbrowser, is that possible? its not an image. its the result of the problem that i made, its a generated answer to the math problem.

See if this helps.

Public Class Form1
    Private urlMain As String = "http://www.algebrahelp.com/calculators/equation/"
    Private sMain, arContent() As String, iSi, iEi As Integer

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Navigate(urlMain)
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        With WebBrowser1
            TextBox2.Text = .Url.AbsoluteUri
            If .Url.AbsoluteUri = urlMain Then '// check if correct.url.
                With .Document
                    .GetElementById("equation").SetAttribute("value", TextBox1.Text)
                    .GetElementById("solvf").SetAttribute("value", ComboBox1.Text)
                    .GetElementById("equationForm").InvokeMember("submit")
                End With
            End If
            If .Url.AbsoluteUri.StartsWith(urlMain) AndAlso .Url.AbsoluteUri.Length > urlMain.Length Then
                sMain = .Document.Body.InnerHtml  '// your html.source.
                If sMain.Contains("<PRE>") Then
                    iSi = sMain.IndexOf("<PRE>") + 5 '// locate your start.point.
                    iEi = sMain.IndexOf("</PRE>", iSi) '// locate end.point.
                    arContent = sMain.Substring(iSi, iEi - iSi).Split(vbCrLf) '// .Split content to get lines.
                    sMain = ""
                    For Each itm As String In arContent '// loop thru all lines of content.
                        If Not sMain = "" Then sMain &= "<br />" & itm Else sMain = itm '// add "<br />" for the wb to recognize it as a new.line.
                    Next
                    .DocumentText = "<div style=""font-family:Verdana;font-size:14px;""><hr />" & sMain & "<hr /></div>" '// get results.
                End If
            End If
        End With
    End Sub
End Class

.btw, I cannot get the answer for "1+1" on that page:(; care to help me in Return and let me know the answer?since I've been trying to figure that out for years.
Thanks in advance.

Many thanks! your the man!.hehe

Glad I could help.:)

I did realize something just now, Not late last night(actually early morning:D) when I previously posted.

Instead of adding and using the arContent() to add line.breaks to the html, it can all be done w/the .Replace.

If .Url.AbsoluteUri.StartsWith(urlMain) AndAlso .Url.AbsoluteUri.Length > urlMain.Length Then
                sMain = .Document.Body.InnerHtml  '// your html.source.
                If sMain.Contains("<PRE>") Then
                    iSi = sMain.IndexOf("<PRE>") + 5 '// locate your start.point.
                    iEi = sMain.IndexOf("</PRE>", iSi) '// locate end.point.
                    .DocumentText = "<div style=""font-family:Verdana;font-size:14px;""><hr />" & sMain.Substring(iSi, iEi - iSi).Replace(vbCrLf, "<br />") & "<hr /></div>" '// get results.
                End If
            End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.