0

Hi I have a bit of a "Why would you even bother" question, but its important for my app Im trying to build (that's the long story short).

My app consists of two side by side web browsers a trigger in one (example is a button), and an associated action in the second (an example an alert pop-up) displayed on the other is this possible using the vb.net language?

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
LabRat
  • 1,996
  • 11
  • 56
  • 91
  • I think your best bet is to capture a click event from the first browser, then programmatically trigger something in the second. See [this](https://stackoverflow.com/a/20921284) and [this](https://stackoverflow.com/a/14934426). – Visual Vincent Nov 03 '17 at 08:33

1 Answers1

0

This is possible to a limited degree assuming you control the content that they are hosting.

The good news is that with the ObjectForScripting property you can pass data back and forth from javascript to .net and then again so that one web browser can react to the other.

The other bit of good news is that they are going to use the same WinInet cache. So if you create a cookie for the site, both browsers could potentially read it if it was a session cookie or such.

Although I would have to know a bit more about what you had in mind to give you a better and more detailed answer.

Here is some sample code nabbed from the MSDN on objectforscipting that shows how to interact with a single web browser control

    Imports System
    Imports System.Windows.Forms
    Imports System.Security.Permissions

    <PermissionSet(SecurityAction.Demand, Name:="FullTrust")>
    <System.Runtime.InteropServices.ComVisibleAttribute(True)>
    Public Class Form1
        Inherits Form

        Private webBrowser1 As New WebBrowser()
        Private WithEvents button1 As New Button()

        <STAThread()>
        Public Shared Sub Main()
            Application.EnableVisualStyles()
            Application.Run(New Form1())
        End Sub

        Public Sub New()
            button1.Text = "call script code from client code"
            button1.Dock = DockStyle.Top
            webBrowser1.Dock = DockStyle.Fill
            Controls.Add(webBrowser1)
            Controls.Add(button1)
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
            Handles Me.Load

            webBrowser1.AllowWebBrowserDrop = False
            webBrowser1.IsWebBrowserContextMenuEnabled = False
            webBrowser1.WebBrowserShortcutsEnabled = False
            webBrowser1.ObjectForScripting = Me
            ' Uncomment the following line when you are finished debugging.
            'webBrowser1.ScriptErrorsSuppressed = True

            webBrowser1.DocumentText =
                "<html><head><script>" &
                "function test(message) { alert(message); }" &
                "</script></head><body><button " &
                "onclick=""window.external.Test('called from script code')"" > " &
                "call client code from script code</button>" &
                "</body></html>"
        End Sub

        Public Sub Test(ByVal message As String)
            MessageBox.Show(message, "client code")
        End Sub

        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
            Handles button1.Click

            webBrowser1.Document.InvokeScript("test",
                New String() {"called from client code"})

        End Sub

    End Class
Alexander Ryan Baggett
  • 2,347
  • 4
  • 34
  • 61