public override void Remove(Control value) { bool change_index = false; TabPage page = value as TabPage; if (page != null && owner.Controls.Contains(page)) { int index = owner.IndexForTabPage(page); if (index < owner.SelectedIndex || owner.SelectedIndex == Count - 1) { change_index = true; } } base.Remove(value); owner.m_helper.Remove(page); // We don't want to raise SelectedIndexChanged until after we // have removed from the collection, so TabCount will be // correct for the user. if (change_index && Count > 0) { // Clear the selected index internally, to avoid trying to access the previous // selected tab when setting the new one - this is what .net seems to do int prev_selected_index = owner.SelectedIndex; owner.selected_index = -1; owner.SelectedIndex = --prev_selected_index; owner.Invalidate(); } else if (change_index) { owner.selected_index = -1; owner.OnSelectedIndexChanged(EventArgs.Empty); owner.Invalidate(); } else { owner.Redraw(); } }