MSHTML newbie question - button events not working

B

Ben Rush

Hello,

I hope someone out there can help me. This one has been bothering me
for a while and I'm sure it's a result of me not having understood
something properly somewhere along the line. I would very much
appreciate someone to set me straight.

My goal is to be able to trace a user's activity as he/she uses a
instance of the axWebBrowser control on my Windows Form. He/She visits
a site and I want to track their motion. Essentially I want to
investiage and monitor Buttons, DropDownLists, and CheckBox clicks. I
do not want to interfere with their use of the browser; simply monitor
what they do.

The problem I'm finding, however, is that when I start recieving
events from the active document via MSHTML, I somehow corrupt them.
For example, when I click on a button, the button submits, but the
button doesn't visually "push down", and when I click on a Select tag,
the drop down list doesn't Drop. It's like I've muted some of the
events on the page.

The source I have as of now is attached at the bottom of this message
(for the Windows Form). I have removed certain regions to make the
message smaller, etc. in the source code. When I try and trap events
at the document level (in other attempts not show below in the source
code), I can even sometimes kill all events on the page, and I get
notification that a click occured and on what element, but the element
itself never responds to the click.

What have I done? Thank you VERY much in advance,

Ben


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using MsHtmHstInterop;
using System.Runtime.InteropServices;

namespace DomExampleClient
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form,
MsHtmHstInterop.IDocHostUIHandler
{
private System.Windows.Forms.TextBox textBox_URL;
private System.Windows.Forms.Button button_Go;
private AxSHDocVw.AxWebBrowser axWebBrowser1;
private System.Windows.Forms.ListBox listBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
....
....
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button_Go_Click(object sender, System.EventArgs e)
{
System.Object url = textBox_URL.Text;
System.Object o = null;
axWebBrowser1.Navigate2(ref url, ref o, ref o, ref o, ref o);
}

private void Form1_Load(object sender, System.EventArgs e)
{
button_Go_Click(null,null);
}

mshtml.HTMLButtonElementEvents_Event bEvent = null;
private mshtml.IHTMLDocument2 doc;
private void axWebBrowser1_DocumentComplete(object sender,
AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
this.doc = (mshtml.IHTMLDocument2)axWebBrowser1.Document;
this.bEvent = (mshtml.HTMLButtonElementEvents_Event)this.doc.all.item("Button1",0);
this.bEvent.onclick += new
mshtml.HTMLButtonElementEvents_onclickEventHandler(bEvent_onclick);

MsHtmHstInterop.ICustomDoc idoc =
(MsHtmHstInterop.ICustomDoc)this.doc;
idoc.SetUIHandler((MsHtmHstInterop.IDocHostUIHandler)this);
}

#region IDocHostUIHandler Members
....
....
#endregion

private bool bEvent_onclick()
{
listBox1.Items.Add("Button Clicked");
return true;
}
}
}
 
J

James Hancock

Two posibilties here:

1. The bool return values that some of the events have are actually
backwards, so you have to return false where you'd think True and True where
you think false.
2. If you just subscribe directly to these events like you would with
ordinary .net events they're dead after that. What you have to do is
inherit from the interface in a class and instantiate that interface. (i.e.
IHTMLEvents2) Take a look here: http://itwriting.co.uk/phorum/list.php?f=3
Do a search for IHTMLEvents2 and you'll see where I explained how to do
this. Also, Tim's version gets you very much started and gives you edit
mode if you want it.

James Hancock

Ben Rush said:
Hello,

I hope someone out there can help me. This one has been bothering me
for a while and I'm sure it's a result of me not having understood
something properly somewhere along the line. I would very much
appreciate someone to set me straight.

My goal is to be able to trace a user's activity as he/she uses a
instance of the axWebBrowser control on my Windows Form. He/She visits
a site and I want to track their motion. Essentially I want to
investiage and monitor Buttons, DropDownLists, and CheckBox clicks. I
do not want to interfere with their use of the browser; simply monitor
what they do.

The problem I'm finding, however, is that when I start recieving
events from the active document via MSHTML, I somehow corrupt them.
For example, when I click on a button, the button submits, but the
button doesn't visually "push down", and when I click on a Select tag,
the drop down list doesn't Drop. It's like I've muted some of the
events on the page.

The source I have as of now is attached at the bottom of this message
(for the Windows Form). I have removed certain regions to make the
message smaller, etc. in the source code. When I try and trap events
at the document level (in other attempts not show below in the source
code), I can even sometimes kill all events on the page, and I get
notification that a click occured and on what element, but the element
itself never responds to the click.

What have I done? Thank you VERY much in advance,

Ben


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using MsHtmHstInterop;
using System.Runtime.InteropServices;

namespace DomExampleClient
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form,
MsHtmHstInterop.IDocHostUIHandler
{
private System.Windows.Forms.TextBox textBox_URL;
private System.Windows.Forms.Button button_Go;
private AxSHDocVw.AxWebBrowser axWebBrowser1;
private System.Windows.Forms.ListBox listBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
....
....
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button_Go_Click(object sender, System.EventArgs e)
{
System.Object url = textBox_URL.Text;
System.Object o = null;
axWebBrowser1.Navigate2(ref url, ref o, ref o, ref o, ref o);
}

private void Form1_Load(object sender, System.EventArgs e)
{
button_Go_Click(null,null);
}

mshtml.HTMLButtonElementEvents_Event bEvent = null;
private mshtml.IHTMLDocument2 doc;
private void axWebBrowser1_DocumentComplete(object sender,
AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
this.doc = (mshtml.IHTMLDocument2)axWebBrowser1.Document;
this.bEvent =
(mshtml.HTMLButtonElementEvents_Event)this.doc.all.item("Button1",0);
this.bEvent.onclick += new
mshtml.HTMLButtonElementEvents_onclickEventHandler(bEvent_onclick);

MsHtmHstInterop.ICustomDoc idoc =
(MsHtmHstInterop.ICustomDoc)this.doc;
idoc.SetUIHandler((MsHtmHstInterop.IDocHostUIHandler)this);
}

#region IDocHostUIHandler Members
....
....
#endregion

private bool bEvent_onclick()
{
listBox1.Items.Add("Button Clicked");
return true;
}
}
}
 
J

James Hancock

Two posibilties here:

1. The bool return values that some of the events have are actually
backwards, so you have to return false where you'd think True and True where
you think false.
2. If you just subscribe directly to these events like you would with
ordinary .net events they're dead after that. What you have to do is
inherit from the interface in a class and instantiate that interface. (i.e.
IHTMLEvents2) Take a look here: http://itwriting.co.uk/phorum/list.php?f=3
Do a search for IHTMLEvents2 and you'll see where I explained how to do
this. Also, Tim's version gets you very much started and gives you edit
mode if you want it.

James Hancock

Ben Rush said:
Hello,

I hope someone out there can help me. This one has been bothering me
for a while and I'm sure it's a result of me not having understood
something properly somewhere along the line. I would very much
appreciate someone to set me straight.

My goal is to be able to trace a user's activity as he/she uses a
instance of the axWebBrowser control on my Windows Form. He/She visits
a site and I want to track their motion. Essentially I want to
investiage and monitor Buttons, DropDownLists, and CheckBox clicks. I
do not want to interfere with their use of the browser; simply monitor
what they do.

The problem I'm finding, however, is that when I start recieving
events from the active document via MSHTML, I somehow corrupt them.
For example, when I click on a button, the button submits, but the
button doesn't visually "push down", and when I click on a Select tag,
the drop down list doesn't Drop. It's like I've muted some of the
events on the page.

The source I have as of now is attached at the bottom of this message
(for the Windows Form). I have removed certain regions to make the
message smaller, etc. in the source code. When I try and trap events
at the document level (in other attempts not show below in the source
code), I can even sometimes kill all events on the page, and I get
notification that a click occured and on what element, but the element
itself never responds to the click.

What have I done? Thank you VERY much in advance,

Ben


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using MsHtmHstInterop;
using System.Runtime.InteropServices;

namespace DomExampleClient
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form,
MsHtmHstInterop.IDocHostUIHandler
{
private System.Windows.Forms.TextBox textBox_URL;
private System.Windows.Forms.Button button_Go;
private AxSHDocVw.AxWebBrowser axWebBrowser1;
private System.Windows.Forms.ListBox listBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
....
....
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button_Go_Click(object sender, System.EventArgs e)
{
System.Object url = textBox_URL.Text;
System.Object o = null;
axWebBrowser1.Navigate2(ref url, ref o, ref o, ref o, ref o);
}

private void Form1_Load(object sender, System.EventArgs e)
{
button_Go_Click(null,null);
}

mshtml.HTMLButtonElementEvents_Event bEvent = null;
private mshtml.IHTMLDocument2 doc;
private void axWebBrowser1_DocumentComplete(object sender,
AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
this.doc = (mshtml.IHTMLDocument2)axWebBrowser1.Document;
this.bEvent =
(mshtml.HTMLButtonElementEvents_Event)this.doc.all.item("Button1",0);
this.bEvent.onclick += new
mshtml.HTMLButtonElementEvents_onclickEventHandler(bEvent_onclick);

MsHtmHstInterop.ICustomDoc idoc =
(MsHtmHstInterop.ICustomDoc)this.doc;
idoc.SetUIHandler((MsHtmHstInterop.IDocHostUIHandler)this);
}

#region IDocHostUIHandler Members
....
....
#endregion

private bool bEvent_onclick()
{
listBox1.Items.Add("Button Clicked");
return true;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top