/// <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> /// Create a new tab by "New" Button /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void newToolStripMenuItem_Click(object sender, EventArgs e) { //Index of the tab page having the name of "New Tab" //this is just a variable to check the name of tab page int tabIndex = 1; //a variable to hold new tab name string newTabName = "New Tab " + tabIndex.ToString(); //loop until we find the "New Tab" tab page has been lacked for (int i = 0; i < tabControl.TabPages.Count; i++) { //get the tabName without "*" (this is considered the real name) string tabName = tabControl.TabPages[i].Text; tabName = tabName.Replace("*", ""); if (tabName == newTabName) { tabIndex++; newTabName = "New Tab " + tabIndex.ToString(); //set this to start over the for loop i = -1; } } //create new tab page TabControlClass.CreateNewTabPage(newTabName); }
/// <summary> /// Open Dialog /// </summary> /// <param name="tabControl"></param> public static void ShowOpenDialog(TabControl tabControl) { //create a new file dialog OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "txt Files (*txt)|*txt|All Files (*.*)|*.*"; //Pop up the file dialog and check if user press open button if (openFileDialog.ShowDialog() == DialogResult.OK) { //check to see if there is already this tab page TabPage targetTabPage = null; foreach (TabPage tabPage in tabControl.TabPages) { if (tabPage.Name == openFileDialog.FileName) { targetTabPage = tabPage; break; } } //if this tab page has already opened, just focus this tab and return if (targetTabPage != null) { tabControl.SelectedTab = targetTabPage; return; } //Create a new tab page TabPage newTabPage = TabControlClass.CreateNewTabPage(openFileDialog.SafeFileName); //a variable to hold text box contained in tab page TextArea newTextArea = TabControlClass.CurrentTextArea; //Get the path of the File string filePath = openFileDialog.FileName; //Get the text of the file string fileText = File.ReadAllText(filePath); //Set the text of current text box by file Text //we don't want Undo to record our text here newTextArea.StopRecordingUndo(); newTextArea.Text = fileText; newTextArea.ContinueRecordingUndo(); //when we set the text by the code above, we changed the text in the text area //and accidentally lead to the MarkTabPage event //that's the reason why we have to do this to unmark the tabPage initially newTabPage.Text = newTabPage.Text.Replace("*", ""); //this is a trick to save the path(FileName) of the saved tab page //and the next time if this tab page has already had a name, we shouldn't open the savefiledialog again //and just implicitly save tabControl.SelectedTab.Name = openFileDialog.FileName; } //dispose for sure openFileDialog.Dispose(); }
private void MainForm_Load(object sender, EventArgs e) { //get controls StatusBar = statusStrip; TaskBar = toolStrip; NewButton = newToolStripButton; SaveButton = saveToolStripButton; OpenButton = openToolStripButton; FindButton = findToolStripButton; FontButton = fontToolStripButton; BoldButton = boldToolStripButton; ItalicButton = italicToolStripButton; UnderLineButton = underlineToolStripButton; CopyButton = copyToolStripButton; CutButton = cutToolStripButton; PasteButton = pasteToolStripButton; FindAndReplaceButton = findAndReplaceToolStripButton; MapButton = documentMapToolStripButton; ToUpperButton = upperToolStripButton; VersionsButton = versionsToolStripButton; //set up Task bar StylesClass.GetAllStylesIntoProperties(); toolStrip.Visible = !StylesClass.HideTaskBar; statusStrip.Visible = StylesClass.ShowStatusBar; newToolStripButton.Visible = StylesClass.ShowNewIcon; openToolStripButton.Visible = StylesClass.ShowOpenIcon; saveToolStripButton.Visible = StylesClass.ShowSaveIcon; documentMapToolStripButton.Visible = StylesClass.ShowDocumentMapIcon; findToolStripButton.Visible = StylesClass.ShowFindIcon; findAndReplaceToolStripButton.Visible = StylesClass.ShowFindAndReplaceIcon; upperToolStripButton.Visible = StylesClass.ShowToUpperIcon; versionsToolStripButton.Visible = StylesClass.ShowVersionsIcon; copyToolStripButton.Visible = StylesClass.ShowCopyIcon; cutToolStripButton.Visible = StylesClass.ShowCutIcon; pasteToolStripButton.Visible = StylesClass.ShowPasteIcon; fontToolStripButton.Visible = StylesClass.ShowFontIcon; boldToolStripButton.Visible = StylesClass.ShowBoldIcon; italicToolStripButton.Visible = StylesClass.ShowItalicIcon; underlineToolStripButton.Visible = StylesClass.ShowUnderLineIcon; //Setup Tab Control TabControlClass.SetupTabControl(tabControl); //just create a new tab page TabControlClass.CreateNewTabPage("New Tab 1"); textLengthStatusLabel.Text = "TextLength: 0"; lineNumberStatusLabel.Text = "LineNumber: 0"; SetupLanguageToolStripMenuItem(StylesClass.DefaultLanguage); }
/// <summary> /// version /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void versionToolStripMenuItem_Click(object sender, EventArgs e) { TabPage tabPage = TabControlClass.CreateNewTabPage("ReadMe.txt"); TextArea textArea = TabControlClass.CurrentTextArea; if (File.Exists("../../ReadMe.txt")) { textArea.Text = File.ReadAllText("../../ReadMe.txt"); } tabPage.Text = tabPage.Text.Replace("*", ""); textArea.ReadOnly = true; }
/// <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> /// Allow drag and drop file into text area /// </summary> /// <param name="textBox"></param> private static void InitDragDropFile(MyRichTextBox textBox, TabControl tabControl) { TextArea textArea = textBox.TextArea; //allow drop file textArea.AllowDrop = true; //On Drag Enter event textArea.DragEnter += delegate(object sender, DragEventArgs e) { //if the dropped file can be converted to specified format //simply means if the dropped file can be read if (e.Data.GetDataPresent(DataFormats.FileDrop)) { //make an effect to look like we are dragging something //this is not really important but we should do it e.Effect = DragDropEffects.Copy; } else { e.Effect = DragDropEffects.None; } }; //On Drop File textArea.DragDrop += delegate(object sender, DragEventArgs e) { //if the dropped file can be read if (e.Data.GetDataPresent(DataFormats.FileDrop)) { //get data in this file //note that because the various type of things can be dropped in the text area //that's why it makes sense that the GetData function return an object //we need to convert this into a String Array (or simply an Array) to get the data stored in the object //it's important to know somehow the data store in the object just the path of the file we are about to read String[] strArray = (String[])e.Data.GetData(DataFormats.FileDrop); //get the path from String Array //note that although the String Array has just only one element (the path of the file), //we can't convert directly the object above into just one string string path = strArray[0]; //just check to make sure the path existing if (File.Exists(path)) { //the number lines of code below is just a copy of open function in MainForm //check to see if there is already this tab page being opened TabPage targetTabPage = null; foreach (TabPage tabPage in tabControl.TabPages) { if (tabPage.Name == path) { targetTabPage = tabPage; break; } } //if this tab page has already opened, just focus this tab and return if (targetTabPage != null) { tabControl.SelectedTab = targetTabPage; return; } //it's better to create a new tab page to write the stuffs than using the selected tab page, //so I'll create new one //this all the code below just a copy of OpenDialog function //Create a new tab page TabPage newTabPage = TabControlClass.CreateNewTabPage(Path.GetFileName(path)); //a variable to hold text box contained in tab page TextArea newTextArea = (newTabPage.Controls[0] as MyRichTextBox).TextArea; //Get the text of the file string fileText = File.ReadAllText(path); //Set the text of current text box by file Text newTextArea.StopRecordingUndo(); newTextArea.Text = fileText; newTextArea.ContinueRecordingUndo(); //when we set the text by the code above, we changed the text in the text area //and accidentally lead to the MarkTabPage event //that's the reason why we have to do this to unmark the tabPage initially newTabPage.Text = newTabPage.Text.Replace("*", ""); //this is a trick to save the path(FileName) of the saved tab page //and the next time if this tab page has already had a name, we shouldn't open the savefiledialog again //and just implicitly save tabControl.SelectedTab.Name = path; } } }; }