How to delete the controls in a CommandBarPopup?

T

Teodora Gancheva

Hi,
I am developing a C# COM add-in for MS Office 2003 and 2007. My add-in
creates a custom command bar with a CommandBarPopup menu containing several
CommandBarPopup submenus populated with CommandBarButton objects. Upon user
click on some buttons, I'd like to perform 'refresh' on the submenus i.e.
delete the buttons and re-create them.
I am doing so using the following code:
for (int index = 1; index <= popup.Controls.Count; index++)
{
popup.Controls[index].Delete(1);
index--;
}
This works for all buttons except the one that was actually clicked, for it
a COMException with message "Error HRESULT E_FAIL has been returned from a
call to a COM component" is thrown. Am I not allowed to delete this button
because it has just been clicked, and what other options do I have - delete
the whole popup?
Thanks for all advice,
Teodora Gancheva
 
K

Ken Slovak - [MVP - Outlook]

It would be logical that you couldn't delete a button during its Click()
event. Think about it.

If you really want to do what you're doing then set a timer in the Click()
event that will fire after that event is completed, then do your refresh in
the timer event handler. Just make sure the timer thread is the same or
marshalled to the main thread.
 

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