/// <summary> /// Show safe dialog when Form is about to close /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (DiaLogClass.ShowSafeCloseFormDialog(tabControl) == "Cancel") { e.Cancel = true; } }
/// <summary> /// close selected tab /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void closeToolStripMenuItem_Click(object sender, EventArgs e) { if (tabControl.TabPages.Count > 1) //we don't remove tab page when tabControl has only one tab page { //we can use this one line of code below to easily remove selected tab page but it causes flinking // tabControl.TabPages.Remove(tabControl.SelectedTab); //Show SaveDialog string result = DiaLogClass.ShowSafeCloseTabDialog(tabControl.SelectedTab); if (result == "Cancel") { return; } //Delete the selected tab page status TabControlClass.RemoveTabPageStatus(tabControl.SelectedTab); //that's the reason why use the number lines of code below //somehow if we set the tab page we are about to remove to another one (in this case we are about to remove selected tab page), //it doesn't cause the flinking TabPage tabToRemove = tabControl.SelectedTab; if (tabControl.SelectedIndex != 0) { //set the selectedtab to the first tab page tabControl.SelectedIndex = tabControl.SelectedIndex - 1; } else //if the tab we are about to remove is the first tab, just simply set selectedtab to 1 { tabControl.SelectedTab = tabControl.TabPages[1]; } //remove tab tabControl.TabPages.Remove(tabToRemove); } }
/// <summary> /// close all tap except selected tab /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void closeAllToolStripMenuItem_Click(object sender, EventArgs e) { //remove all the tab except the selected Tab foreach (TabPage tabPage in tabControl.TabPages) { if (tabPage != tabControl.SelectedTab) { string result = DiaLogClass.ShowSafeCloseTabDialog(tabPage); if (result == "Cancel") { return; } //Delete tab status TabControlClass.RemoveTabPageStatus(tabPage); //Remove tab Page tabControl.TabPages.Remove(tabPage); } } }
/// <summary> /// when user click "x" image /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void tabControl_MouseDown(object sender, MouseEventArgs e) { //this for loop check if user click mouse at the position of an "x" image //loop through all tab page to demonstrate which tab tage is being clicked foreach (TabPage tabPage in tabControl.TabPages) { //get the shape of the tab Rectangle tabRect = tabControl.GetTabRect(tabControl.TabPages.IndexOf(tabPage)); //inflate tabRect.Inflate(-2, -2); //get the shape of the "x" image Rectangle imageRect = new Rectangle(tabRect.Right - CloseImage.Width, tabRect.Top, CloseImage.Width, CloseImage.Height); //if mouse click in the shape of "x" image if (imageRect.Contains(e.Location)) { if ((DiaLogClass.ShowSafeCloseTabDialog(tabPage) == "Cancel")) { break; } //remove this tab if (tabControl.TabPages.Count > 1) //if this is the last tab page remaining, don't close it { //we use selectedTab variable here because if we click mouse, tabControl will select the clicked tab //and this if below will always false //if the tab we are deleting is not the selected tab if (tabPage != (TabPage)tabControl.Tag) { //Important Note: we can change the order of two lines of code below and the main result of us will be the same //but if we do that, it will cause flinking tabControl.SelectedTab = (TabPage)tabControl.Tag; //remove tabpage status RemoveTabPageStatus(tabPage); //remove tab page tabControl.TabPages.Remove(tabPage); } else //if the tab we are deleting is the selected tab { //if this isn't the first tab if (tabControl.SelectedIndex != 0) { //set the selectedtab to the closest tab page tabControl.SelectedIndex = tabControl.SelectedIndex - 1; } else //if the tab we are about to remove is the first tab, just simply set selectedtab to 1 { tabControl.SelectedIndex = 1; } //remove tabpage status RemoveTabPageStatus((TabPage)tabControl.Tag); //remove tab page tabControl.TabPages.Remove((TabPage)tabControl.Tag); } } break; } } }
/// <summary> /// save as file /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) { DiaLogClass.ShowSaveAsDialog(tabControl.SelectedTab); }
/// <summary> /// open the choosen file /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void openToolStripMenuItem_Click(object sender, EventArgs e) { DiaLogClass.ShowOpenDialog(tabControl); }