Customizing Context menu; get duplicates in to-do Bar

Z

Zeus

I am starting an Add-In which so far just modifies the Context menu. It will
do it one way for an email and another for a task. When right click on an
email in the Inbox viewer pane it works great. If I look at the email on my
To-Do bar then, my custom button has been doubled, also if I look at a task
in the To-Do Bar my entires for that get doubled as well.

Here is my ItemContextMenuDisplay handler:

try
{
mail = Selection[1] as Outlook.MailItem;
task = Selection[1] as Outlook.TaskItem;

if (mail != null && task == null)
{
newTaskButton =
CommandBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, missing, missing, missing, true) as Microsoft.Office.Core.CommandBarButton;
newTaskButton.Visible = true;
newTaskButton.Caption = createNewTaskPrompt;
newTaskButton.Click += ConvertMailToTaskAction;
}
else if (task != null && mail == null)
{
startWorkButton =
CommandBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, missing, missing, missing, true) as Microsoft.Office.Core.CommandBarButton;
startWorkButton.Visible = true;
startWorkButton.Caption = startWorkingPrompt;
startWorkButton.Click += startTimerAction;

stopWorkButton =
CommandBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, missing, missing, missing, true) as Microsoft.Office.Core.CommandBarButton;
stopWorkButton.Visible = true;
stopWorkButton.Caption = stopWorkingPrompt;
stopWorkButton.Click += stopTimerAction;
}
}


and Here is my COntextMenuClose handler:

try
{
if (newTaskButton != null)
{
newTaskButton.Click -= ConvertMailToTaskAction;
newTaskButton = null;
}

if (startWorkButton != null)
{
startWorkButton.Click -= startTimerAction;
startWorkButton = null;
}

if (stopWorkButton != null)
{
stopWorkButton.Click -= stopTimerAction;
stopWorkButton = null;
}

mail = null;
task = null;
}

The same code is behaving different and it just has me baffled. I thought
maybe it was getting run twice and made all sorts of logic to check for its
existence and stuff but nothing has prevent duplicates on my to-do bar.

Can anyone see anything I am doing wrong? Or explain the different behavior
between the to-do bar and inbox viewer pane.
 
K

Ken Slovak - [MVP - Outlook]

I don't know if either of these things are affecting what you see, but when
you add your buttons assign them unique Tag properties. Also, instead of
trying to assign Selection[1] to both a task and mail item test the item for
Class to see what it actually is. If it's a task and you assign it to a mail
item the try{} block will fail with an exception and go to your catch{}
block.




Zeus said:
I am starting an Add-In which so far just modifies the Context menu. It
will
do it one way for an email and another for a task. When right click on an
email in the Inbox viewer pane it works great. If I look at the email on
my
To-Do bar then, my custom button has been doubled, also if I look at a
task
in the To-Do Bar my entires for that get doubled as well.

Here is my ItemContextMenuDisplay handler:

try
{
mail = Selection[1] as Outlook.MailItem;
task = Selection[1] as Outlook.TaskItem;

if (mail != null && task == null)
{
newTaskButton =
CommandBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton,
missing, missing, missing, true) as
Microsoft.Office.Core.CommandBarButton;
newTaskButton.Visible = true;
newTaskButton.Caption = createNewTaskPrompt;
newTaskButton.Click += ConvertMailToTaskAction;
}
else if (task != null && mail == null)
{
startWorkButton =
CommandBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton,
missing, missing, missing, true) as
Microsoft.Office.Core.CommandBarButton;
startWorkButton.Visible = true;
startWorkButton.Caption = startWorkingPrompt;
startWorkButton.Click += startTimerAction;

stopWorkButton =
CommandBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton,
missing, missing, missing, true) as
Microsoft.Office.Core.CommandBarButton;
stopWorkButton.Visible = true;
stopWorkButton.Caption = stopWorkingPrompt;
stopWorkButton.Click += stopTimerAction;
}
}


and Here is my COntextMenuClose handler:

try
{
if (newTaskButton != null)
{
newTaskButton.Click -= ConvertMailToTaskAction;
newTaskButton = null;
}

if (startWorkButton != null)
{
startWorkButton.Click -= startTimerAction;
startWorkButton = null;
}

if (stopWorkButton != null)
{
stopWorkButton.Click -= stopTimerAction;
stopWorkButton = null;
}

mail = null;
task = null;
}

The same code is behaving different and it just has me baffled. I thought
maybe it was getting run twice and made all sorts of logic to check for
its
existence and stuff but nothing has prevent duplicates on my to-do bar.

Can anyone see anything I am doing wrong? Or explain the different
behavior
between the to-do bar and inbox viewer pane.
 
Z

Zeus

Thanks Ken,
I implemented both of your suggestions, they did not fix my issue, but I
appreciate knowing how to do things in the more prescribed manner. I will
make sure to do things this way from now on.

Ken Slovak - said:
I don't know if either of these things are affecting what you see, but when
you add your buttons assign them unique Tag properties. Also, instead of
trying to assign Selection[1] to both a task and mail item test the item for
Class to see what it actually is. If it's a task and you assign it to a mail
item the try{} block will fail with an exception and go to your catch{}
block.




Zeus said:
I am starting an Add-In which so far just modifies the Context menu. It
will
do it one way for an email and another for a task. When right click on an
email in the Inbox viewer pane it works great. If I look at the email on
my
To-Do bar then, my custom button has been doubled, also if I look at a
task
in the To-Do Bar my entires for that get doubled as well.

Here is my ItemContextMenuDisplay handler:

try
{
mail = Selection[1] as Outlook.MailItem;
task = Selection[1] as Outlook.TaskItem;

if (mail != null && task == null)
{
newTaskButton =
CommandBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton,
missing, missing, missing, true) as
Microsoft.Office.Core.CommandBarButton;
newTaskButton.Visible = true;
newTaskButton.Caption = createNewTaskPrompt;
newTaskButton.Click += ConvertMailToTaskAction;
}
else if (task != null && mail == null)
{
startWorkButton =
CommandBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton,
missing, missing, missing, true) as
Microsoft.Office.Core.CommandBarButton;
startWorkButton.Visible = true;
startWorkButton.Caption = startWorkingPrompt;
startWorkButton.Click += startTimerAction;

stopWorkButton =
CommandBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton,
missing, missing, missing, true) as
Microsoft.Office.Core.CommandBarButton;
stopWorkButton.Visible = true;
stopWorkButton.Caption = stopWorkingPrompt;
stopWorkButton.Click += stopTimerAction;
}
}


and Here is my COntextMenuClose handler:

try
{
if (newTaskButton != null)
{
newTaskButton.Click -= ConvertMailToTaskAction;
newTaskButton = null;
}

if (startWorkButton != null)
{
startWorkButton.Click -= startTimerAction;
startWorkButton = null;
}

if (stopWorkButton != null)
{
stopWorkButton.Click -= stopTimerAction;
stopWorkButton = null;
}

mail = null;
task = null;
}

The same code is behaving different and it just has me baffled. I thought
maybe it was getting run twice and made all sorts of logic to check for
its
existence and stuff but nothing has prevent duplicates on my to-do bar.

Can anyone see anything I am doing wrong? Or explain the different
behavior
between the to-do bar and inbox viewer pane.
 
Top