private static void TabControl_OnMouseDown(object sender, MouseEventArgs e) { //Check all the tab to determind which tab that has mouse click fall into. foreach (TabPage tabPage in TabControl.TabPages) { //get the area of tab Rectangle tabRect = TabControl.GetTabRect(TabControl.TabPages.IndexOf(tabPage)); //get the area of the "x" image Rectangle imageXRect = new Rectangle(tabRect.Right - tabRect.Height, tabRect.Top, tabRect.Height, tabRect.Height); //Remove if (imageXRect.Contains(e.Location)) { //Safe close if ((CurrentTextArea.requiresSaving == 1) && (Dialog.ShowSafeCloseTabDialog(tabPage) == "Cancel")) { break; } //When we close the unselected tab, it will be automatically selected //So we need reset to the previous selected tab. //Check whether the previous selected tab is existing if (PreviousSelectedTabpage != null && TabControl.TabPages.Contains(PreviousSelectedTabpage)) { TabControl.SelectedTab = PreviousSelectedTabpage; } else { //if not, set the next selected tab to the nearest tab if (TabControl.SelectedIndex == 0) { TabControl.SelectedIndex = 1; } else { TabControl.SelectedIndex -= 1; } } //remove tabpage info in listOfTabPageInfo RemoveTabPageInfo(tabPage); //remove tab page TabControl.TabPages.Remove(tabPage); break; } } if (TabControl.TabPages.Count > 0) { CurrentTextArea.Focus(); } }
private static void TabControl_DragTab(object sender, DragEventArgs e) { //If the thing dragged over isn't a Tab if (!e.Data.GetDataPresent(typeof(TabPage))) { return; } //Get the tab being dragged TabPage draggedTab = (TabPage)e.Data.GetData(typeof(TabPage)); //Get the index of the tab being dragged int draggedTabIndex = TabControl.TabPages.IndexOf(draggedTab); //Get the index of the tab having the mouse on it int hoveredTabIndex = GetHoverTabIndex(); //If the mouse is not on any Tab if (hoveredTabIndex < 0) { e.Effect = DragDropEffects.None; return; } //Change the type of cursor showing while dragging e.Effect = DragDropEffects.Move; TabPage hoveredTab = TabControl.TabPages[hoveredTabIndex]; if (draggedTab == hoveredTab) { return; } SwapTabPages(draggedTab, hoveredTab); //Set the selected tab to the dragged tab TabControl.SelectedTab = draggedTab; CurrentTextArea.Focus(); }