Results 1 to 5 of 5

Thread: Webview2 and reading a text box IN the webpage

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2022
    Posts
    29

    Webview2 and reading a text box IN the webpage

    Thought I'd try and stump you today.
    I got WeView2 to show a web page correctly and wondered if its possible to pull the value of a textbox on the web page into the VB form.

    Code:
            Dim jScript As String = "document.getElementById('WH').value;"
            Dim t As Task(Of String) = WebView22.CoreWebView2.ExecuteScriptAsync(jScript)
            Me.WebView21.CoreWebView2.ExecuteScriptAsync(jScript)
    I found this code. Added a button to the form and onclick, run the code, but it does not appear to pull the value over.

    Is there an Import I am missing?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Webview2 and reading a text box IN the webpage

    You have to actually get the String out of the completed Task. You have two options. You could do this:
    vb.net Code:
    1. Dim script = "document.getElementById('WH').value;"
    2. Dim wh2 = Await WebView22.CoreWebView2.ExecuteScriptAsync(script)
    3. Dim wh1 = Await WebView21.CoreWebView2.ExecuteScriptAsync(script)
    The Await operator automatically waits for the Task to complete and pulls out the String, but you would have to declare your method Async as well to use it. That is the usual way to go, unless you have a chain of method calls that you cannot make Async. The alternative would be this:
    vb.net Code:
    1. Dim script = "document.getElementById('WH').value;"
    2. Dim t2 = WebView22.CoreWebView2.ExecuteScriptAsync(script)
    3. Dim t1 = WebView21.CoreWebView2.ExecuteScriptAsync(script)
    4. Dim wh2 = t2.Result
    5. Dim wh1 = t1.Result
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2022
    Posts
    29

    Re: Webview2 and reading a text box IN the webpage

    Quote Originally Posted by jmcilhinney View Post
    You have to actually get the String out of the completed Task. You have two options. You could do this:
    vb.net Code:
    1. Dim script = "document.getElementById('WH').value;"
    2. Dim wh2 = Await WebView22.CoreWebView2.ExecuteScriptAsync(script)
    3. Dim wh1 = Await WebView21.CoreWebView2.ExecuteScriptAsync(script)
    The Await operator automatically waits for the Task to complete and pulls out the String, but you would have to declare your method Async as well to use it. That is the usual way to go, unless you have a chain of method calls that you cannot make Async. The alternative would be this:
    vb.net Code:
    1. Dim script = "document.getElementById('WH').value;"
    2. Dim t2 = WebView22.CoreWebView2.ExecuteScriptAsync(script)
    3. Dim t1 = WebView21.CoreWebView2.ExecuteScriptAsync(script)
    4. Dim wh2 = t2.Result
    5. Dim wh1 = t1.Result
    Woohoo! This will blow my clients mind
    Still needs a bit of clean up but will work perfectly. I suppose I can move data from the form TO the web page as well..

    Code:
        
    Private Async Function Button2_ClickAsync() As Task
            Dim script = "document.getElementById('WH').value;"
            Dim WH As String = Await WebView23.CoreWebView2.ExecuteScriptAsync(script)
    
            TextBox1.Text = WH.ToString
    
        End Function
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Button2_ClickAsync()
        End Sub
    Many many thanks!
    Last edited by Juliemac57; Feb 25th, 2022 at 10:19 AM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Webview2 and reading a text box IN the webpage

    Quote Originally Posted by Juliemac57 View Post
    Woohoo! This will blow my clients mind
    Still needs a bit of clean up but will work perfectly. I suppose I can move data from the form TO the web page as well..

    Code:
        
    Private Async Function Button2_ClickAsync() As Task
            Dim script = "document.getElementById('WH').value;"
            Dim WH As String = Await WebView23.CoreWebView2.ExecuteScriptAsync(script)
    
            TextBox1.Text = WH.ToString
    
        End Function
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Button2_ClickAsync()
        End Sub
    Many many thanks!
    That's not right. While you generally shouldn't declare a Sub Async, you can specifically for the purposes of event handlers. You should declare your event handler Async and then either get rid of the second method and put all the code in the event handler or else Await the second method when you call it from the event handler.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2022
    Posts
    29

    Re: Webview2 and reading a text box IN the webpage

    Quote Originally Posted by jmcilhinney View Post
    That's not right. While you generally shouldn't declare a Sub Async, you can specifically for the purposes of event handlers. You should declare your event handler Async and then either get rid of the second method and put all the code in the event handler or else Await the second method when you call it from the event handler.
    That has been cleaned up. Thanks

Tags for this Thread

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