vb.net-获取在Web视图中显示的文本输入框的值,并将其恢复为Winforms

问题描述

我有一个包含Web视图的Winform。 在某些时候,Web视图将显示html内容包括供最终用户在其中输入一些响应的输入框。

我是否可以与Web视图中的输入框进行交互以编程方式获取内容? 即使只是在消息框中显示该文本为例,也可以使我顺利前进!

因此,在下面的示例表单中,我希望抓住最终用户输入到“ userresponse”中的所有内容并将其放入变量中。喜欢...

 dim response as string = 'the contents of the input Box in the webview
 messageBox.show(response)

任何帮助表示感谢,并预先感谢! :)

 <HTML>
     <Body>
      <form>
        <input type="text" id="userresponse" name="userresponse"><br>
      </form>
     </Body>
 </HTML>

解决方法

您可以使用InvokeScriptAsync()来实现。

示例代码:

Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object,e As EventArgs) Handles MyBase.Load
    Dim html As String = File.ReadAllText("test1.html")
    WebView1.NavigateToString(html)
End Sub

Private Async Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click
    Dim ss As String = Await WebView1.InvokeScriptAsync("eval",New String() {"document.getElementById('userresponse').value;"})
    MsgBox(ss)
End Sub

End Class

结果:

enter image description here