private void OnComponentRemoving(object sender, ComponentEventArgs e)
{
// If our control is being removed
if (e.Component == _ribbon)
{
// Need access to host in order to delete a component
IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
// We need to remove all the button spec instances
for (int i = _ribbon.ButtonSpecs.Count - 1; i >= 0; i--)
{
ButtonSpec spec = _ribbon.ButtonSpecs[i];
_ribbon.ButtonSpecs.Remove(spec);
host.DestroyComponent(spec);
}
// We need to remove all the QAT button specifications
for (int i = _ribbon.QATButtons.Count - 1; i >= 0; i--)
{
Component button = _ribbon.QATButtons[i];
_ribbon.QATButtons.Remove(button);
host.DestroyComponent(button);
}
// We need to remove all the ribbon context instances
for (int i = _ribbon.RibbonContexts.Count - 1; i >= 0; i--)
{
KryptonRibbonContext context = _ribbon.RibbonContexts[i];
_ribbon.RibbonContexts.Remove(context);
host.DestroyComponent(context);
}
// We need to remove all the ribbon tab instances
for (int i = _ribbon.RibbonTabs.Count - 1; i >= 0; i--)
{
KryptonRibbonTab tab = _ribbon.RibbonTabs[i];
_ribbon.RibbonTabs.Remove(tab);
host.DestroyComponent(tab);
}
}
}