Loading

Invoking JavaScript function from WinForms

Submitted by: 
Visitors have accessed this post 9133 times.


I've been working on application that needs to load page and display it in WebBrowser control. Requirement was to allow interaction between WinForms application and web page.

Here I will show a sample application where JavaScript function is called from WinForms application.
For this Blog I will be using Google Web elements , Google Web Elements allow you to easily add your favorite Google products to your website.

http://www.google.com/webelements/#!/custom-search

Process can be described by following steps:

Create Windows Forms application
Drop WebBrowser control into Form
Create Html with JavaScript function you want to call

Calling JavaScript function from WinForms application isn't so difficult.
.NET WebBrowser control has property Document which gets an HtmlDocument representing the Web page currently displayed in the WebBrowser control.

Well this javascript part is not well written ,maybe my Web God-Like Buddy (Rustan) can clean this mess..lol

anyway here's the script

JAVASCRIPT

  1. <script type="text/javascript">
  2.  
  3. google.load("elements", "1", {packages: "keyboard"});
  4.  
  5. var kbd;
  6.  
  7. function onLoad() {
  8. var content = document.getElementById('content');
  9. // Create the HTML for out text area
  10. content.innerHTML = '<div>' +
  11. 'Demo of Invoking JavaScript using .NET' +
  12. '.</div>' +
  13. '<textarea id="t1" cols="47" rows="7"></textarea>';
  14.  
  15. kbd = new google.elements.keyboard.Keyboard(
  16. [google.elements.keyboard.LayoutCode.ENGLISH],
  17. ['t1']);
  18. }
  19.  
  20. function ChangeLayout() {
  21.  
  22. var LayOut=document.getElementById('txtLang').value;
  23. kbd.setLayout(LayOut);
  24.  
  25. }
  26.  
  27.  
  28. function toggleVisible() {
  29.  
  30. if (kbd.isVisible()) {
  31. kbd.setVisible(false);
  32. } else {
  33. kbd.setVisible(true);
  34.  
  35. }
  36. }
  37.  
  38.  
  39. google.setOnLoadCallback(onLoad);
  40.  
  41. </script>

So how can you call JavaScript functions in your WebBrowser control? Call the InvokeScript method on the HtmlDocument.

.NET CODE (winforms)

  1. Sub SHOW_HIDE_VIR_KEYBOARD()
  2. With Me.WBrowser
  3.  
  4. .Document.InvokeScript("toggleVisible", New Object() {""}) 'call the javascript from the .htm files
  5.  
  6. End With
  7. End Sub
  8.  
  9. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShow.Click
  10. SHOW_HIDE_VIR_KEYBOARD()
  11. End Sub
  12.  
  13.  
  14. Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  15. With Me.WBrowser
  16. .Document.GetElementById("txtLang").InnerText = Trim(Me.ComboBox1.Text) 'parse data from winforms to Htm files
  17. .Document.InvokeScript("ChangeLayout", New Object() {""}) 'call the javascript from the .htm files
  18.  
  19. End With
  20. End Sub
  21.  
  22.  

Conclusions:

With the ability to call JavaScript from .NET App you can now embed and extend web applications into native applications with ease. A native application gives you more control over the environment and access to computer resources that you cannot access from a web page. You can Mashup web applications with computer hardware or software in new and interesting ways.

For the complete sample please download the demo project

For questions just Contact me @ the Addresses below:

mobile : +639399393702

http://www.emondsoft-solutions.com/

Facebook Fan Page

http://www.facebook.com/pages/emondsoft/120502167993693

Follow me on twitter:

http://twitter.com/#!/emond07




Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
To prevent automated spam submissions leave this field empty.

Filtered HTML

  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.