user736893
user736893

Reputation:

pass data/actions between browsercontrol and winform app in vb.net?

I'm a web developer by trade, but I'm trying to write a frontend for game emulators. Since I'm familiar with html/css/javascript, ideally I'd like to do all my layout with that. What I need to know is if that's going to be a showstopper. I'll need to do things like when a user clicks an element on the browser control, make the form code launch something based on a specific path.

Is this possible? If so, is it a terrible idea? Should I stop now and just use the form designer?

Upvotes: 1

Views: 1009

Answers (1)

user736893
user736893

Reputation:

This article helped me: http://www.codeproject.com/Articles/35373/VB-NET-C-and-JavaScript-communication

Specifically:

Add an import statement like Imports System.Security.Permissions in your form1 (main form). Add a couple of attributes to form1, like:

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

then

Calling VB.NET from JavaScript

Continuing with the last project, add the following lines of code in your form1:

     Public Sub showVbHelloWorld()
         MsgBox("Hello world")
     End Sub

In your form1 load event, add this line of code Me.WebBrowser1.ObjectForScripting = Me What this line does is, it exposes your form1 class to the JavaScript on the HTML page. You could expose any class as long as you make it visible to COM and set its permission to fulltrust.

In your HTML page, add a Button, and on its click event, call this function:

    <script type="text/javascript">
       function showVbHelloWorld() {
          window.external.showVbHelloWorld();
       } 
    </script> 

Upvotes: 3

Related Questions