J
Joseph Geretz
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm
adding matching pairs of Toolbar buttons and Menu items. All of the buttons
and menu items are wired up to send events to the same method (aka
delegate?). I use the Tag property within this method to determine what user
action is taking place. Very simple:
When adding toolbar button:
tbButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.User_Click);
When adding Menu item:
MenuItem.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.User_Click);
private void User_Click(CommandBarButton cmdButton, ref bool cancel)
{
MessageBox.Show(cmdButton.Tag);
}
Right now, all I've got is a simple dialog box to show me what's going on.
Strangely enough, if I use the same Tag for a Menu item and its
corresponding Toolbar button (This makes sense for me, since for any given
action, I don't really care if the user clicked a Menu or a Toolbar button)
I get two message boxes. Why two??? Surely, the delegate method is wired up
per object instance and not tracked by the Tag of the object? But seemingly,
when I click a Menu I get the event for the menu and the corresponding event
for the corresponding toolbar button as well! And vice versa. This is very
strange.
If I change the Tags so they don't match (e.g. Find|Toolbar and Find|Menu as
opposed to simply Find and Find) then I get the expected behavior. It's easy
enough for me to do this, and then strip out the basic tag value as follows:
MessageBox.Show("Viewpoint - " +
cmdButton.Tag.Remove(cmdButton.Tag.IndexOf("|")));
However, I'm puzzled by why this should all be necessary. Can you explain
this to me?
Thanks very much for your help!
- Joseph Geretz -
adding matching pairs of Toolbar buttons and Menu items. All of the buttons
and menu items are wired up to send events to the same method (aka
delegate?). I use the Tag property within this method to determine what user
action is taking place. Very simple:
When adding toolbar button:
tbButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.User_Click);
When adding Menu item:
MenuItem.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.User_Click);
private void User_Click(CommandBarButton cmdButton, ref bool cancel)
{
MessageBox.Show(cmdButton.Tag);
}
Right now, all I've got is a simple dialog box to show me what's going on.
Strangely enough, if I use the same Tag for a Menu item and its
corresponding Toolbar button (This makes sense for me, since for any given
action, I don't really care if the user clicked a Menu or a Toolbar button)
I get two message boxes. Why two??? Surely, the delegate method is wired up
per object instance and not tracked by the Tag of the object? But seemingly,
when I click a Menu I get the event for the menu and the corresponding event
for the corresponding toolbar button as well! And vice versa. This is very
strange.
If I change the Tags so they don't match (e.g. Find|Toolbar and Find|Menu as
opposed to simply Find and Find) then I get the expected behavior. It's easy
enough for me to do this, and then strip out the basic tag value as follows:
MessageBox.Show("Viewpoint - " +
cmdButton.Tag.Remove(cmdButton.Tag.IndexOf("|")));
However, I'm puzzled by why this should all be necessary. Can you explain
this to me?
Thanks very much for your help!
- Joseph Geretz -