Advertisement
GlobalAccessSoftware

Quick And Easy Text Editor in C#.NET

Sep 17th, 2017 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.13 KB | None | 0 0
  1.  
  2. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  3. // ShowMeEdit.cs v5.7.7.86 by -JpE-
  4. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  5. // Modeled after ShowMeForm But Editable
  6. // Ready to testwell and evolve, etc.
  7. // v5.7.7.88 21-Aug-2017 Early Beta
  8. // Still Designing as I Develop this.
  9. // v5.7.7.89 Opts Out of Dual Use mods.
  10. // Leave commented out code for that in
  11. // v5.7.7.91 MUMs the Word. 22-Aug-2017
  12. // v5.7.8.05 28-Aug-2017 Context Menu
  13. // v5.7.8.07 30-Aug--17 Fixed & trimmed.
  14. // v5.7.8.17 08-Sep-2017 Prep for Rev.
  15. // v5.7.8.21 19-Sep-2017 File-Menu stuff
  16. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  17.  
  18. // Dependencies
  19. using System;
  20. using System.Globalization;
  21. using System.IO;
  22. using System.Windows.Forms;
  23.  
  24. // Class Aliases
  25. using AP = WhoAreYou.AppStatic;
  26. // Properties Aliases
  27. using PS = WhoAreYou.Properties.PersonSettings;
  28. using PR = WhoAreYou.Properties.PersonResources;
  29. using OS = WhoAreYou.Properties.OptionsSettings;
  30. using AU = WhoAreYou.Properties.AppUserSettings;
  31.  
  32. namespace WhoAreYou
  33. {
  34.   public sealed partial class ShowMeEdit : Form
  35.   {
  36.     // Class Global Properties
  37.     private bool   Ok      { get; set; }
  38.     private string TextWas { get; set; }
  39.     private readonly string _original;
  40.     private string Edited  { get; set; }
  41.     public  string // ^v Cool Trick, NOTE!
  42.       EditedText { get{ return Edited; } }
  43.     private string ClipBoard { get; set; }
  44.     // Class Global ^ for the Edit/Context Menu Items.
  45.  
  46. #region ShowMeEdit Form Related Code v5.7.8.03 28-Aug-2017
  47.  
  48.     public ShowMeEdit(string text, string title) //, bool read) // MUMs
  49.     {
  50.       InitializeComponent();
  51.  
  52.       //ShowMeRichTextBox.ReadOnly = read; // <-= ^v Multiple Use Mods.
  53.       _original = TextWas = ShowMeRichTextBox.Text = Edited = text;
  54.       // NOTE: quadruple assignment ^ at Start.
  55.       Text += title; // Edit This ^ text file.
  56.  
  57.       // // KEEP THIS! v^ MUMs the word.
  58.       //SaveButton.Visible  = OkButton.Visible = // MUMs ^
  59.       //  MenuStrip.Visible = !read;
  60.       //if (read) CanceButton.Text = PR.SME_EditClose;
  61.     }
  62.  
  63.     private void OkButtonClick(object sender, EventArgs e)
  64.     {
  65.       if (Edited != ShowMeRichTextBox.Text)
  66.       {
  67.         if (AP.YesNoDialog(
  68.           "Unsaved Changes were made.\n\n" +
  69.           "Would you like to Save These Changes?",
  70.           "Save Changes?"))
  71.         {
  72.           Edited = ShowMeRichTextBox.Text;
  73.           Ok = true;
  74.         }
  75.       }
  76.       Close();
  77.     }
  78.  
  79.     private void SaveButtonClick(object sender, EventArgs e)
  80.     {
  81.       if (Edited == ShowMeRichTextBox.Text) return;
  82.  
  83.       Ok = true;
  84.       Edited = TextWas = ShowMeRichTextBox.Text;
  85.  
  86.       if (OS.Default.MiniVerb) return;
  87.       MessageBox.Show(PR.SME_ChangesSaved);
  88.     }
  89.  
  90.     private void EditFormClosing(
  91.       object sender, FormClosingEventArgs e)
  92.     {
  93.       if (!Ok) Edited = TextWas;
  94.       else
  95.         if (ShowMeRichTextBox.Text != Edited)
  96.           OkButtonClick("Form Closing", EventArgs.Empty);
  97.       PS.Default.Save();
  98.     }
  99. #endregion ShowMeEdit Form Related Code v5.7.8.03 28-Aug-2017
  100.     //\\//\\//\\////\\//\\//\\////\\//\\//\\////\\//\\//\\//
  101.  
  102.  
  103. #region File Menu
  104.  
  105.     private void SaveChangesClick(object sender, EventArgs e)
  106.     {
  107.       Ok = true;
  108.       Edited = TextWas = ShowMeRichTextBox.Text;
  109.     }
  110.  
  111.     // Start over with Previous Text
  112.     // if that equals the _original txt then no Ok.
  113.     private void RevertClick(object sender, EventArgs e)
  114.     {
  115.       if (!OS.Default.MiniVerb)
  116.         if (!AP.YesNoDialog(
  117.           "Revert back to previous save?")) return;
  118.       ShowMeRichTextBox.Text = TextWas;
  119.       if (TextWas.Equals(_original)) Ok = false;
  120.     }
  121.  
  122.     private void ClearAllClick(object sender, EventArgs e)
  123.     {
  124.       const string
  125.         txt = "Wipe this entire document:\n\n" +
  126.             "Are You Sure?";
  127.       if (!OS.Default.MiniVerb)
  128.         if (!AP.YesNoDialog(
  129.           txt, "Clear All Text?")) return;
  130.       TextWas = ShowMeRichTextBox.Text;
  131.       ShowMeRichTextBox.Text = "";
  132.     }
  133.  
  134.     // todo Largely Completed Above Here. /\/\/\/\/\/\/\/\/\
  135.  
  136.     private void ImportClick(object sender, EventArgs e)
  137.     {
  138.       // todo testwell
  139.       var fn = GetDefaultFileName();
  140.       if (!string.IsNullOrEmpty(PS.Default.SME_Imported))
  141.         fn = PS.Default.SME_Imported;
  142.       else PS.Default.SME_Imported = fn; // <-= TestWell this branch.
  143.       const string tt = "Importing this text file:";
  144.       using (var d = new OpenFileDialog())
  145.       {
  146.         d.Title = tt;
  147.         d.FileName = Path.GetFileName(fn);
  148.         d.InitialDirectory = Path.GetDirectoryName(fn);
  149.         d.DefaultExt = PS.Default.TxtExt;
  150.         if (!d.ShowDialog().Equals(DialogResult.OK)
  151.           || string.IsNullOrEmpty(d.FileName)) return;
  152.         PS.Default.SME_Imported = d.FileName;
  153.       }
  154.       TextWas = ShowMeRichTextBox.Text =
  155.         File.ReadAllText(PS.Default.SME_Imported);
  156.       PS.Default.Save();
  157.       //Validate(); // todo remnant?
  158.     }
  159.    
  160.     /// <summary> Saves Duplicating this Code Twice Here.
  161.     /// <returns></returns>
  162.     private static string GetDefaultFileName()
  163.     {
  164.       var fn = AU.Default.RootDir +
  165.         "\\" + AU.Default.FileName;
  166.       var rc = (InfoForm.CurrentRecord + 1)
  167.         .ToString(CultureInfo.InvariantCulture);
  168.       fn = fn.Substring(0, fn.Length - 4) +
  169.         "_Rec" + rc + "_Note.txt";
  170.       return fn;
  171.     }
  172.  
  173.     private void ExportClick(object sender, EventArgs e)
  174.     {
  175.       var fn = GetDefaultFileName();
  176.       if (!string.IsNullOrEmpty(PS.Default.SME_Exported))
  177.         fn = PS.Default.SME_Exported; // test this branch now!
  178.       else PS.Default.SME_Exported = fn; // this branch tests well...
  179.       while (File.Exists(fn))
  180.         fn = AP.ItsACopy(fn); // todo test this too
  181.       // but it's 100% tested code reused here as far
  182.       // as the AppStatic Class (AP) is concerned..
  183.       const string tt = "Exporting this to text file:";
  184.       using (var s = new SaveFileDialog())
  185.       {
  186.         s.Title = tt;
  187.         s.FileName = Path.GetFileName(fn);
  188.         s.InitialDirectory = Path.GetDirectoryName(fn);
  189.         if (!s.ShowDialog().Equals(DialogResult.OK)
  190.           || string.IsNullOrEmpty(s.FileName)) return;
  191.         PS.Default.SME_Exported = s.FileName;
  192.       }
  193.       // Now simply Write the string to that path.
  194.       File.WriteAllText(PS.Default.SME_Exported,
  195.         ShowMeRichTextBox.Text);
  196.       PS.Default.Save();
  197.     }
  198. #endregion File Menu
  199.     //\\//\\//\\////\\//\\//\\////\\//\\//\\////\\//\\//\\//
  200.  
  201.  
  202. #region EditMenu, ClipBoard && ContextMenu Items covered here.
  203.  
  204.     /* Brought in from InfoFormMenuAndEdit.cs:
  205.      * v5.7.8.03 28-Aug-2017
  206.      * Allows all the functionality of an editor.
  207.      * Sorta, working on it anyway. Not to bad of a start.
  208.      */
  209.  
  210.     private void
  211.       UndoClick(object sender, EventArgs e)
  212.     {
  213.       if (ActiveControl == null) return;
  214.       var tx = ActiveControl as RichTextBox;
  215.       if (tx != null)
  216.       {
  217.         ClipBoard = tx.SelectedText;
  218.         tx.Undo();
  219.       }
  220.       var rtx = ActiveControl as RichTextBox;
  221.       if (rtx == null) return;
  222.       ClipBoard = rtx.SelectedText;
  223.       rtx.Undo();
  224.     }
  225.  
  226.     private void
  227.       RedoClick(object sender, EventArgs e)
  228.     {
  229.       if (ActiveControl == null) return;
  230.       var tx = ActiveControl as RichTextBox;
  231.       if (tx != null)
  232.       {
  233.         tx.SelectedText = ClipBoard;
  234.       }
  235.       var rtx = ActiveControl as RichTextBox;
  236.       if (rtx == null) return;
  237.       rtx.SelectedText = ClipBoard;
  238.       rtx.Redo();
  239.     }
  240.  
  241.     private void
  242.       CutClick(object sender, EventArgs e)
  243.     {
  244.       if (ActiveControl == null) return;
  245.       var tx = ActiveControl as RichTextBox;
  246.       if (tx != null)
  247.       {
  248.         ClipBoard = tx.SelectedText;
  249.         tx.Cut();
  250.       }
  251.       var rtx = ActiveControl as RichTextBox;
  252.       if (rtx == null) return;
  253.       ClipBoard = rtx.SelectedText;
  254.       rtx.Cut();
  255.     }
  256.  
  257.     private void CopyClick(object sender, EventArgs e)
  258.     {
  259.       if (ActiveControl == null) return;
  260.       var tx = ActiveControl as RichTextBox;
  261.       if (tx != null)
  262.       {
  263.         ClipBoard = tx.SelectedText;
  264.         tx.Copy();
  265.       }
  266.       var rtx = ActiveControl as RichTextBox;
  267.       if (rtx == null) return;
  268.       ClipBoard = rtx.SelectedText;
  269.       rtx.Copy();
  270.     }
  271.  
  272.     private void PasteClick(object sender, EventArgs e)
  273.     {
  274.       if (ActiveControl == null) return;
  275.       var tx = ActiveControl as RichTextBox;
  276.       if (tx != null)
  277.       {
  278.         ClipBoard = tx.SelectedText;
  279.         tx.Paste();
  280.       }
  281.       var rtx = ActiveControl as RichTextBox;
  282.       if (rtx == null) return;
  283.       ClipBoard = rtx.SelectedText;
  284.       //rtx.Paste();
  285.     }
  286.  
  287.     private void SelectAllClick(object sender, EventArgs e)
  288.     {
  289.       if (ActiveControl == null) return;
  290.       var tx = ActiveControl as RichTextBox;
  291.       if (tx != null)
  292.       {
  293.         tx.SelectAll();
  294.       }
  295.       var rtx = ActiveControl as RichTextBox;
  296.       if (rtx == null) return;
  297.       rtx.SelectAll();
  298.     }
  299. #endregion Edit Menu
  300.     //\\//\\//\\////\\//\\//\\////\\//\\//\\////\\//\\//\\//
  301.  
  302.  
  303. #region Options Menu
  304.  
  305.     private void ChangeFontClick(object sender, EventArgs e)
  306.     {
  307.       var f = PS.Default.SME_Font;
  308.  
  309.       Zone();
  310.  
  311.       // todo
  312.     }
  313.  
  314.     private void BackColorClick(object sender, EventArgs e)
  315.     {
  316.       var bc = PS.Default.SME_BackColor;
  317.  
  318.       using (var c = new ColorDialog())
  319.       {
  320.  
  321.         c.ShowDialog();
  322.  
  323.       }
  324.       Zone();
  325.       // todo
  326.     }
  327.  
  328.     private void ForeColorClick(object sender, EventArgs e)
  329.     {
  330.       var fc = PS.Default.SME_ForeColor;
  331.  
  332.       using (var c = new ColorDialog())
  333.       {
  334.  
  335.         c.ShowDialog();
  336.  
  337.       }
  338.       Zone();
  339.       // todo
  340.     }
  341.  
  342.     // todo testcode
  343.     private static void Zone()
  344.     {
  345.       const string t =
  346.         "Under Construction, Please Return.";
  347.       MessageBox.Show(t);
  348.     }
  349.  
  350.     private void WordWrapClick(object sender, EventArgs e)
  351.     {
  352.       WordWrapMenuItem.Checked =
  353.         ShowMeRichTextBox.WordWrap =
  354.           !ShowMeRichTextBox.WordWrap;
  355.     }
  356. #endregion Options Menu
  357.     //\\//\\//\\////\\//\\//\\////\\//\\//\\////\\//\\//\\//
  358.    
  359.  
  360.   }
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement