//Closes the window. Promt if the user wants to quit, if yes! The user is prompt with an other //window asking if the user wants to save, if changes have been made in the document. private void Form1_Closing(Object sender, CancelEventArgs e) { DialogResult dialogResult = MessageBox.Show("Whould you like to save first?", "Close Application", MessageBoxButtons.YesNoCancel); switch (dialogResult) { case DialogResult.Yes: if (!isDataSaved && ((ControlAccessibleObject)((Control)sender).AccessibilityObject).Name.Contains("*") == true) { e.Cancel = true; string text = richTextBoxWindow.Text; DialogHandler.SaveFile(text); Application.Exit(); } else { e.Cancel = false; Application.Exit(); } break; default: e.Cancel = true; Application.Exit(); break; } }
//Create a new file. Ask if the user want to save before clearing textbox. private void NewTextField(object sender, EventArgs e) { DialogHandler.SaveQuestion(richTextBoxWindow); richTextBoxWindow.Text = string.Empty; DialogHandler.filePath = string.Empty; ActiveForm.Text = "dok1.txt".ToString(); }
//Save as function. Is almost the same as above function but the user is prompt with a save as window. private void SaveAsBtn_Click(object sender, EventArgs e) { ActiveForm.Text = ActiveForm.Text.TrimEnd('*'); string text = richTextBoxWindow.Text; DialogHandler.SaveAsDialogWindow(text); isDataSaved = true; }
//Drag and drop function. public void DragAndDrop(DragEventArgs e, RichTextBox richTextBox, Keys keys) { //Reads the content from the file that has been droped in the editor. if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string s = ""; try { string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, false); StreamReader file = new StreamReader(files[0]); s = file.ReadToEnd(); file.Close(); } catch (IOException ex) { Console.WriteLine(ex); throw new Exception(); } //Diffrent action depending on the key the user press under the time the user //drags and drop content to editor. switch (keys) { case Keys.Control: string text = richTextBox.Text; richTextBox.Clear(); text += richTextBox.Text.ToString(); richTextBox.Text = text + s; break; case Keys.Shift: richTextBox.SelectionStart += richTextBox.SelectionLength; richTextBox.SelectionLength = 0; richTextBox.SelectedText = s; break; default: //Warning window, ask if the user wants to save before the new content is displayed in the editor. if (MessageBox.Show("Whould you like to save first?", "Close Application", MessageBoxButtons.YesNo) == DialogResult.Yes) { DialogHandler.SaveFile(richTextBox.Text); richTextBox.Clear(); richTextBox.Text = s; } else { richTextBox.Clear(); richTextBox.Text = s; } break; } } }
//Update information about the text. Also add an astrix if the text is changed. private void RichTextBoxWindow_TextChanged(object sender, EventArgs e) { windowHandler.TextChange(); isDataSaved = false; string text = richTextBoxWindow.Text.ToString(); string a = string.Concat(text.Where(c => !char.IsWhiteSpace(c))); int counter = a.Length; InformationLbl.Text = "Antal bokstäver: " + richTextBoxWindow.Text.Length + " Antal bokstäver utan mellanslag: " + counter.ToString() + " Antal rader: " + richTextBoxWindow.Lines.Length + " Antal ord: " + DialogHandler.WordCount(richTextBoxWindow.Text); }
//Open a new file. private void OpenFileBtn_Click(object sender, EventArgs e) { richTextBoxWindow.Text = DialogHandler.OpenFileDialogWindow(richTextBoxWindow); }
private void CloseBtn_Click(object sender, EventArgs e) { DialogHandler.SaveQuestion(richTextBoxWindow); Application.Exit(); }