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 RichTextEditor : Form
- {
- public RichTextEditor()
- {
- InitializeComponent();
- }
- string fileName = ""; //string fileName variable, to hold the name of a file used with the open or save dialogs
- bool textChanged = false; //boolean variable to monitor if the text has been changed
- bool fileSaved = true;// boolean variable to monitor if the text has been saved
- private void newToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (fileSaved == false)
- {
- }
- }
- private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
- {
- }
- private void saveFileToolStripMenuItem_Click(object sender, EventArgs e)//working
- {
- if (fileSaved == false) // begins the save process by checking the state of the fileSaved boolean variable
- {
- saveFileDialog.Filter = "Rich Text Files (*.rtf)|*.rtf|Plaintext Files (*txt)|*.txt";// sets the filter for the saveFileDialog
- if (fileName != "")// if the file name is not equal to nothing, it starts to save the file (as opposed to save as)
- {
- if (fileName.EndsWith("txt"))// if the file name ends in "txt", the program saves the file as plaintext...
- {
- rtbContent.SaveFile(fileName, RichTextBoxStreamType.PlainText);// ...in the right file with the name provided in the fileName variable
- }
- else if (fileName.EndsWith("rtf"))// if the file name ends in "txt", the program saves the file as richtext...
- {
- rtbContent.SaveFile(fileName, RichTextBoxStreamType.RichText);// ...in the right file with the name provided in the fileName variable
- }
- }
- else// if the file Name is equal to nothing program starts the save as process
- {
- if (saveFileDialog.ShowDialog() != DialogResult.Cancel)//if the save file dialog is not cancelled, the save as program can proceed
- {
- fileName = saveFileDialog.FileName;//assigns the fileName provided by the save file dialog to the fileName variable
- string saveFileName = saveFileDialog.FileName;// sets the savefileName to the file name provided by the save file dialog
- if (saveFileName.EndsWith("txt"))// if the file name ends in "txt", the program saves the file as plaintext...
- {
- rtbContent.SaveFile(saveFileName, RichTextBoxStreamType.PlainText);// ...in the right file with the name provided in the fileName variable
- }
- else if (saveFileName.EndsWith("rtf")) // if the file name ends in "txt", the program saves the file as plaintext...
- {
- rtbContent.SaveFile(saveFileName, RichTextBoxStreamType.RichText);// ...in the right file with the name provided in the fileName variable
- }
- }
- fileName = Path.GetFileName(saveFileDialog.FileName);// gets the name of the file selected in the dialog without the directory
- Text = "RTF Editor" + ": " + fileName;// sets the title of the form to the default name plus the name of the file open
- }
- }
- }
- private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)//working
- {
- saveFileDialog.Filter = "Rich Text Files (*.rtf)|*.rtf|Plaintext Files (*txt)|*.txt";// sets the filter for the saveFileDialog
- if (saveFileDialog.ShowDialog() != DialogResult.Cancel)//if the save file dialog is not cancelled, the save as program can proceed
- {
- fileName = saveFileDialog.FileName;//assigns the fileName provided by the save file dialog to the fileName variable
- string saveFileName = saveFileDialog.FileName;// sets the savefileName to the file name provided by the save file dialog
- if (saveFileName.EndsWith("txt"))// if the file name ends in "txt", the program saves the file as plaintext...
- {
- rtbContent.SaveFile(saveFileName, RichTextBoxStreamType.PlainText);// ...in the right file with the name provided in the fileName variable
- }
- else if (saveFileName.EndsWith("rtf")) // if the file name ends in "txt", the program saves the file as plaintext...
- {
- rtbContent.SaveFile(saveFileName, RichTextBoxStreamType.RichText);// ...in the right file with the name provided in the fileName variable
- }
- }
- fileName = Path.GetFileName(saveFileDialog.FileName);// gets the name of the file selected in the dialog without the directory
- Text = "RTF Editor" + ": " + fileName;// sets the title of the form to the default name plus the name of the file open
- }
- private void exitToolStripMenuItem_Click(object sender, EventArgs e)
- {
- }
- private void cutToolStripMenuItem_Click(object sender, EventArgs e)//working
- {
- if (rtbContent.SelectedText != "")//if the selected text in the rich text box is not nothing...
- {
- rtbContent.Cut();// ...Then cut the selection and pass it to the clipboard
- }
- }
- private void copyToolStripMenuItem_Click(object sender, EventArgs e)//working
- {
- if (rtbContent.SelectedText != "")//if the selected text in the rich text box is not nothing...
- {
- rtbContent.Copy();// ...Then copy the selection and pass it to the clipboard
- }
- }
- private void pasteToolStripMenuItem_Click(object sender, EventArgs e)//working
- {
- if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)//if there is text data present in the clip board...
- {
- rtbContent.Paste();// ...paste it into the rich text box
- }
- }
- private void fontToolStripMenuItem_Click(object sender, EventArgs e)//working
- {
- if(fontDialog.ShowDialog() != DialogResult.Cancel)// if the dialog is not cancelled...
- {
- rtbContent.Font = fontDialog.Font;// ...replace the font value of the RTB with the font value recovered from the font dialog
- }
- }
- private void colourToolStripMenuItem_Click(object sender, EventArgs e)// working
- {
- if (colorDialog.ShowDialog() != DialogResult.Cancel)// if the dialog is not cancelled...
- {
- rtbContent.ForeColor = colorDialog.Color;// ...replace the Forecolor value of the RTB with the color value recovered from the color dialog
- }
- }
- private void findTextToolStripMenuItem_Click(object sender, EventArgs e)
- {
- }
- private void aboutMeToolStripMenuItem_Click(object sender, EventArgs e)//working
- {
- MessageBox.Show("10053670", "About Me", MessageBoxButtons.OK, MessageBoxIcon.Information);//call MessageBox function, set text to "10053670", the title to "About Me" and give the box an ok button and an information symbol
- }
- private void tsbNew_Click(object sender, EventArgs e)
- {
- }
- private void tsbOpen_Click(object sender, EventArgs e)
- {
- }
- private void tsbSave_Click(object sender, EventArgs e) //working
- {
- if (fileSaved == false) // begins the save process by checking the state of the fileSaved boolean variable
- {
- saveFileDialog.Filter = "Rich Text Files (*.rtf)|*.rtf|Plaintext Files (*txt)|*.txt";// sets the filter for the saveFileDialog
- if (fileName != "")// if the file name is not equal to nothing, it starts to save the file (as opposed to save as)
- {
- if (fileName.EndsWith("txt"))// if the file name ends in "txt", the program saves the file as plaintext...
- {
- rtbContent.SaveFile(fileName, RichTextBoxStreamType.PlainText);// ...in the right file with the name provided in the fileName variable
- }
- else if (fileName.EndsWith("rtf"))// if the file name ends in "txt", the program saves the file as richtext...
- {
- rtbContent.SaveFile(fileName, RichTextBoxStreamType.RichText);// ...in the right file with the name provided in the fileName variable
- }
- }
- else// if the file Name is equal to nothing program starts the save as process
- {
- if (saveFileDialog.ShowDialog() != DialogResult.Cancel)//if the save file dialog is not cancelled, the save as program can proceed
- {
- fileName = saveFileDialog.FileName;//assigns the fileName provided by the save file dialog to the fileName variable
- string saveFileName = saveFileDialog.FileName;// sets the savefileName to the file name provided by the save file dialog
- if (saveFileName.EndsWith("txt"))// if the file name ends in "txt", the program saves the file as plaintext...
- {
- rtbContent.SaveFile(saveFileName, RichTextBoxStreamType.PlainText);// ...in the right file with the name provided in the fileName variable
- }
- else if (saveFileName.EndsWith("rtf")) // if the file name ends in "txt", the program saves the file as plaintext...
- {
- rtbContent.SaveFile(saveFileName, RichTextBoxStreamType.RichText);// ...in the right file with the name provided in the fileName variable
- }
- }
- fileName = Path.GetFileName(saveFileDialog.FileName);// gets the name of the file selected in the dialog without the directory
- Text = "RTF Editor" + ": " + fileName;// sets the title of the form to the default name plus the name of the file open
- }
- }
- }
- private void tsbCut_Click(object sender, EventArgs e) //working
- {
- if (rtbContent.SelectedText != "")//if the selected text in the rich text box is not nothing...
- {
- rtbContent.Cut();// ...Then cut the selection and pass it to the clipboard
- }
- }
- private void tsbCopy_Click(object sender, EventArgs e)//working
- {
- if (rtbContent.SelectedText != "")//if the selected text in the rich text box is not nothing...
- {
- rtbContent.Copy();// ...Then copy the selection and pass it to the clipboard
- }
- }
- private void tsbPaste_Click(object sender, EventArgs e)//working
- {
- if(Clipboard.GetDataObject() .GetDataPresent(DataFormats.Text) == true)//if there is text data present in the clip board...
- {
- rtbContent.Paste();// ...paste it into the rich text box
- }
- }
- private void tsbAbout_Click(object sender, EventArgs e)//working
- {
- MessageBox.Show("10053670", "About Me", MessageBoxButtons.OK, MessageBoxIcon.Information);//call MessageBox function, set text to "10053670", the title to "About Me" and give the box an ok button and an information symbol
- }
- private void rtbContent_TextChanged(object sender, EventArgs e)//working
- {
- textChanged = true;// set the text changed variable to true
- fileSaved = false;// set the file saved variable to false, this indicates that the text in the RTB has been changed and saved when necessary
- }
- private void RichTextEditor_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (fileSaved == false)
- {
- MessageBox.Show("ohbutt");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment