ohbutt69

rtf editor main form

Dec 7th, 2011
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace Rich_Text_Editor
  12. {
  13.     public partial class frmRichTextEditor : Form
  14.     {
  15.         public frmRichTextEditor()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         bool textSaved = true;// creates a boolean variable and
  21.         string fileName = "";// creates filename variable as a string
  22.         string fileTitle;// creates the fileTitle variable as a string
  23.         DialogResult result;// declares result as a DialogResult
  24.  
  25.         private void msNew_Click(object sender, EventArgs e)//checks if the user wants to save before a new document is created
  26.         {
  27.             dlgSave.Filter = "Rich Text Files (*.rtf)|*.rtf|Plaintext Files (*txt)|*.txt";// sets the save file dialog filter
  28.             if (textSaved == false)// if the text has not been saved
  29.             {
  30.                 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
  31.                 if (result == DialogResult.Yes)// if the user clicked yes
  32.                 {
  33.                     msSaveFile_Click(sender, e);// simulate a click on the save file button, calling the save function
  34.                 }
  35.                 else if (result == DialogResult.No)// if the user clicked no
  36.                 {
  37.                     textSaved = false;// set the textSaved value to no, indicating the text needs to be saved
  38.                     rtbContent.Clear();// clear the rich text box
  39.                     Text = "RTF Editor: untitled";// set the title of the program to show the document is a new document
  40.                 }
  41.             }
  42.         }
  43.         private void msOpenFile_Click(object sender, EventArgs e)// asks the user if they want to save their document and opens a new file
  44.         {
  45.             if (textSaved == false)// if the text has not been saved
  46.             {
  47.                 msSaveFile_Click(sender, e);// simulate a click on the save file button, calling the save function
  48.             }
  49.             dlgOpen.Filter = "Rich Text Files (*.rtf)|*.rtf|Plaintext Files (*txt)|*.txt";// set the open dialog filter
  50.             if (dlgOpen.ShowDialog() != DialogResult.Cancel)// if the dialog cancel button was not clicked
  51.             {
  52.                 fileName = dlgOpen.FileName;//sets the fileName variable to the file name selected in the open file dialog
  53.                 if (fileName.EndsWith("txt"))// if the fileName variable ends with "txt"
  54.                 {
  55.                     rtbContent.LoadFile(fileName, RichTextBoxStreamType.PlainText);// load content into the rich text box in plain text format
  56.                 }
  57.                 else if (fileName.EndsWith("rtf"))// else is the fileName variable ends in "rtf"
  58.                 {
  59.                     rtbContent.LoadFile(fileName, RichTextBoxStreamType.RichText);// load content into the rich text box in rich text format
  60.                 }
  61.                 textSaved = true;// set the textSaved variable to true, indicating that the document was saved
  62.                 fileTitle = Path.GetFileName(fileName);// sets the file title variable to the file name found in the folder
  63.                 Text = "RTF Editor: " + fileTitle;// sets the title of the program to "RTF Editor: " + the fileTitle variable
  64.             }
  65.         }
  66.         private void msSaveFile_Click(object sender, EventArgs e)// saves the file
  67.         {
  68.             dlgSave.Filter = "Rich Text Files (*.rtf)|*.rtf|Plaintext Files (*txt)|*.txt";// sets the filter for the save file dialog
  69.             if (textSaved == false)// if the document has not been saved
  70.             {
  71.                 if (fileName != "")// if the file name is not equal to nothing
  72.                 {
  73.                     if (fileName.EndsWith("txt"))//  if the file name ends with "txt"
  74.                     {
  75.                         rtbContent.SaveFile(fileName, RichTextBoxStreamType.PlainText);// save the content in the rtb as plain text format
  76.                     }
  77.                     else if (fileName.EndsWith("rtf"))// else if the file name ends with "rtf"
  78.                     {
  79.                         rtbContent.SaveFile(fileName, RichTextBoxStreamType.RichText);// save the content in the rtb as rich text format
  80.                     }
  81.                 }
  82.                 else// else (if the filename does equal nothing)
  83.                 {
  84.                     if (dlgSave.ShowDialog() != DialogResult.Cancel)// if the save file dialog was not cancelled
  85.                     {
  86.                         fileName = dlgSave.FileName;// set the file name variable to the filename specified in the save file dialog
  87.                         if (fileName.EndsWith("txt"))// if the file name ends with "txt"
  88.                         {
  89.                             rtbContent.SaveFile(fileName, RichTextBoxStreamType.PlainText);// save the content in the rtb as plain text format
  90.                         }
  91.                         else if (fileName.EndsWith("rtf"))// if the file name ends with "txt"
  92.                         {
  93.                             rtbContent.SaveFile(fileName, RichTextBoxStreamType.RichText);// save the content in the rtb as rich text format
  94.                         }
  95.                     }
  96.                 }
  97.                 textSaved = true;// set the text saved variable to true, indicating the text file has been saved
  98.                 fileTitle = Path.GetFileName(fileName);// sets the file title variable to the file name found in the folder
  99.                 Text = "RTF Editor: " + fileTitle;// sets the title of the program to "RTF Editor: " + the fileTitle variable
  100.             }
  101.         }
  102.         private void msSaveAs_Click(object sender, EventArgs e)// simulates a save as for the text in the document
  103.         {
  104.             dlgSave.Filter = "Rich Text Files (*.rtf)|*.rtf|Plaintext Files (*txt)|*.txt";// sets the filter for the save file dialog
  105.             if (dlgSave.ShowDialog() != DialogResult.Cancel)// the save file dialog was not cancelled
  106.             {
  107.                 fileName = dlgSave.FileName;// set the file name variable to the name of the file determined by the save file dialog
  108.                 if (fileName.EndsWith("txt"))// if the file ends with "txt"
  109.                 {
  110.                     rtbContent.SaveFile(fileName, RichTextBoxStreamType.PlainText);// saves the content of the rich text box in plain text
  111.                 }
  112.                 else if (fileName.EndsWith("rtf"))// if the file ends with "rtf"
  113.                 {
  114.                     rtbContent.SaveFile(fileName, RichTextBoxStreamType.RichText);// saves the content of the rich text box inrich text
  115.                 }
  116.                 textSaved = true;// set the text saved variable to true, indicating the text file has been saved
  117.                 fileTitle = Path.GetFileName(fileName);// sets the file title variable to the file name found in the folder
  118.                 Text = "RTF Editor: " + fileTitle;// sets the title of the program to "RTF Editor: " + the fileTitle variable
  119.             }
  120.         }
  121.         private void msExit_Click(object sender, EventArgs e)//closes the program
  122.         {
  123.             this.Close();// closes the program
  124.         }
  125.         private void msCut_Click(object sender, EventArgs e)//cuts selected text
  126.         {
  127.             if (rtbContent.SelectedText != "")// if the selected text is not equal to nothing
  128.             {
  129.                 rtbContent.Cut();//cut the selection
  130.             }
  131.         }
  132.         private void msCopy_Click(object sender, EventArgs e)//copies selected text
  133.         {
  134.             if (rtbContent.SelectedText != "")// if the selected text is not equal to nothing
  135.             {
  136.                 rtbContent.Copy();// copies the selected text
  137.             }
  138.         }
  139.         private void msPaste_Click(object sender, EventArgs e)// pastes text into the rich text box
  140.         {
  141.             if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)// if there is text data present in the clipboard
  142.             {
  143.                 rtbContent.Paste();//paste the text into the tich text box
  144.             }
  145.         }
  146.         private void msFont_Click(object sender, EventArgs e)// opens the font dialog box
  147.         {
  148.             if (dlgFont.ShowDialog() != DialogResult.Cancel)//if the font dialog box was not cancelled
  149.             {
  150.                 rtbContent.Font = dlgFont.Font;// change the font to the font selected in the font dialog
  151.             }
  152.         }
  153.         private void msColour_Click(object sender, EventArgs e)// opens the colour dialog box
  154.         {
  155.             if (dlgColour.ShowDialog() != DialogResult.Cancel)//if the colour dialog box was not cancelled
  156.             {
  157.                 rtbContent.ForeColor = dlgColour.Color;// set the colour of the text to the colour selected in the colour dialog
  158.             }
  159.         }
  160.         private void msFindText_Click(object sender, EventArgs e)// opens the find text form
  161.         {
  162.             frmFindReplace formFindReplace = new frmFindReplace();// declares a new instance of the find and replace form
  163.             formFindReplace.Owner = this;// sets the owner of the find and replace form as the main form (form1)
  164.             formFindReplace.Show();//shows the find and replace form
  165.         }
  166.         private void msAboutMe_Click(object sender, EventArgs e)// shows a message box detailing my student number
  167.         {
  168.             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.
  169.         }
  170.         private void tsbNew_Click(object sender, EventArgs e)//creates a new document
  171.         {
  172.             dlgSave.Filter = "Rich Text Files (*.rtf)|*.rtf|Plaintext Files (*txt)|*.txt";// sets the save dialog filter
  173.             if (textSaved == false)// if the text has not been saved
  174.             {
  175.                 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
  176.                 if (result == DialogResult.Yes)// if the result was yes
  177.                 {
  178.                     msSaveFile_Click(sender, e);// simulate a click on the save file button, calling the save function
  179.                 }
  180.                 else if (result == DialogResult.No)// else if the result was no
  181.                 {
  182.                     textSaved = false;// set the text saved variable to false, indicating the text has not been saved
  183.                     rtbContent.Clear();// clear the rich text box content
  184.                     Text = "RTF Editor: untitled";// set the title of the form to "untitled"
  185.                 }
  186.             }
  187.         }
  188.         private void tsbOpen_Click(object sender, EventArgs e)// opens a document
  189.         {
  190.             if (textSaved == false)// if the text has not been saved
  191.             {
  192.                 msSaveFile_Click(sender, e);// simulate a click on the save file button, calling the save function
  193.             }
  194.             dlgOpen.Filter = "Rich Text Files (*.rtf)|*.rtf|Plaintext Files (*txt)|*.txt";// set the dialog open filter
  195.             if (dlgOpen.ShowDialog() != DialogResult.Cancel)// if the open dialog was not cancelled
  196.             {
  197.                 fileName = dlgOpen.FileName;// set the filename variable to the name of the file specified in the dialog
  198.                 if (fileName.EndsWith("txt"))// if the file name ends with "txt"
  199.                 {
  200.                     rtbContent.LoadFile(fileName, RichTextBoxStreamType.PlainText);// load the file into the rich text box as a plain text file type
  201.                 }
  202.                 else if (fileName.EndsWith("rtf"))// else if the file name ends with "rtf"
  203.                 {
  204.                     rtbContent.LoadFile(fileName, RichTextBoxStreamType.RichText);// load the file into the rich text box as a rich text file type
  205.                 }
  206.                 textSaved = true;// set the textSaved variable to true, indicating the text was saved
  207.                 fileTitle = Path.GetFileName(fileName);//sets the fileTitle  variable to the path of the selected file
  208.                 Text = "RTF Editor: " + fileTitle;// sets the title of the program to "RTF Editor: " + the fileTitle variable
  209.             }
  210.         }
  211.         private void tsbSave_Click(object sender, EventArgs e)// saves a file
  212.         {
  213.             dlgSave.Filter = "Rich Text Files (*.rtf)|*.rtf|Plaintext Files (*txt)|*.txt";// sets the save file dialog filter
  214.             if (textSaved == false)// if the text has not been saved
  215.             {
  216.                 if (fileName != "")// if the fileName variable is not nothing
  217.                 {
  218.                     if (fileName.EndsWith("txt"))// if the filename variable ends with "txt"
  219.                     {
  220.                         rtbContent.SaveFile(fileName, RichTextBoxStreamType.PlainText);// save the file in a plain text format
  221.                     }
  222.                     else if (fileName.EndsWith("rtf"))//else if the file ends in "rtf"
  223.                     {
  224.                         rtbContent.SaveFile(fileName, RichTextBoxStreamType.RichText);// save the file in a rich text format
  225.                     }
  226.                 }
  227.                 else// else (if the file name variable does equal something)
  228.                 {
  229.                     if (dlgSave.ShowDialog() != DialogResult.Cancel)// if the save dialog was not cancelled
  230.                     {
  231.                         fileName = dlgSave.FileName;// sets the fileName variable as the file name specified by the dialog
  232.                         if (fileName.EndsWith("txt"))// if the fileName ends with "txt"
  233.                         {
  234.                             rtbContent.SaveFile(fileName, RichTextBoxStreamType.PlainText);// save the file in plain text format
  235.                         }
  236.                         else if (fileName.EndsWith("rtf"))// else if the file name ends with "rtf"
  237.                         {
  238.                             rtbContent.SaveFile(fileName, RichTextBoxStreamType.RichText);// save the file in plain text format
  239.                         }
  240.                     }
  241.                 }
  242.                 textSaved = true;// set the textSaved variable to true, indicating the text was saved
  243.                 fileTitle = Path.GetFileName(fileName);//sets the fileTitle  variable to the path of the selected file
  244.                 Text = "RTF Editor: " + fileTitle;// sets the title of the program to "RTF Editor: " + the fileTitle variable
  245.             }
  246.         }
  247.         private void tsbCut_Click(object sender, EventArgs e)//cuts selected text
  248.         {
  249.             if (rtbContent.SelectedText != "")// the selected text is not equal to nothing
  250.             {
  251.                 rtbContent.Cut();// cut the selected text
  252.             }
  253.         }
  254.         private void tsbCopy_Click(object sender, EventArgs e)// copies selected text
  255.         {
  256.             if (rtbContent.SelectedText != "")// if the selected text is not equal to nothing
  257.             {
  258.                 rtbContent.Copy();// copy the selected text
  259.             }
  260.         }
  261.         private void tsbPaste_Click(object sender, EventArgs e)// pastes text into the document
  262.         {
  263.             if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)// if there is text data present in the clipboard
  264.             {
  265.                 rtbContent.Paste();// pastes the clipboard data into the rich text box
  266.             }
  267.         }
  268.         private void tsbAboutMe_Click(object sender, EventArgs e)// shows a message box detailing my student number
  269.         {
  270.             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.
  271.         }
  272.         private void rtbContent_TextChanged(object sender, EventArgs e)// if the text value of the rich text box has been changed
  273.         {
  274.             textSaved = false;// set the text saved variable to false
  275.         }
  276.         private void frmRichTextEditor_FormClosing(object sender, FormClosingEventArgs e)// this event handler fires with the form starts to close
  277.         {
  278.             if (textSaved == false)// if the text has not been saved
  279.             {
  280.                 dlgSave.Filter = "Rich Text Files (*.rtf)|*.rtf|Plaintext Files (*txt)|*.txt";// sets the filter for the save dialog
  281.                 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
  282.                 if (result == DialogResult.Yes)// if the result of the message box dialog was yes
  283.                 {
  284.                     msSaveFile_Click(sender, e);// simulate a save file button click
  285.                 }
  286.                 else if (result == DialogResult.Cancel)// if the result of the dialog is cancel
  287.                 {
  288.                     e.Cancel = true;// cancel the shutdown
  289.                 }
  290.             }
  291.         }
  292.     }
  293. }// end of program
  294.  
  295.  
Advertisement
Add Comment
Please, Sign In to add comment