0

Good day

I have made a small website for my google api( because I didn't find a way to use it in my C# Form program). The idea is that when I click on the map icon a event needs to trigger in side the C# Program. I am using the webbrowser tool on my C# From to show my website. I am really hoping this is even possible.

If the above is not possible or if any one has a better idea on how to implement a map on a forms application with icons/buttons it will really help.

I have looked at : Sharpmap -- but my employer doesn't like the look and : ThinkGeo] -- it costs money (still a option)

JvD
  • 473
  • 3
  • 18
  • As in [Invoke C# code from JavaScript in a Document in a WebBrowser](http://stackoverflow.com/questions/3694028/invoke-c-sharp-code-from-javascript-in-a-document-in-a-webbrowser) ? – Alex K. Apr 19 '16 at 13:54

2 Answers2

0

This documentation describes two-way communication between a web browser control and the form on which it resides, which includes raising events from the browser control to be handled by the form.

A few key details:

In the Form_Load event, the control's ObjectForScripting property is set to the form:

webBrowser1.ObjectForScripting = this;

In the HTML within the browser control, window.external is used to access methods in the 'object for scripting.'

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>";

So this:

window.external.Test('called from script code')

calls the Test method within the form, passing called from script code as an argument.

public void Test(String message)
{
    MessageBox.Show(message, "client code");
}
Scott Hannen
  • 27,588
  • 3
  • 45
  • 62
0

Thx Scott I got the From to register my java Script events. All you have to do extra from what Scot said is add these namespaces:

using System.Runtime.InteropServices;
using System.Security.Permissions;

and this above your form obj

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class Form1 : Form
   {...}

I also didn't use webBrowser1.DocumentText I used webBrowser1.Url-- i made the website 1st and just added it. There is problem with this the website needs to be hosted so use XAMPP to local host it(for testing)

JvD
  • 473
  • 3
  • 18