Search code examples
c#.neteventsgeckofx

Event handler firing for all controls instead of individually


I am having a rather odd problem with the Gecko Webbrowser control, I have created my own class which inherits off of the Gecko Webcontrol and within the constructor of this I have set an event:

class fooGeckoClass: Gecko.GeckoWebBrowser
{
  public fooGeckoClass()
  {
    this.DomClick += new EventHandler<Gecko.GeckoDomEventArgs>(fooEventFunction);
  }

  private static void fooEventFunction(Object sender, Gecko.GeckoDomEventArgs e)
  {
    ((Gecko.GeckoWebBrowser)sender).Navigate("www.foo.com");
  }
}

I am using three of these controls in a manually created UserControl, the controls are loaded in dynamically at start up from a config file and added the the UserControl controls collection. When clicking on any of the three controls, all three will navigate to "www.foo.com" away from there original site. I had a look at:

e.StopPropagation();

Which specifies that it stops further propagation of events during an event flow, however it does also specify that it will handle all events in the current flow, I believe the events must have already been given to the controls before this has a chance to stop it as the the three controls will still fire the event. I also tried e.Handled = true to no avail. Has anyone encountered this problem before and have any kind of solution to make it only fire on the control that was clicked on?

EDIT:

It may be worth showing how the controls are added to the form seeing as this must be where the problem is occurring (it does not happen if the controls are just placed in a user control in a small test app).

private void fooUserControl_Load(object sender, EventArgs e)
{
  if (!this.DesignMode)
  {
    for (int iControls = 0; iControls < geckObs.Count(); iControls ++)
    {
          fooGeckoClass geckControl = new fooGeckoClass();
          this.Controls.Add(geckControl );
          break;
    }
   }
 }

Solution

  • Odd answer but I seem to have resolved the issue, DomClick was being called at first run, changing to DomMouseClick or DomMouseUp has completely resolved the issue. I assume DomClick must be an event unto itself as it also doesn't use the GeckoDomMouseEventArgs but the regular GeckoEventArgs.

    EDIT:

    To add to this, the site I was going to was actually calling DomClick when it had finished loading hence the reason it was being called at start up across all three browsers.