0

I added a custom method to the Folder.Items.ItemAdd EventHandler during the Button click event in the Ribbon control of a Compose Email.

Now, Every time I click the button the EventHandler Item is getting added again and again.

So, because of it the custom method is calling n number of times for n number of clicks to the button.

I Need to check whether the custom method is already added to that Folder.Items.ItemAdd EventHandler

Please Help : @roopa, @Dmitry

IamIC
  • 17,747
  • 20
  • 91
  • 154
Spartan
  • 13
  • 2

2 Answers2

0

I suppose this is a common programming question which is not related to Outlook or ribbon controls.

In case if you use C#:

Visual Basic .Net:

Community
  • 1
  • 1
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Buddy: I checked the links. the method GetInvocationList, would solve my problem. and that is not available to any of the objects in Outlook. But, I couldn't get the List of Events subscribed to "Folder.Items.ItemAdd". so from that list i can check "whether i have asked the handler to fire a custom method on ItemAdd event" @Eugene – Spartan Feb 06 '15 at 12:20
0

Do you not store the Items object in a separate variable? Check if it is already set and do not set the event handler again.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Eugene: C#4.0 Dmitry: how to check, if it already exists or not. That is my pain point buddy. – Spartan Feb 06 '15 at 18:30
  • You really need to show the relevant snippets of your code. How/where do you keep the object that raises the events? – Dmitry Streblechenko Feb 06 '15 at 20:35
  • Class Ribbon.cs { data i = new data(); RibbonButton_Click() { sentItemsFolder.items.itemsAdd+= new ItemADD_EventHandler(Items_ItemAdd); Mail.send(); } void Items_ItemAdd(object item) { WindowsForm w = new WindowsForm(); w.popup(i); //this will pass the data class details to the form and will display the form. } } On all Email (composing mode), this button will appear. so, every time i click the Ribbon button. this event is getting added again. So, this Items_ItemAdd method is invoked n times for n hits to the Ribbon button. – Spartan Feb 09 '15 at 04:23
  • "sentItemsFolder.items.itemsAdd" would not work - you are using multiple dot notation, so the object that raises the events (sentItemsFolder.items) is stored in a temporary implicit variable. As soon as that variable is garbage collected, no events will be raised. Store the Items object in a separate variable (or list). An extra benefit is that Items.Parent will return the parent folder, so you can easily track whether a particular folder is being tracked by your code. – Dmitry Streblechenko Feb 09 '15 at 19:43