Question

As the title suggest I am looking for a way to get the Item which fired the event.

I Bind Property_Change Event in the Application_ItemLoad event in which I have a object Item which it seems I can not use.

After the Property_Change is fired I need the Item which fired the event in order to call my function. Is there a way to get this? (I know I can get the current open Item via ActiveExplorer, but if the Items is modified in the main Window I get null).

Code: ThisAddIn.cs

private Explorer _Explorer;
private List<WrapperItem> list = new List<WrapperItem>;

private void ThisAddIn_Startup(object sender, System.EventArgs e){
   _Explorer = this.Application.ActiveExplorer();
   _Explorer.SelectioNChange += _Explorer_SelectionChange;
}

void _Explorer_SelectionChange()
{
     list.Clear();
     foreach (MailItem mail in _Explorer.Selection)
     {
         list.Add(new WrapperItem(mail));
     }
 }
Was it helpful?

Solution

You are setting an event sink on a particular item, right? What is your code?

If you are tracking events from multiple objects, the rule of thumb is to create your own wrapper class that will contain a pointer to the object raising the event and implement the event handler in the same wrapper class. This way when an event fires, you will know which object raised it.

A sample wrapper (off the top of my head) can look something like

public class MailItemWrapper
{
  public MailItem item;
  public MailItemWrapper(MailItem OutlookItem)
  {
    item = OutlookItem;
    item.PropertyChange += new System.EventHandler(PropertyChangeHandler);
  }
  private PropertyChangeHandler(string Name)
  {
    MessageBox.Show(string.Format("Property named {0} changed on item {1}", name, item.Subject))
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top