The wonderful weird world of everything and nothing

aka The "Don't Panic" button!

A major bug in using managed Outlook command popup/buttons.

Scenario:

You want to create an addin to Outlook to extend the menu system so you use the VS wizard to create the addin code and then begin adding popup/button code. If you code ala standard .NET object programming and your menu has more than 1 button then you are doomed to failure due to a major 'gotcha'. Let me walk you through it and how to solve it. Here is a 'standard' vanilla code example that we all could code with our eyes shut.

CommandPopup PopupMenu;
CommandButton MenuButton;

// create 1st button
MenuButton = PopupMenu.Add(...);
MenuButton.Caption = "Publish"
MenuButton.OnClickEvent += ...

// create 2nd button
MenuButton = PopupMenu.Add(...);
MenuButton.Caption = "Subscribe"
MenuButton.OnClickEvent += ...

By now you see the pattern and think all is well and good but this will NEVER work with Outlook.

Outlook (or the CommandPopup code) maintains an ugly list somewhere of the buttons created and as soon as you create the 2nd button that code loses a reference to the OnClick event upon garbage collection. So your first invocation of the code works but upon exit is GC'd and when you go to use it the 2nd time there is no event attached to the button.

Solution: Each button MUST be a unique object hence you must code the following (or similar to):

CommandPopup PopupMenu;
CommandButton [] MenuButtons;

MenuButtons = new CommandButton[2];

// create 1st button
MenuButtons[0] = PopupMenu.Add(...);
MenuButtons[0].Caption = "Publish"
MenuButtons[0].OnClickEvent += ...

// create 2nd button
MenuButtons[1] = PopupMenu.Add(...);
MenuButtons[1].Caption = "Subscribe"
MenuButtons[1].OnClickEvent += ...

Well, this satisfies the unique button object problem and so you would think that there is no further problems///but this doesn't work either YET.

Each .NET object has a 'Tag' property that gets a 'standard' value and is not really used by anyone...think again! The CommandPopup/Outlook code uses this field to differentiate the buttons. Never mind that in .NET each object is a unique object and thus can be tracked individually. NOOOO! The code uses the Tag property to track each object and since each object has the standard identifier then each object 'looks' the same.

Solution: Given each tag a unique value thus:
CommandPopup PopupMenu;
CommandButton [] MenuButtons;

MenuButtons = new CommandButton[2];

// create 1st button
MenuButtons[0] = PopupMenu.Add(...);
MenuButtons[0].Caption = "Publish"
MenuButtons[0].Tag = Guid.NewGuid().ToString();
MenuButtons[0].OnClickEvent += ...

// create 2nd button
MenuButtons[1] = PopupMenu.Add(...);
MenuButtons[1].Caption = "Subscribe"
MenuButtons[1].Tag = Guid.NewGuid().ToString();
MenuButtons[1].OnClickEvent += ...

NOW your code works fine! Wasn't that just so intuitive! I wasted 4 days trying to solve this and only after a late night session with the Outlook guys did we find the reasons.

BTW, why do all the samples on the Internet work? They only have 1 button!

Published Wednesday, July 28, 2004 10:31 AM by robertcol
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker