/// <summary> /// Creates new custom file window. /// </summary> /// <param name="fileName"></param> /// <param name="filePath"></param> /// <param name="isNextNew">If new window's file should be blank new file, then it should be set to true, otherwise false</param> /// <returns></returns> private FormFileWindow CreateParameterizedTextWindow(string fileName, string filePath, bool isNextNew) { try { Button newFormButton = new Button(); FormFileWindow newForm = new FormFileWindow { MdiParent = this, WindowState = FormWindowState.Maximized, IsChangedFlag = false, connectedButton = newFormButton }; // Sets new filename and connects button to form. newForm.ChangeFileName(fileName, isNextNew ? null : filePath); newFormButton.Click += (sender, EventArgs) => { ButtonNext_Click(sender, EventArgs, newForm); }; newFormButton.MouseDown += (sender, EventArgs) => { Button_MouseWheelClick(sender, EventArgs, newForm); }; flowLayoutPanelTabButtons.Controls.Add(newFormButton); CountOpenedWindows++; RefreshColorTheme(); return(newForm); } catch (Exception ex) { MessageBox.Show($"Unexpected error happened while trying to create new editor window. {ex.Message}"); } return(null); }
/// <summary> /// Allows to close windows by clicking middle mouse button. /// </summary> /// <param name="sender"></param> /// <param name="eventArgs"></param> /// <param name="newForm"></param> private void Button_MouseWheelClick(object sender, MouseEventArgs eventArgs, FormFileWindow newForm) { if (eventArgs.Button == MouseButtons.Middle) { newForm.CloseFile(); } }
/// <summary> /// Opens new file. /// </summary> private void OpenFile(string filePath = null) { FormFileWindow newForm = null; string fileName; try { if (filePath == null) { if (!CheckIfPossibleToCreateOrOpenNewFile()) { return; } (filePath, fileName) = GetFilePathViaDialog(); if (filePath == null) { return; } } else { fileName = Path.GetFileName(filePath); } newForm = CreateParameterizedTextWindow(Path.GetFileName(filePath), filePath, false); // Checks file extensons and chooses actions. if (Path.GetExtension(filePath) == ".rtf") { newForm.LoadRichText(); } else if (Path.GetExtension(filePath) == ".txt") { newForm.LoadText(File.ReadAllText(filePath)); } else if (Path.GetExtension(filePath) == ".cs") { newForm.LoadText(File.ReadAllText(filePath)); } return; } catch (Exception ex) { MessageBox.Show($"Unexpected error happened while trying to open new file. {ex.Message}"); } if (newForm != null) { newForm.ForcedCloseFile(); } }