// Programmer: Janet Joy // Rich text editor using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Notes1 { public partial class Form1 : Form { String currentFileName = "Untitled"; Boolean changed = false; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // At timer1 tick display date and time timer1.Enabled = true; // Position the rich text box to left, under the toolstrip. richTextBox1.Top = toolStrip1.Top + toolStrip1.Height; richTextBox1.Left = 0; resizeRichText(); } private void timer1_Tick(object sender, EventArgs e) { toolStripStatusDateTime.Text = System.DateTime.Now.ToString(); } private void Form1_Resize(object sender, EventArgs e) { // Select the form, in properties, click events, add resize: resizeRichText(); } private void resizeRichText() { // Rich text box will fill the form between the toolstrip and the status bar. richTextBox1.Width = this.Width; richTextBox1.Height = statusStrip1.Top - richTextBox1.Top; } private void SaveFile() { // Make the save file name the same as open file. saveFileDialog1.FileName = openFileDialog1.FileName; if (currentFileName == "Untitled") { saveFileDialog1.Filter = "RichText|*.rtf"; saveFileDialog1.ShowDialog(); // If user selected cancel filename will be "". if (saveFileDialog1.FileName != "") { currentFileName = saveFileDialog1.FileName; } } if (currentFileName != "Untitled") { // Save the file. richTextBox1.SaveFile(saveFileDialog1.FileName); // There have been no changes since the last save. changed = false; // Display the filename in the status bar. toolStripStatusFilename.Text = saveFileDialog1.FileName; } } private void saveToolStripButton_Click(object sender, EventArgs e) { // Save the file. SaveFile(); } private void saveToolStripMenuItem_Click(object sender, EventArgs e) { // Save the file. SaveFile(); } private Boolean AskToSave() { // If there is no change to list don't ask, don't save, and don't cancel DialogResult result = DialogResult.No; if (changed) { result = MessageBox.Show("Do you want to save changes to " + currentFileName + "?", "Closing", MessageBoxButtons.YesNoCancel); // If they answer yes, write the file if (result == DialogResult.Yes) SaveFile(); } // Returns true if they selected cancel. // Returns false if there has been no change. return (result == DialogResult.Cancel); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { // Ask if they want to save. it returns true if the user chose to cancel. // The FormClosingEventArgs e has a cancel property. Setting it to true cancels the close event. e.Cancel = AskToSave(); } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { // Ask if they want to save. it returns true if the user chose to cancel. Boolean cancel = AskToSave(); if (!cancel) Close(); } private void richTextBox1_TextChanged(object sender, EventArgs e) { // Set change to true if there are any changes to the rich text box. changed = true; } private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) { // Set filter for saveFileDialog saveFileDialog1.Filter = "RichText|*.rtf"; // Set open file as the default: saveFileDialog1.FileName = openFileDialog1.FileName; saveFileDialog1.ShowDialog(); // Filename will be "" if user selects cancel. if (saveFileDialog1.FileName != "") { richTextBox1.SaveFile(saveFileDialog1.FileName); // There have been no changes since the last save. changed = false; // Display the filename in the status bar. toolStripStatusFilename.Text = saveFileDialog1.FileName; } } private void OpenFile() { // Remind to save. Boolean cancel = AskToSave(); // If user did not select cancel, continue. if (!cancel) { // Set the filter. This could be done once in form load. openFileDialog1.Filter = "RichText|*.rtf"; // Set default to current file openFileDialog1.FileName = currentFileName; openFileDialog1.ShowDialog(); // If the user selects cancel filename will be "". if (openFileDialog1.FileName != "") { // Load the new file richTextBox1.LoadFile(openFileDialog1.FileName); // There have been no changes since the last save. changed = false; // track current filename. currentFileName = openFileDialog1.FileName; // Display the filename in the status bar. toolStripStatusFilename.Text = openFileDialog1.FileName; } } } private void openToolStripMenuItem_Click(object sender, EventArgs e) { // OpenFile is called from both the menu item and tool strip button. OpenFile(); } private void openToolStripButton_Click(object sender, EventArgs e) { // OpenFile is called from both the menu item and tool strip button. OpenFile(); } private void newToolStripButton_Click(object sender, EventArgs e) { // Delete everything from richtext box NewDocument(); } private void newToolStripMenuItem_Click(object sender, EventArgs e) { // Delete everything from richtext box NewDocument(); } private void NewDocument() { // Delete everything from richtext box // Remind to save. Boolean cancel = AskToSave(); // If user did not select cancel, continue. if (!cancel) { // Delete the contents of the rich text box. richTextBox1.Text = ""; // Set the filename to untitled. currentFileName = "Untitled"; // No changes so far. changed = false; // Show untitled in status bar. toolStripStatusFilename.Text = currentFileName; } } private void toolStripFont_Click(object sender, EventArgs e) { // You can also add a font item to the menu under tools. SelectFont(); } private void SelectFont() { // Display the font dialog and change font of selected text. // If no text is selected this has no effect. Font selectionFont = richTextBox1.SelectionFont; fontDialog1.Font = richTextBox1.SelectionFont; fontDialog1.ShowDialog(); richTextBox1.SelectionFont = fontDialog1.Font; } private void toolStripColor_Click(object sender, EventArgs e) { SelectColor(); } private void SelectColor() { // Display the color dialog and change color of selected text. // If no text is selected this has no effect. Color selectionColor = richTextBox1.SelectionColor; colorDialog1.ShowDialog(); richTextBox1.SelectionColor = colorDialog1.Color; } private void pasteToolStripButton_Click(object sender, EventArgs e) { // Using the button on toolbar. Paste(); } private void pasteToolStripMenuItem_Click(object sender, EventArgs e) { // This is very important because the shortcut key for the menu item is Ctrl+V Paste(); } private void Paste() { // Paste from Clipboard richTextBox1.SelectedText = Clipboard.GetText(); changed = true; } private void Cut() { // Cut copys to clipboard and then deletes. Clipboard.SetText(richTextBox1.SelectedText); richTextBox1.SelectedText = ""; changed = true; } private void Copy() { // Copys to clipboard, no change to richtext Clipboard.SetText(richTextBox1.SelectedText); richTextBox1.SelectedText = ""; } private void cutToolStripMenuItem_Click(object sender, EventArgs e) { // This is very important because the shortcut key for the menu item is Ctrl+X Cut(); } private void cutToolStripButton_Click(object sender, EventArgs e) { // Cut using tool bar button. Cut(); } private void copyToolStripButton_Click(object sender, EventArgs e) { // Copy from tool bar button. Copy(); } private void copyToolStripMenuItem_Click(object sender, EventArgs e) { // Copy from menu. Copy(); } private void contextCut_Click(object sender, EventArgs e) { // Cut from context menu. Cut(); } private void contextCopy_Click(object sender, EventArgs e) { // Copy from context menu. Copy(); } private void contextPaste_Click(object sender, EventArgs e) { // Paste from context menu. Paste(); } } }