Reputation: 10305
I am following this walkthrough on MSDN: Creating a Custom Tab by Using the Ribbon Designer
Looking at steps 3 and 4:
In step 3 it adds an event handler to the ribbon_Load
function, basically adding a click event to a button in the ribbon:
private void MyRibbon_Load(object sender, RibbonUIEventArgs e)
{
this.button1.Click += new RibbonControlEventHandler(this.button1_Click);
}
Then, in step 4 they add another event handler in the way that I am more used to, like so:
private void button1_Click(object sender, RibbonControlEventArgs e)
{
MergeReportInterface ui = new MergeReportInterface();
ui.ShowDialog();
}
I am not really understanding the purpose of this, because all it does is cause the event to fire twice. If I comment out the event handler that was added to the load
function the event occurs once.
Could someone please explain to me what the point of this is? if there is any, or if there is some error on the MSDN site. What should be the proper way to handle a ribbon click event?
Upvotes: 0
Views: 1432
Reputation: 27357
private void button1_Click(object sender, RibbonControlEventArgs e)
{
MergeReportInterface ui = new MergeReportInterface();
ui.ShowDialog();
}
This is not adding an event handler. This is the method that your event will call.
this.button1.Click += new RibbonControlEventHandler(this.button1_Click);
This is saying 'When button1 fires its Click
event, call this.button1_Click
'.
Your code only sets up one event handler, it should only fire once.
However, it's likely you created the button1_Click
method by double clicking a button on your form designer. This, behind the scenes, adds an additional event handler. This is why you're getting the event fired twice.
So you have two options:
Go back into the IDE and remove the click handler via your form designer. Go to your code and manually write the method button1_Click
.
OR
Remove this line: this.button1.Click += new RibbonControlEventHandler(this.button1_Click);
, as VisualStudio is doing that for you automatically.
Upvotes: 1