简体   繁体   English

事件控件触发所有控件,而不是单独触发

[英]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: 我在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. 我在手动创建的UserControl中使用了其中三个控件,这些控件在启动时从配置文件中动态加载,并添加了UserControl控件集合。 When clicking on any of the three controls, all three will navigate to "www.foo.com" away from there original site. 当单击三个控件中的任何一个时,所有三个控件都将导航到远离原始站点的“ www.foo.com”。 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. 我也尝试过e.Handled = true无济于事。 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;
    }
   }
 }

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. 奇怪的答案,但我似乎已经解决了该问题,DomClick在首次运行时被调用,更改为DomMouseClick或DomMouseUp已完全解决了该问题。 I assume DomClick must be an event unto itself as it also doesn't use the GeckoDomMouseEventArgs but the regular GeckoEventArgs. 我认为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,因此在所有三个浏览器中启动该网站的原因都是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM