NetOfficeFw / NetOffice

🌌 Create add-ins and automation code for Microsoft Office applications.
MIT License
695 stars 143 forks source link

CommandBarButton click event not really disposed ? #396

Closed FabienJT closed 11 months ago

FabienJT commented 11 months ago

I Guys! First off all, thank you for great job on the NetOffice project that help us in our daily work! I need you help about a behaviour that I don’t understand the reason why.

I’m working on Excel Pivot Tables (can be many in a work sheet), then on right click on any pivot I have to propose a dynamic menu with list of buttons CommandBarButtons, following the list of all pivot fields. Meaning that I have to recreate the buttons on every right click event on the sheet.

To do so, I first, retrieve the CommandBarPopUp to which the existing buttons are children of, then I fetch the buttons in its controls list and delete() them. (Even dispose() them). Then after I recreate a new list of CommandBarButton (I used a GUID, to be sure that never I have, the same Tag), and associate an method to ClickEvent.

When I execute my code, I see that every new time that I right click, even if I have new instances on CommandBarButton, it seems a new event is added for the same button in the com objects events management.

Exemple of workflow : Having 1 pivot with fields A and B. First Right Click => creation of 2 buttons with (their own click event), one for A and one for B => click on A => 1 event raised.

Adding a new field in the pivot, C. 2nd right Click => deleting existing buttons => creation of 2 buttons with (their own click event), one for A, one for B and onr for C => click on A => 2 events raised with the same sender instance id

3rd rigth click : 3 events raised with the same sender instance id, when click on A

And so on…

Do you catch my point, or need to see the code? Is there anything I can do about this?

Thank you in advance for you advises!

Best Regards,

Fabien JT