Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- namespace Rich_Text_Editor
- {
- public partial class frmRichTextEditor : Form
- {
- public frmRichTextEditor()
- {
- InitializeComponent();
- }
- bool textSaved = true;// creates a boolean variable and
- string fileName = "";// creates filename variable as a string
- string fileTitle;// creates the fileTitle variable as a string
- DialogResult result;// declares result as a DialogResult
- private void msNew_Click(object sender, EventArgs e)//checks if the user wants to save before a new document is created
- {
- dlgSave.Filter = "Rich Text Files (*.rtf)|*.rtf|Plaintext Files (*txt)|*.txt";// sets the save file dialog filter
- if (textSaved == false)// if the text has not been saved
- {
- result = MessageBox.Show("Do you wish to save your changes?", "Text Changed Since Last Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);// sets the result variable into as the answer chosen by the user
- if (result == DialogResult.Yes)// if the user clicked yes
- {
- msSaveFile_Click(sender, e);// simulate a click on the save file button, calling the save function
- }
- else if (result == DialogResult.No)// if the user clicked no
- {
- textSaved = false;// set the textSaved value to no, indicating the text needs to be saved
- rtbContent.Clear();// clear the rich text box
- Text = "RTF Editor: untitled";// set the title of the program to show the document is a new document
- }
- }
- }
- private void msOpenFile_Click(object sender, EventArgs e)// asks the user if they want to save their document and opens a new file
- {
- if (textSaved == false)// if the text has not been saved
- {
- msSaveFile_Click(sender, e);// simulate a click on the save file button, calling the save function
- }
- dlgOpen.Filter = "Rich Text Files (*.rtf)|*.rtf|Plaintext Files (*txt)|*.txt";// set the open dialog filter
- if (dlgOpen.ShowDialog() != DialogResult.Cancel)// if the dialog cancel button was not clicked
- {
- fileName = dlgOpen.FileName;//sets the fileName variable to the file name selected in the open file dialog
- if (fileName.EndsWith("txt"))// if the fileName variable ends with "txt"
- {
- rtbContent.LoadFile(fileName, RichTextBoxStreamType.PlainText);// load content into the rich text box in plain text format
- }
- else if (fileName.EndsWith("rtf"))// else is the fileName variable ends in "rtf"
- {
- rtbContent.LoadFile(fileName, RichTextBoxStreamType.RichText);// load content into the rich text box in rich text format
- }
- textSaved = true;// set the textSaved variable to true, indicating that the document was saved
- fileTitle = Path.GetFileName(fileName);// sets the file title variable to the file name found in the folder
- Text = "RTF Editor: " + fileTitle;// sets the title of the program to "RTF Editor: " + the fileTitle variable
- }
- }
- private void msSaveFile_Click(object sender, EventArgs e)// saves the file
- {
- dlgSave.Filter = "Rich Text Files (*.rtf)|*.rtf|Plaintext Files (*txt)|*.txt";// sets the filter for the save file dialog
- if (textSaved == false)// if the document has not been saved
- {
- if (fileName != "")// if the file name is not equal to nothing
- {
- if (fileName.EndsWith("txt"))// if the file name ends with "txt"
- {
- rtbContent.SaveFile(fileName, RichTextBoxStreamType.PlainText);// save the content in the rtb as plain text format
- }
- else if (fileName.EndsWith("rtf"))// else if the file name ends with "rtf"
- {
- rtbContent.SaveFile(fileName, RichTextBoxStreamType.RichText);// save the content in the rtb as rich text format
- }
- }
- else// else (if the filename does equal nothing)
- {
- if (dlgSave.ShowDialog() != DialogResult.Cancel)// if the save file dialog was not cancelled
- {
- fileName = dlgSave.FileName;// set the file name variable to the filename specified in the save file dialog
- if (fileName.EndsWith("txt"))// if the file name ends with "txt"
- {
- rtbContent.SaveFile(fileName, RichTextBoxStreamType.PlainText);// save the content in the rtb as plain text format
- }
- else if (fileName.EndsWith("rtf"))// if the file name ends with "txt"
- {
- rtbContent.SaveFile(fileName, RichTextBoxStreamType.RichText);// save the content in the rtb as rich text format
- }
- }
- }
- textSaved = true;// set the text saved variable to true, indicating the text file has been saved
- fileTitle = Path.GetFileName(fileName);// sets the file title variable to the file name found in the folder
- Text = "RTF Editor: " + fileTitle;// sets the title of the program to "RTF Editor: " + the fileTitle variable
- }
- }
- private void msSaveAs_Click(object sender, EventArgs e)// simulates a save as for the text in the document
- {
- dlgSave.Filter = "Rich Text Files (*.rtf)|*.rtf|Plaintext Files (*txt)|*.txt";// sets the filter for the save file dialog
- if (dlgSave.ShowDialog() != DialogResult.Cancel)// the save file dialog was not cancelled
- {
- fileName = dlgSave.FileName;// set the file name variable to the name of the file determined by the save file dialog
- if (fileName.EndsWith("txt"))// if the file ends with "txt"
- {
- rtbContent.SaveFile(fileName, RichTextBoxStreamType.PlainText);// saves the content of the rich text box in plain text
- }
- else if (fileName.EndsWith("rtf"))// if the file ends with "rtf"
- {
- rtbContent.SaveFile(fileName, RichTextBoxStreamType.RichText);// saves the content of the rich text box inrich text
- }
- textSaved = true;// set the text saved variable to true, indicating the text file has been saved
- fileTitle = Path.GetFileName(fileName);// sets the file title variable to the file name found in the folder
- Text = "RTF Editor: " + fileTitle;// sets the title of the program to "RTF Editor: " + the fileTitle variable
- }
- }
- private void msExit_Click(object sender, EventArgs e)//closes the program
- {
- this.Close();// closes the program
- }
- private void msCut_Click(object sender, EventArgs e)//cuts selected text
- {
- if (rtbContent.SelectedText != "")// if the selected text is not equal to nothing
- {
- rtbContent.Cut();//cut the selection
- }
- }
- private void msCopy_Click(object sender, EventArgs e)//copies selected text
- {
- if (rtbContent.SelectedText != "")// if the selected text is not equal to nothing
- {
- rtbContent.Copy();// copies the selected text
- }
- }
- private void msPaste_Click(object sender, EventArgs e)// pastes text into the rich text box
- {
- if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)// if there is text data present in the clipboard
- {
- rtbContent.Paste();//paste the text into the tich text box
- }
- }
- private void msFont_Click(object sender, EventArgs e)// opens the font dialog box
- {
- if (dlgFont.ShowDialog() != DialogResult.Cancel)//if the font dialog box was not cancelled
- {
- rtbContent.Font = dlgFont.Font;// change the font to the font selected in the font dialog
- }
- }
- private void msColour_Click(object sender, EventArgs e)// opens the colour dialog box
- {
- if (dlgColour.ShowDialog() != DialogResult.Cancel)//if the colour dialog box was not cancelled
- {
- rtbContent.ForeColor = dlgColour.Color;// set the colour of the text to the colour selected in the colour dialog
- }
- }
- private void msFindText_Click(object sender, EventArgs e)// opens the find text form
- {
- frmFindReplace formFindReplace = new frmFindReplace();// declares a new instance of the find and replace form
- formFindReplace.Owner = this;// sets the owner of the find and replace form as the main form (form1)
- formFindReplace.Show();//shows the find and replace form
- }
- private void msAboutMe_Click(object sender, EventArgs e)// shows a message box detailing my student number
- {
- MessageBox.Show("10053670", "About Me", MessageBoxButtons.OK, MessageBoxIcon.Information);// opens a message box, sets the content to "10053670", the title to "about me", only allows an ok button and sets the message box icon to a question mark icon.
- }
- private void tsbNew_Click(object sender, EventArgs e)//creates a new document
- {
- dlgSave.Filter = "Rich Text Files (*.rtf)|*.rtf|Plaintext Files (*txt)|*.txt";// sets the save dialog filter
- if (textSaved == false)// if the text has not been saved
- {
- result = MessageBox.Show("Do you wish to save your changes?", "Text Changed Since Last Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);// sets the result variable to the result of a message box dialog
- if (result == DialogResult.Yes)// if the result was yes
- {
- msSaveFile_Click(sender, e);// simulate a click on the save file button, calling the save function
- }
- else if (result == DialogResult.No)// else if the result was no
- {
- textSaved = false;// set the text saved variable to false, indicating the text has not been saved
- rtbContent.Clear();// clear the rich text box content
- Text = "RTF Editor: untitled";// set the title of the form to "untitled"
- }
- }
- }
- private void tsbOpen_Click(object sender, EventArgs e)// opens a document
- {
- if (textSaved == false)// if the text has not been saved
- {
- msSaveFile_Click(sender, e);// simulate a click on the save file button, calling the save function
- }
- dlgOpen.Filter = "Rich Text Files (*.rtf)|*.rtf|Plaintext Files (*txt)|*.txt";// set the dialog open filter
- if (dlgOpen.ShowDialog() != DialogResult.Cancel)// if the open dialog was not cancelled
- {
- fileName = dlgOpen.FileName;// set the filename variable to the name of the file specified in the dialog
- if (fileName.EndsWith("txt"))// if the file name ends with "txt"
- {
- rtbContent.LoadFile(fileName, RichTextBoxStreamType.PlainText);// load the file into the rich text box as a plain text file type
- }
- else if (fileName.EndsWith("rtf"))// else if the file name ends with "rtf"
- {
- rtbContent.LoadFile(fileName, RichTextBoxStreamType.RichText);// load the file into the rich text box as a rich text file type
- }
- textSaved = true;// set the textSaved variable to true, indicating the text was saved
- fileTitle = Path.GetFileName(fileName);//sets the fileTitle variable to the path of the selected file
- Text = "RTF Editor: " + fileTitle;// sets the title of the program to "RTF Editor: " + the fileTitle variable
- }
- }
- private void tsbSave_Click(object sender, EventArgs e)// saves a file
- {
- dlgSave.Filter = "Rich Text Files (*.rtf)|*.rtf|Plaintext Files (*txt)|*.txt";// sets the save file dialog filter
- if (textSaved == false)// if the text has not been saved
- {
- if (fileName != "")// if the fileName variable is not nothing
- {
- if (fileName.EndsWith("txt"))// if the filename variable ends with "txt"
- {
- rtbContent.SaveFile(fileName, RichTextBoxStreamType.PlainText);// save the file in a plain text format
- }
- else if (fileName.EndsWith("rtf"))//else if the file ends in "rtf"
- {
- rtbContent.SaveFile(fileName, RichTextBoxStreamType.RichText);// save the file in a rich text format
- }
- }
- else// else (if the file name variable does equal something)
- {
- if (dlgSave.ShowDialog() != DialogResult.Cancel)// if the save dialog was not cancelled
- {
- fileName = dlgSave.FileName;// sets the fileName variable as the file name specified by the dialog
- if (fileName.EndsWith("txt"))// if the fileName ends with "txt"
- {
- rtbContent.SaveFile(fileName, RichTextBoxStreamType.PlainText);// save the file in plain text format
- }
- else if (fileName.EndsWith("rtf"))// else if the file name ends with "rtf"
- {
- rtbContent.SaveFile(fileName, RichTextBoxStreamType.RichText);// save the file in plain text format
- }
- }
- }
- textSaved = true;// set the textSaved variable to true, indicating the text was saved
- fileTitle = Path.GetFileName(fileName);//sets the fileTitle variable to the path of the selected file
- Text = "RTF Editor: " + fileTitle;// sets the title of the program to "RTF Editor: " + the fileTitle variable
- }
- }
- private void tsbCut_Click(object sender, EventArgs e)//cuts selected text
- {
- if (rtbContent.SelectedText != "")// the selected text is not equal to nothing
- {
- rtbContent.Cut();// cut the selected text
- }
- }
- private void tsbCopy_Click(object sender, EventArgs e)// copies selected text
- {
- if (rtbContent.SelectedText != "")// if the selected text is not equal to nothing
- {
- rtbContent.Copy();// copy the selected text
- }
- }
- private void tsbPaste_Click(object sender, EventArgs e)// pastes text into the document
- {
- if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)// if there is text data present in the clipboard
- {
- rtbContent.Paste();// pastes the clipboard data into the rich text box
- }
- }
- private void tsbAboutMe_Click(object sender, EventArgs e)// shows a message box detailing my student number
- {
- MessageBox.Show("10053670", "About Me", MessageBoxButtons.OK, MessageBoxIcon.Information);// opens a message box, sets the content to "10053670", the title to "about me", only allows an ok button and sets the message box icon to a question mark icon.
- }
- private void rtbContent_TextChanged(object sender, EventArgs e)// if the text value of the rich text box has been changed
- {
- textSaved = false;// set the text saved variable to false
- }
- private void frmRichTextEditor_FormClosing(object sender, FormClosingEventArgs e)// this event handler fires with the form starts to close
- {
- if (textSaved == false)// if the text has not been saved
- {
- dlgSave.Filter = "Rich Text Files (*.rtf)|*.rtf|Plaintext Files (*txt)|*.txt";// sets the filter for the save dialog
- result = MessageBox.Show("Do you wish to save your changes?", "Text Changed Since Last Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);// asks the user if they want to save the document
- if (result == DialogResult.Yes)// if the result of the message box dialog was yes
- {
- msSaveFile_Click(sender, e);// simulate a save file button click
- }
- else if (result == DialogResult.Cancel)// if the result of the dialog is cancel
- {
- e.Cancel = true;// cancel the shutdown
- }
- }
- }
- }
- }// end of program
Advertisement
Add Comment
Please, Sign In to add comment