public void DestroyComponent(IComponent component) { //deselect it if selected ISelectionService sel = this.GetService(typeof(ISelectionService)) as ISelectionService; bool found = false; if (sel != null) { foreach (IComponent c in sel.GetSelectedComponents()) { if (c == component) { found = true; break; } } } //can't modify selection in loop if (found) { sel.SetSelectedComponents(null); } if (component != RootComponent) { //remove from component and document ((Control)RootComponent).Controls.Remove((Control)component); RootDocument.RemoveControl((Control)component); } //remove from container if still sited if (component.Site != null) { container.Remove(component); } component.Dispose(); }