CommandBarButton click event for multiple buttons

I need to create my own toolbar with a set of buttons in a Visio window. The following code creates a toolbar (it works), creates buttons (it works), and assigns event handlers to them.

vsoCommandBar = vsoCommandBars.Add("MyCommandBat", Office.MsoBarPosition.msoBarTop, false, true);

var vsoButtonX1 = (Office.CommandBarButton)vsoCommandBar.Controls.Add(
    Office.MsoControlType.msoControlButton, 1, 2, 1, false);
vsoButtonX1.Caption = "Test1";
vsoButtonX1.Tag = "";
vsoButtonX1.Click += GlobalEvents.btnTestClick;

var vsoButtonX2 = (Office.CommandBarButton)vsoCommandBar.Controls.Add(
    Office.MsoControlType.msoControlButton, 1, 2, 2, false);
vsoButtonX2.Caption = "Test2";
vsoButtonX2.Tag = "";
vsoButtonX2.Click += GlobalEvents.btnTest2Click;

      

The handlers are simple but different (of course).

public void btnTestClick(Office.CommandBarButton vsoButton, ref bool cancelDefault)
{
    MessageBox.Show("btnTestClick!");
}

public void btnTest2Click(Office.CommandBarButton vsoButton, ref bool cancelDefault)
{
    MessageBox.Show("btnTest2Click!");
}

      

When I click on any button, I see a dialog with "btnTestClick!". and after clicking "OK" the "btnTest2Click!" dialog box. On either of the two buttons.

Also, if I miss this line:

vsoButtonX2.Click += GlobalEvents.btnTest2Click;

      

I will see one dialog "btnTestClick!" on any button.

It seems to have only one click processor, at least for the command line. It's true?!

PS: Of course, I can use the "vsoButton" parameter and tags to detect when the wht button is pressed, but it seems more convenient to use different event handlers ... Perhaps?

+3


source to share


1 answer


Visio distinguishes buttons with its ... TAGS ...

vsoButtonX1.Tag = "1";
vsoButtonX2.Tag = "2";

      



solves the problem ... I left the question alive, maybe it will help someone ...

+4


source







All Articles