MenuBar events get lost

A

Axel101

Hello,

I've a problem with an add-in for Visio 2007.

After opening the application I create a new menu bar and add some buttons
with events. Everything is working fine, until I'm opening a new document.
Then all events get lost and the menu elements seem to be recreated, because
they all get a new instanceID.

Any ideas how to fix this?

thanks,
Axel
 
D

David Parker

Visio has two alternative UI object models:
1. UIObject with MenuSets and ToolbarSets - which can be for a particular
drawing or for the application
2. Office.CommandBars

So, you do not give enough information on how you have created your UI.
It could be that you have only created it for your drawing, or it could be
that a new drawing carries code that removes your UI objects.
 
A

Axel101

I use Microsoft.Office.Core.CommandBars for creation.

First I get the active MenuBar:
menuBar = applicationCommandBars.ActiveMenuBar;

Then I add everything I need with:
....menuBar.Controls.Add...

and store the information in a hashtable for further needs.

In another class I add the events like this:
cmdBarButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.createNewJob);

So as I said this works until I open a new document.
(currentApplication.Documents.AddEx("", VisMeasurementSystem.visMSMetric, 0,
0);)

I still can access the menu elements via my hashtable and add events after
the document has been created.

I tried this after the DocOpen and DocCreate events, but the events also get
lost.
 
M

Mark Nelson [MS]

I have encountered a similar problem before where the event handler just
stops getting called after a while. I thought that a specific action was
causing them to stop working, but it turned out that I had declared the
control incorrectly at the very beginning.

When you add your own control to a CommandBar in an Office application, you
must hang onto the control you added via CommandBar.Controls.Add(). If that
object goes out of scope, the event handler is lost. It is not enough that
you are hanging on to an Application object which contains the CommandBar
that you added the control to. The event handler is specifically tied to
your object.

In my case, I declared the control locally thinking that the Click event
handler would be stored as part of the CommandBar holding the control. That
didn't happen so my event handler went out of scope as soon as I finished
setting up the control. Thanks to the delayed garbage collection in managed
code, the control object hung around for a while - long enough to handle
some events. Eventually, GC kicked in and the click events stopped.
Performing complex actions like file I/O are just more likely to trigger GC.

I hope this explanation addresses your situation.

--
Mark Nelson
Office Graphics - Visio
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Axel101

Thanks for the reply. I don't think that the event handler gets lost, because
as long as I don't open a new document my button works well. Maybe there is
something while creating a new document which overwrites this Event.

I tried exactly the same procedure with a CommandBar object instead of a
MenuBar object and it works great - no loss of events happens. It's a
solution, but I'm sill interested in what causes the described problem.
 
O

Oleg

Hi Axel
Mark Nelson is right, I was having exactly the same problem as you described,
but if you declare your Office.CommandBarButton submenuItem1;
as global in the scope of your add-in class then events keep firing
correctly even if you create a new document.

submenuItem1 = Office.CommandBarButton)menuItem.Controls.Add(Office.
MsoControlType.msoControlButton, missing, missing, 1, true);
submenuItem1.Click += new Microsoft.Office.Core.
_CommandBarButtonEvents_ClickEventHandler(submenuItem1_Click);

hope it's not too late

url:http://www.ureader.com/msg/11301507.aspx
 

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