I had the same problem and I couldn't find the solution, so I tried another way to solve this problem:
When you change the tab, delete all previous tab buttons (create them again when you click on your tab)
Put a condition inside all of your OnClick functions that if this button is not visible, do nothing.
Rewrite your ON_EVENT:
#define ON_EVENT(event,control,handler) if(id==(event+CHARTEVENT_CUSTOM) && sparam==control.Name()) {handler(); return(true);}