事件处理程序为所有控件而不是单独触发

时间:2021-03-08 15:52:57

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:

我对Gecko Webbrowser控件有一个相当奇怪的问题,我创建了自己的类继承了Gecko Webcontrol,在我的构造函数中我设置了一个事件:

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:

我在手动创建的UserControl中使用其中三个控件,控件在从配置文件启动时动态加载并添加了UserControl控件集合。当点击三个控件中的任何一个时,这三个控件都将远离原始站点导航到“www.foo.com”。我看了看:

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?

这指定它在事件流期间停止事件的进一步传播,但它也指定它将处理当前流中的所有事件,我相信事件必须已经被赋予控件才有机会阻止它因为三个控件仍然会触发事件。我也试过e.Handled = true无济于事。有没有人遇到过这个问题,并且有任何解决方案只能点击被点击的控件?

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;
    }
   }
 }

1 个解决方案

#1


1  

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.

奇怪的答案,但我似乎已经解决了这个问题,DomClick在第一次运行时被调用,改为DomMouseClick或DomMouseUp已完全解决了这个问题。我认为DomClick必须是一个事件本身,因为它也不使用GeckoDomMouseEventArgs而是常规的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.

为了增加这一点,我要去的网站实际上是在完成加载时调用DomClick,因此它是在所有三个浏览器启动时被调用的原因。

#1


1  

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.

奇怪的答案,但我似乎已经解决了这个问题,DomClick在第一次运行时被调用,改为DomMouseClick或DomMouseUp已完全解决了这个问题。我认为DomClick必须是一个事件本身,因为它也不使用GeckoDomMouseEventArgs而是常规的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.

为了增加这一点,我要去的网站实际上是在完成加载时调用DomClick,因此它是在所有三个浏览器启动时被调用的原因。