Each DOMNodeimplements DOMEventTargetinterface that provides methods for registering DOM events. You can register DOM listener to receive DOM events such asclick,mousedownmouseupkeydownload, error etc. The following sample demonstrates how to create click event listener for a document HTML element:

C#

DOMEventHandler domEvent = delegate(object sender, DOMEventArgs e)
{
     DOMEventType eventType = e.Type;
     Console.Out.WriteLine("handleEvent = " + eventType);
 };

VB.NET

Dim domEvent As DOMEventHandler= New DOMEventHandler(AddressOf Browser_DOMEventHandler)

Private Sub Browser_DOMEventHandler(sender As Object, e As DOMEventArgs)
    Dim eventType As DOMEventType = e.Type
    Console.Out.WriteLine("handleEvent = " + eventType.ToString())
End Sub


The listener is registered as shown below:

C#

DOMElement documentElement = document.DocumentElement;
documentElement.AddEventListener(DOMEventType.OnClick, domEvent, false);

VB.NET

Dim documentElement As DOMElement = document.DocumentElement
documentElement.AddEventListener(DOMEventType.OnClick, domEvent, false)


You can register DOM event listener only for the document of the loaded web page. After reloading the web page, all the registered DOM event listeners will not work anymore, so you need to register the required DOM event listeners again.


Attach a single event listener to several DOM events by a single call


Starting from DotNetBrowser 1.8.2 it is possible to attach a single DOM event listener to several DOM events by a single call. The DOMEventType is a flags enum, and you can combine the flags for the subscription to several DOM events as shown below:

С#

documentElement.AddEventListener(DOMEventType.OnClick | DOMEventType.OnMouseOver, domEvent, false);

VB.NET

documentElement.AddEventListener(DOMEventType.OnClick Or DOMEventType.OnMouseOver, domEvent, False)


Custom DOM Event Types

DotNetBrowser allows you to listen to the custom DOM Event types as well. The following code demonstrates how to listen to the MyEvent DOM Events:

C#

DOMEvent myEvent = browser.CreateEvent("MyEvent");
documentElement.AddEventListener(myEvent, domEvent, false);

VB.NET

Dim myEvent As DOMEvent = browser.CreateEvent("MyEvent")
documentElement.AddEventListener(myEvent, domEvent, False)


WinForms 

C#

Form1.cs

using DotNetBrowser;
using DotNetBrowser.DOM;
using DotNetBrowser.DOM.Events;
using DotNetBrowser.Events;
using DotNetBrowser.WinForms;
using System;
using System.Windows.Forms;


namespace DOMEventsSample
{
    public partial class Form1 : Form
    {
        private BrowserView browserView;
        public Form1()
        {
            InitializeComponent();
            browserView = new WinFormsBrowserView();
            browserView.Browser.DialogHandler = new WinFormsDefaultDialogHandler((Control)browserView.GetComponent());

            this.Controls.Add((Control)browserView.GetComponent());

            DOMEventHandler domEvent = delegate(object sender, DOMEventArgs e)
            {
                DOMEventType eventType = e.Type;
                Console.Out.WriteLine("handleEvent = " + eventType);
            };


            browserView.Browser.FinishLoadingFrameEvent += delegate(object sender, FinishLoadingEventArgs e)
            {
                if (e.IsMainFrame)
                {
                    DOMDocument document = e.Browser.GetDocument();
                    DOMElement documentElement = document.DocumentElement;
                    documentElement.AddEventListener(DOMEventType.OnClick, domEvent, false);
                }
            };

            browserView.Browser.LoadURL("http://www.teamdev.com");
        }
    }
}


VB.NET

Form1.vb

Imports DotNetBrowser
Imports DotNetBrowser.DOM
Imports DotNetBrowser.DOM.Events
Imports DotNetBrowser.Events
Imports DotNetBrowser.WinForms

Public Class Form1

    Dim browserView As BrowserView
    Dim domEvent As DOMEventHandler

    Public Sub New()
        browserView = New WinFormsBrowserView()
        browserView.Browser.DialogHandler = New WinFormsDefaultDialogHandler(browserView.GetComponent())

        Me.Controls.Add(browserView.GetComponent())

        domEvent = New DOMEventHandler(AddressOf Browser_DOMEventHandler)
        AddHandler browserView.Browser.FinishLoadingFrameEvent, AddressOf Browser_FinishLoadingFrameEvent

        browserView.Browser.LoadURL("http://www.teamdev.com")
    End Sub

    Private Sub Browser_DOMEventHandler(sender As Object, e As DOMEventArgs)
        Dim eventType As DOMEventType = e.Type
        Console.Out.WriteLine("handleEvent = " + eventType.ToString())
    End Sub

    Private Sub Browser_FinishLoadingFrameEvent(sender As Object, e As FinishLoadingEventArgs)
        If e.IsMainFrame Then
            Dim document As DOMDocument = e.Browser.GetDocument()
            Dim documentElement As DOMElement = document.DocumentElement
            documentElement.AddEventListener(DOMEventType.OnClick, domEvent, False)
        End If
    End Sub
End Class