3

I want to add click event of webBrowser control. This is my code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        HtmlDocument htmlDoc; 
        public Form1()
        {
            InitializeComponent();
            OpenFileDialog open = new OpenFileDialog();
            open.ShowDialog();
            this.webBrowser1.Navigate(open.FileName);

        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webBrowser1.Document != null)
            {
                htmlDoc = webBrowser1.Document;
                htmlDoc.Click += new HtmlElementEventHandler(htmlDoc_Click);
            }
        }
        private void htmlDoc_Click(object sender, HtmlElementEventArgs e)
        {
            MessageBox.Show("Click");
        }

    }
}

I want it to diplay a .ppt file. It is ok to display but when I click the webBrowser there is no messagebox show up. Is there any other solution? thanks

Tony Lin
  • 765
  • 3
  • 15
  • 35
  • You can find the answer in http://stackoverflow.com/questions/9110388/web-browser-control-how-to-capture-document-events – srsyogesh Jul 09 '13 at 05:39
  • thanks, but it doesn't solve my problem. I can't use mshtml in the link you gave. – Tony Lin Jul 09 '13 at 05:54
  • is there any special reason because of which you can't use this library ?? – srsyogesh Jul 09 '13 at 06:07
  • actually i dont know how to include this library – Tony Lin Jul 09 '13 at 06:11
  • doc = (mshtml.HTMLDocument)webBrowser1.Document; it says type can not changed from 'System.Windows.Forms.HtmlDocument' to 'mshtml.HTMLDocument' – Tony Lin Jul 09 '13 at 06:17
  • declare the doc variable as mshtml.HTMLDocument doc = (mshtml.HTMLDocument)webBrowser1.Document . The precondition is you have to add a reference to the library (mshtml) as mentioned in the article. Still if you face problem in adding a reference do let us know. – srsyogesh Jul 09 '13 at 06:20
  • @srsyogesh my declaration is `mshtml.HTMLDocument doc; doc = (mshtml.HTMLDocument)webBrowser1.Document; mshtml.HTMLDocumentEvents2_Event iEvent; iEvent = (mshtml.HTMLDocumentEvents2_Event)doc; iEvent.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(ClickEventHandler);` still wrong – Tony Lin Jul 09 '13 at 08:09
  • Can you explain in detail what is wrong , compiler error or runtime error (any exception) ? – srsyogesh Jul 09 '13 at 13:53

1 Answers1

9

I'm using ObjectForScripting to do these kind of things. It allows JavaScript to call a C#-Method. Let JavaScript react on events, thats much easier and you don't need MSHTML. It's nicely explained here. Youl'll need using System.Runtime.InteropServices; for it to work, so that the app knows the ComVisible-Annotation.

You don't have to be a JavaScript-pro to use this. For example: Just add a button like this:

<button onclick="javascript:window.external.showPpt('test.ppt');">Click me</button>

which will call a method named showPpt in the ObjectForScripting. Remember: You may create the HTML using C# too. This way you can store information in the document. Here is a full example:

public partial class frmBrowser : Form
{
    HtmlDocument doc;

    [ComVisible(true)]
    public class ScriptManager
    {
        private frmBrowser mForm;

        public ScriptManager(frmBrowser form)
        {
             mForm = form;
        }

        public void recall(string statusID)
        {
             mForm.RestoreStatus(statusID);
        }
    }

    public frmBrowser()
    {
        InitializeComponent();
        this.webBrowser.ObjectForScripting = new ScriptManager(this);
        string url = "file:///" + Application.StartupPath + "\\start\\start.html";
        this.webBrowser.Navigate(url);
    }

    private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        doc = webBrowser.Document;
        showRecent();
    }

    private void showRecent()
    {
        HtmlElement divRecent = doc.GetElementById("recent_cont");
        List<DaoStatus> status = Center.DB.GetUIStatus();
        string html = "";
        foreach (DaoStatus st in status)
        {
             html += "<button onclick=\"javascript:window.external.recall('" + st.ID + "');\">" + st.Name + "</button>";
        }
        divRecent.InnerHtml = html;
    }
}

The webBrowser control navigates to a local file. If fully loaded showRecent() is called, which gets information from a database and creates buttons accordingly using a div-Element with the id "recent_cont".

user414873
  • 741
  • 8
  • 20