|
-
Feb 24th, 2022, 02:01 PM
#1
Thread Starter
Junior Member
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?
-
Feb 24th, 2022, 07:51 PM
#2
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:
Dim script = "document.getElementById('WH').value;"
Dim wh2 = Await WebView22.CoreWebView2.ExecuteScriptAsync(script)
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:
Dim script = "document.getElementById('WH').value;"
Dim t2 = WebView22.CoreWebView2.ExecuteScriptAsync(script)
Dim t1 = WebView21.CoreWebView2.ExecuteScriptAsync(script)
Dim wh2 = t2.Result
Dim wh1 = t1.Result
-
Feb 25th, 2022, 10:14 AM
#3
Thread Starter
Junior Member
Re: Webview2 and reading a text box IN the webpage
 Originally Posted by jmcilhinney
You have to actually get the String out of the completed Task. You have two options. You could do this:
vb.net Code:
Dim script = "document.getElementById('WH').value;"
Dim wh2 = Await WebView22.CoreWebView2.ExecuteScriptAsync(script)
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:
Dim script = "document.getElementById('WH').value;"
Dim t2 = WebView22.CoreWebView2.ExecuteScriptAsync(script)
Dim t1 = WebView21.CoreWebView2.ExecuteScriptAsync(script)
Dim wh2 = t2.Result
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.
-
Feb 25th, 2022, 08:29 PM
#4
Re: Webview2 and reading a text box IN the webpage
 Originally Posted by Juliemac57
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.
-
Feb 26th, 2022, 07:16 PM
#5
Thread Starter
Junior Member
Re: Webview2 and reading a text box IN the webpage
 Originally Posted by jmcilhinney
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|