CommandBarButton events

A

Amitai

Hi,
All I'm trying to do is trap a CommandBarButton event in Visio Add-in.
The code which creates the button is as follows:
CommandBars ourBars =
(Microsoft.Office.Core.CommandBars)applicationObject.CommandBars;
CommandBar myBar = ourBars.Add("My Tools", MsoBarPosition.msoBarTop, false,
true);
myBar.Visible = true;
CommandBarButton btn1 =
(CommandBarButton)myBar.Controls.Add(MsoControlType.msoControlButton, 1, "",
1,true);
btn1.Visible = true;
btn1.TooltipText = "do jump";
btn1.Caption = "jump";
btn1.Enabled = true;
btn1.FaceId = 3;

Now I try to attach an event handler.
I first tried:
btn1.Click+=new _CommandBarButtonEvents_ClickEventHandler(OnMyClick);
with the function:
public void OnMyClick(CommandBarButton btn, ref bool b)
{
MessageBox.Show("MyClick in action");
}
I got no exception but the event hasn't been fired either. Just nothing.

Then I tried using OnAction.
After the control creation code, I added:
btn1.OnAction = "HelloWorld";
and the function
public void HelloWorld()
{
MessageBox.Show("MyClick in action");
}
but here I got an exception on runtime once I run over the line btn.OnAction
= ...
The Exception I got had the interesting text: "unspecified error".
I tried putting Connect.HelloWorld or MyAddin.Connect.HelloWorld or
HelloWorld()
and tried defining the function static - all with no success.

So how can I trap my CommandBarButton click event in Visio add-in?

Please help
Thanks
Amitai
 
J

Jon Summers

Amitai ...
All I'm trying to do is trap a CommandBarButton event in Visio Add-in.

You may need to add the WithEvent keyword to your class. MSDN
documentation says this:

To expose these events, you must first declare an object variable in a
class module by using the WithEvents keyword. The following code, entered
in the Declarations section of a class module, creates object variables
representing the CommandBars collection, three command bar buttons, and a
combo box control on a custom toolbar:

Public WithEvents colCBars As Office.CommandBars
Public WithEvents cmdBold As Office.CommandBarButton
Public WithEvents cmdItalic As Office.CommandBarButton
Public WithEvents cmdUnderline As Office.CommandBarButton
Public WithEvents cboFontSize As Office.CommandBarComboBox
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top