Using a Web Browser with a button and text box linked across 2 forms?

眉间皱痕 提交于 2019-12-14 03:33:30

问题


I have written a piece of a code which takes the postcode from my textbox then searches it within Bing for the user, at the moment all of it is on Form1 and works as it should, I'm looking to improve on the idea by allowing the user to press a button on Form1 then the web browser appearing in Form2. So far my attempts to figure this out have failed and was wondering if anyone had any ideas.

I have attached a copy of my code which all works within in one Form

Public Class Form1

    Dim Automate As Boolean

    Private Sub BTNMap_Click_1(sender As Object, e As EventArgs) Handles BTNMap.Click
        Automate = True
        WebBrowser1.Navigate("http://www.bing.com")
    End Sub


    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As 

        WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        If Automate = True Then Automate = False Else Exit Sub
        Dim txt As HtmlElement = WebBrowser1.Document.GetElementById("q")
        Dim btn As HtmlElement = WebBrowser1.Document.GetElementById("go")

       txt.SetAttribute("value", PostcodeTextBox.Text)
        btn.InvokeMember("click")
    End Sub

End Class

回答1:


Not really the way I would advise doing something like this, but this is the answer to the question as you've asked it:

Form1 needs to contain your Button and TextBox controls. Form2 needs to contain the WebBrowser control.

Form1.vb:

Public Class Form1
    Dim Automate As Boolean

    Private Sub BTNMap_Click_1(sender As Object, e As EventArgs) Handles BTNMap.Click
        Automate = True

        Form2.Automate = Automate
        Form2.SearchInput = PostcodeTextBox.Text
        Form2.WebBrowser1.Navigate("http://www.bing.com")
        Form2.Show()
    End Sub
End Class

Form2.vb:

Public Class Form2
    Public Automate As Boolean
    Public SearchInput As String

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        If Automate = True Then Automate = False Else Exit Sub
        Dim txt As HtmlElement = WebBrowser1.Document.GetElementById("q")
        Dim btn As HtmlElement = WebBrowser1.Document.GetElementById("go")

        txt.SetAttribute("value", SearchInput)
        btn.InvokeMember("click")
    End Sub
End Class



回答2:


Public Class Form1
    Dim Automate As Boolean

    Private Sub BTNMap_Click_1(sender As Object, e As EventArgs) Handles BTNMap.Click
        Automate = True

        Form2.Automate = Automate
        Form2.SearchInput = PostcodeTextBox.Text
        Form2.WebBrowser1.Navigate("http://www.bing.com")
        Form2.Show()
    End Sub
End Class


来源:https://stackoverflow.com/questions/29955021/using-a-web-browser-with-a-button-and-text-box-linked-across-2-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!