Click here to Skip to main content
15,895,142 members
Articles / Security / Encryption

Encrypting Editor Notepad Replacement

Rate me:
Please Sign up or sign in to vote.
4.85/5 (50 votes)
28 Jul 2014CPOL80 min read 179.4K   6.1K   112  
A C# .NET 3.5 (Win7) Windows Forms Application with source code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace CryptPad
{
	public partial class Replace : Form
	{
		CryptPad Crypt_Pad;

		public Replace ()
		{
			InitializeComponent ();
			this.Font = SystemFonts.MessageBoxFont;
		}

		private void Replace_Load (object sender, EventArgs e)
		{
			Crypt_Pad = (CryptPad) this.Owner;
			this.textFind.Text = Crypt_Pad.find_string;
			comboBoxPatterns.Tag = textFind;
			this.textReplace.Text = Crypt_Pad.replace_string;
			this.checkBoxRegEx.Checked = null != Crypt_Pad.find_regex;
			this.comboBoxPatterns.SelectedIndexChanged += new System.EventHandler (Crypt_Pad.comboBoxPatterns_SelectedIndexChanged);
			this.comboBoxPatterns.Click += new System.EventHandler (Crypt_Pad.comboBoxPatterns_Click);
			UpdateFindNextButton ();
			Crypt_Pad.Position (this);
		}

		private void Replace_FormClosed (object sender, FormClosedEventArgs e)
		{
			Crypt_Pad.replace_Form = null;
		}

		private void textFind_Enter (object sender, EventArgs e)
		{
			comboBoxPatterns.Tag = textFind;
			comboBoxPatterns.Items.Clear ();
			comboBoxPatterns.Items.AddRange (Crypt_Pad.combo_search);
			comboBoxPatterns.Items.AddRange (Crypt_Pad.combo_base);
			if (checkBoxRegEx.Checked)
				comboBoxPatterns.Items.AddRange (Crypt_Pad.combo_regex_find);
			comboBoxPatterns.Text = (string) comboBoxPatterns.Items[0];
		}

		private void textFind_TextChanged (object sender, EventArgs e)
		{
			UpdateFindNextButton ();
		}

		private void textReplace_Enter (object sender, EventArgs e)
		{
			comboBoxPatterns.Tag = textReplace;
			comboBoxPatterns.Items.Clear ();
			comboBoxPatterns.Items.AddRange (Crypt_Pad.combo_replacement);
			comboBoxPatterns.Items.AddRange (Crypt_Pad.combo_base);
			if (checkBoxRegEx.Checked)
				comboBoxPatterns.Items.AddRange (Crypt_Pad.combo_regex_replace);
			comboBoxPatterns.Text = (string) comboBoxPatterns.Items[0];
		}

		private void checkBoxRegEx_CheckedChanged (object sender, EventArgs e)
		{
			((TextBox) comboBoxPatterns.Tag).Focus ();
		}

		private void UpdateFindNextButton ()
		{
			if (0 < this.textFind.Text.Length)
			{
				this.buttonFindNext.Enabled = true;
				this.buttonReplace.Enabled = true;
				this.buttonReplaceAll.Enabled = true;
			}
			else
			{
				this.buttonFindNext.Enabled = false;
				this.buttonReplace.Enabled = false;
				this.buttonReplaceAll.Enabled = false;
			}
		}

		private void buttonFindNext_Click (object sender, EventArgs e)
		{
			Crypt_Pad.replace_string = this.textReplace.Text;
			Crypt_Pad.checkBoxMatchCase = this.checkBoxMatchCase.Checked;
			if (Crypt_Pad.checkBoxRegEx (this.checkBoxRegEx.Checked, this.textFind.Text))
				return;
			Crypt_Pad.FindNext (true);
			Crypt_Pad.Position (this);
		}

		private void buttonReplace_Click (object sender, EventArgs e)
		{
			Crypt_Pad.replace_string = this.textReplace.Text;
			Crypt_Pad.checkBoxMatchCase = this.checkBoxMatchCase.Checked;
			if (Crypt_Pad.checkBoxRegEx (this.checkBoxRegEx.Checked, this.textFind.Text))
				return;
			string str_replace = Crypt_Pad.Unescape (Crypt_Pad.replace_string);
			if (null != Crypt_Pad.find_regex)
			{
				Match match = Crypt_Pad.find_regex.Match (Crypt_Pad.txtBody.SelectedText);
				if (match.Success)
				{
					string s = Crypt_Pad.find_regex.Replace (Crypt_Pad.txtBody.SelectedText, str_replace);
					Crypt_Pad.txtBody.Paste (s);
					Crypt_Pad.txtBody.SelectionStart -= s.Length;
					Crypt_Pad.txtBody.SelectionLength = s.Length;
				}
			}
			else
			{
				string str_find = Crypt_Pad.Unescape (Crypt_Pad.find_string);
				if (this.checkBoxMatchCase.Checked)
				{
					if (str_find == Crypt_Pad.txtBody.SelectedText)
					{
						Crypt_Pad.txtBody.Paste (str_replace);
						Crypt_Pad.txtBody.SelectionStart -= str_replace.Length;
						Crypt_Pad.txtBody.SelectionLength = str_replace.Length;
					}
				}
				else
				{
					if (str_find.Equals (Crypt_Pad.txtBody.SelectedText,
						StringComparison.CurrentCultureIgnoreCase))
					{
						Crypt_Pad.txtBody.Paste (str_replace);
						Crypt_Pad.txtBody.SelectionStart -= str_replace.Length;
						Crypt_Pad.txtBody.SelectionLength = str_replace.Length;
					}
				}
			}
			Crypt_Pad.FindNext (true);
			Crypt_Pad.Position (this);
		}

		private void buttonReplaceAll_Click (object sender, EventArgs e)
		{
			Crypt_Pad.RememberCaretPosition ();
			int i, j;
			Crypt_Pad.replace_string = this.textReplace.Text;
			Crypt_Pad.checkBoxMatchCase = this.checkBoxMatchCase.Checked;
			if (Crypt_Pad.checkBoxRegEx (this.checkBoxRegEx.Checked, this.textFind.Text))
				return;
			// first we determine if there are zero, one, or more matches
			string s = Crypt_Pad.txtBody.Text;
			string str_replace = Crypt_Pad.Unescape (Crypt_Pad.replace_string);
			if (null != Crypt_Pad.find_regex)
			{
				Match match = Crypt_Pad.find_regex.Match (s);
				if (!match.Success)
				{
					Crypt_Pad.RestoreCaretPosition ();
					Crypt_Pad.NotFound ();
					return;
				}
				int index = match.Groups[0].Index;
				int length = match.Groups[0].Value.Length;
				s = match.Groups[0].Value;
				match = match.NextMatch ();
				if (!match.Success)
				{
					s = Crypt_Pad.find_regex.Replace (s, str_replace);
					ReplaceSelect (index, length, s);
					return;
				}
				s = Crypt_Pad.find_regex.Replace (Crypt_Pad.txtBody.Text, str_replace);
				Crypt_Pad.txtBody.SelectAll ();
				Crypt_Pad.txtBody.Paste (s);
				Crypt_Pad.RestoreCaretPosition ();
				return;
			}
			// simple string replace
			string str_find = Crypt_Pad.Unescape (Crypt_Pad.find_string);
			if (checkBoxMatchCase.Checked)
			{
				i = s.IndexOf (str_find);
				j = s.LastIndexOf (str_find);
			}
			else
			{
				i = s.IndexOf (str_find, StringComparison.CurrentCultureIgnoreCase);
				j = s.LastIndexOf (str_find, StringComparison.CurrentCultureIgnoreCase);
			}
			// zero matches so give the error Notepad doesn't
			if (0 > i)
			{
				Crypt_Pad.RestoreCaretPosition ();
				Crypt_Pad.NotFound ();
				return;
			}
			// one match so change just that one for simple undo
			if (i == j)
			{
				ReplaceSelect (i, str_find.Length, str_replace);
				return;
			}
			// multiple matches so undo the whole shebang
			if (checkBoxMatchCase.Checked)
			{
				// easy to use standard function
				s = s.Replace (str_find, str_replace);
			}
			else
			{
				// harder to do our own
				j = str_find.Length;

				for (; ; )
				{
					s = s.Substring (0, i) + str_replace + s.Substring (i + j);
					i = s.IndexOf (str_find, i + str_replace.Length, StringComparison.CurrentCultureIgnoreCase);
					if (0 > i)
						break;
				}
			}
			Crypt_Pad.txtBody.SelectAll ();
			Crypt_Pad.txtBody.Paste (s);
			Crypt_Pad.RestoreCaretPosition ();
		}

		/// <summary> Common to regex and simple single case replace
		/// </summary>
		/// <param name="i"></param>
		/// <param name="j"></param>
		/// <param name="s"></param>
		private void ReplaceSelect (int i, int j, string s)
		{
			Crypt_Pad.txtBody.Select (i, j);
			Crypt_Pad.txtBody.Paste (s);
			Crypt_Pad.SelectScroll (i, s.Length);
			Messagebox_Class.Messagebox.Show ("Selected the only replacement.");
			Crypt_Pad.SelectScroll (i, s.Length);
			Crypt_Pad.RestoreCaretPosition ();
			return;
		}

		private void buttonCancel_Click (object sender, EventArgs e)
		{
			this.Close ();
		}

		private void contextMenuStripFind_Opening (object sender, CancelEventArgs e)
		{
			toolStripMenuUndo.Enabled = textFind.CanUndo;
			toolStripMenuPaste.Enabled = Clipboard.ContainsText ();
			if (0 < textFind.SelectionLength)
			{
				toolStripMenuCut.Enabled = true;
				toolStripMenuCopy.Enabled = true;
				toolStripMenuDelete.Enabled = true;
			}
			else
			{
				toolStripMenuCut.Enabled = false;
				toolStripMenuCopy.Enabled = false;
				toolStripMenuDelete.Enabled = false;
			}
		}

		private void toolStripMenuUndo_Click (object sender, EventArgs e)
		{
			textFind.Undo ();
		}

		private void toolStripMenuCut_Click (object sender, EventArgs e)
		{
			string s = textFind.SelectedText;
			if (!checkBoxRegEx.Checked)
				s = Crypt_Pad.Unescape (s);
			Clipboard.SetText (s);
			textFind.Paste ("");
		}

		private void toolStripMenuCopy_Click (object sender, EventArgs e)
		{
			string s = textFind.SelectedText;
			if (!checkBoxRegEx.Checked)
				s = Crypt_Pad.Unescape (s);
			Clipboard.SetText (s);
		}

		private void toolStripMenuPaste_Click (object sender, EventArgs e)
		{
			string s = Clipboard.GetText ();
			if (!checkBoxRegEx.Checked)
				s = Crypt_Pad.Escape (s);
			textFind.Paste (s);
		}

		private void toolStripMenuDelete_Click (object sender, EventArgs e)
		{
			if (0 == this.textFind.SelectionLength)
				textFind.SelectionLength = 1;
			textFind.Paste ("");
		}

		private void toolStripMenuSelectAll_Click (object sender, EventArgs e)
		{
			textFind.SelectAll ();
		}

		private void contextMenuStripReplace_Opening (object sender, CancelEventArgs e)
		{
			toolStripReplaceMenuUndo.Enabled = textReplace.CanUndo;
			toolStripReplaceMenuPaste.Enabled = Clipboard.ContainsText ();
			if (0 < textReplace.SelectionLength)
			{
				toolStripReplaceMenuCut.Enabled = true;
				toolStripReplaceMenuCopy.Enabled = true;
				toolStripReplaceMenuDelete.Enabled = true;
			}
			else
			{
				toolStripReplaceMenuCut.Enabled = false;
				toolStripReplaceMenuCopy.Enabled = false;
				toolStripReplaceMenuDelete.Enabled = false;
			}
		}

		private void toolStripReplaceMenuUndo_Click (object sender, EventArgs e)
		{
			textReplace.Undo ();
		}

		private void toolStripReplaceMenuCut_Click (object sender, EventArgs e)
		{
			string s = textReplace.SelectedText;
			if (!checkBoxRegEx.Checked)
				s = Crypt_Pad.Unescape (s);
			Clipboard.SetText (s);
			textReplace.Paste ("");
		}

		private void toolStripReplaceMenuCopy_Click (object sender, EventArgs e)
		{
			string s = textReplace.SelectedText;
			if (!checkBoxRegEx.Checked)
				s = Crypt_Pad.Unescape (s);
			Clipboard.SetText (s);
		}

		private void toolStripReplaceMenuPaste_Click (object sender, EventArgs e)
		{
			string s = Clipboard.GetText ();
			if (!checkBoxRegEx.Checked)
				s = Crypt_Pad.Escape (s);
			textReplace.Paste (s);
		}

		private void toolStripReplaceMenuDelete_Click (object sender, EventArgs e)
		{
			if (0 == this.textReplace.SelectionLength)
				textReplace.SelectionLength = 1;
			textReplace.Paste ("");
		}

		private void toolStripReplaceMenuSelectAll_Click (object sender, EventArgs e)
		{
			textReplace.SelectAll ();
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer IAUA End Time Ministry
United States United States
I am a retired Software Systems Design Engineer experienced with IEEE standards and the entire IEEE software development life cycle. Concept Exploration, Requirements, Design, Implementation, Test, Installation and Checkout, Operation and Maintenance. I enjoy working with people and solving customer problems.

I am currently a writer for my personal ministry: IAUA End Time Ministry

Comments and Discussions