_Commandbar button event handler for multiple buttons

E

Exchnerd

Hi,

I have a COM add-in that is a toolbar for outlook and has some
controls on it - buttons and combo boxes. I am able to successfully
register event handlers for them. However, when I have more than 1
button, I don't receive the callback for the second button.

Is there something that needs to be done to receive callbacks for the
2nd button? I thought the handler has an input param, IDispatch* which
we can be used for verifying which button was clicked. However, I fail
to receive callbacks for both the buttons. I get for either of them at
any point of time depending on which button I used to register.

Thanks,
 
E

Exchnerd

Use unique Tag properties for each of your controls.

If you are referring to using the put_Tag method, I have already done
that and each of them have unique strings.
My 2nd registration attempt fails and I understand that you cannot
have 2 handlers for the same component. Am I right if I say that the
unique tags would suffice to have a single DispEventAdvice for buttons
and I will receive callbacks for both the buttons?
 
K

Ken Slovak - [MVP - Outlook]

Each button is a different object and needs its own click event handler.




<snip>
If you are referring to using the put_Tag method, I have already done
that and each of them have unique strings.
My 2nd registration attempt fails and I understand that you cannot
have 2 handlers for the same component. Am I right if I say that the
unique tags would suffice to have a single DispEventAdvice for buttons
and I will receive callbacks for both the buttons?
 
E

Exchnerd

Each button is a different object and needs its own click event handler.




<snip>
If you are referring to using the put_Tag method, I have already done
that and each of them have unique strings.
My 2nd registration attempt fails and I understand that you cannot
have 2 handlers for the same component. Am I right if I say that the
unique tags would suffice to have a single DispEventAdvice for buttons
and I will receive callbacks for both the buttons?

Can you be more clear?
Do I need to register twice? - meaning call DispEventAdvise for each
button? The second call fails for me.
How do I map the event handlers for the buttons?
 
K

Ken Slovak - [MVP - Outlook]

I don't do C++ but in any other language I'd just add one separate click
event procedure for each button and I'd hook up those handlers to their
corresponding button's Click events. This is what I'd do in C#:

button1.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(button1Click);
button2.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(button2Click);
etc.




<snip>Can you be more clear?
Do I need to register twice? - meaning call DispEventAdvise for each
button? The second call fails for me.
How do I map the event handlers for the buttons?
 
E

Exchnerd

I don't do C++ but in any other language I'd just add one separate click
event procedure for each button and I'd hook up those handlers to their
corresponding button's Click events. This is what I'd do in C#:

button1.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(button1Click);
button2.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(button2Click);
etc.




<snip>Can you be more clear?
Do I need to register twice? - meaning call DispEventAdvise for each
button? The second call fails for me.
How do I map the event handlers for the buttons?

that means I will have to give a new sink for every button and
register/unregister for the events (for each button added)
 
K

Ken Slovak - [MVP - Outlook]

Yes, that's what I've been telling you.




<snip>
that means I will have to give a new sink for every button and
register/unregister for the events (for each button added)
 
E

Exchnerd

Yes, that's what I've been telling you.




<snip>
that means I will have to give a new sink for every button and
register/unregister for the events (for each button added)

Thanks! It works now with 2 entry for the SINK and 2 DispEventAdvise
 
C

cainrandom

Thanks! It works now with 2 entry for the SINK and 2 DispEventAdvise

You can use the same object for multiple sinks, like so:

class CButtonEventSink :
public IDispEventSimpleImpl</*nID =*/ 1, CButtonEventSink, &__uuidof
(Office::_CommandBarButtonEvents)>,
public IDispEventSimpleImpl</*nID =*/ 2, CButtonEventSink, &__uuidof
(Office::_CommandBarButtonEvents)>

And then:
IDispEventSimpleImpl< 1, CButtonEventSink, &__uuidof
(Outlook::CommandBarButtonEvents)>::DispEventAdvise( mButton1);
IDispEventSimpleImpl< 2, CButtonEventSink, &__uuidof
(Outlook::CommandBarButtonEvents)>::DispEventAdvise( mButton2);

However, while I've tried to be economical with regard to the number
of objects, I usually find in the end that with ATL and Outlook add-
ins that you're better off creating one sink object per OOM object.

P.S. Don't forget to override AddRef/Release, especially for your
button event sinks.
 
E

Exchnerd

You can use the same object for multiple sinks, like so:

class CButtonEventSink :
public IDispEventSimpleImpl</*nID =*/ 1, CButtonEventSink, &__uuidof
(Office::_CommandBarButtonEvents)>,
public IDispEventSimpleImpl</*nID =*/ 2, CButtonEventSink, &__uuidof
(Office::_CommandBarButtonEvents)>

And then:
IDispEventSimpleImpl< 1, CButtonEventSink, &__uuidof
(Outlook::CommandBarButtonEvents)>::DispEventAdvise( mButton1);
IDispEventSimpleImpl< 2, CButtonEventSink, &__uuidof
(Outlook::CommandBarButtonEvents)>::DispEventAdvise( mButton2);

However, while I've tried to be economical with regard to the number
of objects, I usually find in the end that with ATL and Outlook add-
ins that you're better off creating one sink object per OOM object.

P.S. Don't forget to override AddRef/Release, especially for your
button event sinks.

Yes, I had done the same. I was wondering if 1 sink was enough for all
the buttons!
Thanks for your help,
 

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