Outlook 2k3 Shared Add-in Events not firing as they should

V

Vaelek

I am developing a shared addin for Outlook 2003 with VS2005 in C#.
I have created wrapper functions to create a commandbar, commandbarpopup,
and commandbarbuttons. Initially the relative CommandBar* objects were
defined both at the class level, and within the functions that added the new
ones, which simply returned the created object which was then assigned to the
global object. This seemed to work but my events would stop firing after a
few minutes.
After searching for a while I found that the objects must be declared at the
class level so I moved my temporary object declarations there as well. My
problem now is this: The only event that fires is the one attatched to the
last CommandBarButton added. I am reusing a global object to add the items
and I suspect this is where the problem is coming from.

My globals are:
//These 2 are used repeatedly during item creation
private CommandBarPopup SubMenu;
private CommandBarButton MenuItem;

//These 2 are used within the wrapper functions
private CommandBarControl _item;
private CommandBarControl _popup;

my functions look like this:

private CommandBarPopup AddPopupMenu(string Caption, CommandBarControl
_parent)
{
object mis = Missing.Value;
_popup = (_parent as
CommandBarPopup).Controls.Add(MsoControlType.msoControlPopup, mis, mis, mis,
true);
_popup.Caption = Caption;

return (_popup as CommandBarPopup);
}

private CommandBarControl AddMenuItem(string Caption, CommandBarPopup _parent)
{
object mis = Missing.Value;
_item = _parent.Controls.Add(MsoControlType.msoControlButton, mis, mis,
mis, true);
_item.Caption = Caption;

return _item;
}

I build the menu as such

SubMenu = AddPopupMenu("This is a popup menu");
SubMenu = AddPopupMenu("This is a submenu");
MenuItem = (CommandBarButton)AddMenuItem("Menu Item");
MenuItem.Click += EventMethod1
MenuItem = (CommandBarButton)AddMenuItem("Another Item");
MenuItem.Click += EventMethod2

There is a little more going on as I have the appropriate parents tracked in
overrides of the above functions, but that is not the issue, what is above
works with what I have. The problem, is that given the above code,
EventMethod1 will never be executed. EventMethod2 will work just fine.

What do I have to do to be able to build the menus in this fashion? I
absolutely do *NOT* want to have to create an object to hold each and every
item as it is generated dynamically.

HELP!
THANKS!
 
D

Dmitry Streblechenko

You must always keep the object that will be raising events alive, otherwise
GC releases the object and no events will be fired:

MenuItem = (CommandBarButton)AddMenuItem("Menu Item");
MenuItem.Click += EventMethod1
MenuItem2 = (CommandBarButton)AddMenuItem("Another Item");
MenuItem2.Click += EventMethod2

Note MenuItem2 instead of MenuItem. MenuItem2 (just like MenuItem) must be
declared as s global (class) bariable so it stays referenced.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
V

Vaelek

There must be another way becuase I have seen numerous posts from people who
have implemented dynamicly built menus in addins. What I have done to solve
it is declare globally as

CommandBarButton[] MenuItems;

and then once it is known how many items there will be, it is "redimed" as
MenuItems = new CommandBarButton[ItemCount];

From there it is just a matter of keeping an index to add the items and
events to the appropriate object. This would be much cleaner if it were
possible to simply do MenuItem = new CommandBarButton(); but as you know that
is not possible.
 
D

Dmitry Streblechenko

No, there is no other way.
What I usually do is create a wrapper class for each explorer and inspector
where I insert buttons, each class in turn contains a list of buttons.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool

Vaelek said:
There must be another way becuase I have seen numerous posts from people
who
have implemented dynamicly built menus in addins. What I have done to
solve
it is declare globally as

CommandBarButton[] MenuItems;

and then once it is known how many items there will be, it is "redimed" as
MenuItems = new CommandBarButton[ItemCount];

From there it is just a matter of keeping an index to add the items and
events to the appropriate object. This would be much cleaner if it were
possible to simply do MenuItem = new CommandBarButton(); but as you know
that
is not possible.

Dmitry Streblechenko said:
You must always keep the object that will be raising events alive,
otherwise
GC releases the object and no events will be fired:

MenuItem = (CommandBarButton)AddMenuItem("Menu Item");
MenuItem.Click += EventMethod1
MenuItem2 = (CommandBarButton)AddMenuItem("Another Item");
MenuItem2.Click += EventMethod2

Note MenuItem2 instead of MenuItem. MenuItem2 (just like MenuItem) must
be
declared as s global (class) bariable so it stays referenced.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 

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