Excel加载项function区单击事件冒泡
我正在遵循MSDN上的演练: 使用function区devise器创build自定义选项卡
看第3步和第4步:
在第3步中,它将一个事件处理函数添加到ribbon_Load
函数中,基本上将一个单击事件添加到function区中的button:
private void MyRibbon_Load(object sender, RibbonUIEventArgs e) { this.button1.Click += new RibbonControlEventHandler(this.button1_Click); }
然后,在步骤4中,以我更习惯的方式添加另一个事件处理程序,如下所示:
private void button1_Click(object sender, RibbonControlEventArgs e) { MergeReportInterface ui = new MergeReportInterface(); ui.ShowDialog(); }
我并不真正理解这个目的,因为它所做的只是导致事件发生两次。 如果我注释掉添加到load
函数的事件处理程序,事件就会发生一次。
请问有人可以向我解释这是什么意思? 如果有的话,或者如果MSDN网站上有一些错误。 应该怎样处理一个丝带点击事件?
private void button1_Click(object sender, RibbonControlEventArgs e) { MergeReportInterface ui = new MergeReportInterface(); ui.ShowDialog(); }
这不是添加事件处理程序。 这是您的事件将调用的方法。
this.button1.Click += new RibbonControlEventHandler(this.button1_Click);
这是说'当button1触发它的Click
事件时,调用this.button1_Click
'。
你的代码只设置一个事件处理程序,它应该只触发一次。
但是 ,您可能通过双击表单devise器上的button来创buildbutton1_Click
方法。 这在幕后添加了一个额外的事件处理程序。 这就是为什么你会把事件发射两次。
所以你有两个select:
返回到IDE并通过您的表单devise器删除点击处理程序。 转到您的代码并手动编写方法button1_Click
。
要么
删除这一行: this.button1.Click += new RibbonControlEventHandler(this.button1_Click);
,因为VisualStudio正在为你自动完成。