Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Encrypting Editor Notepad Replacement

, 28 Jul 2014 CPOL
A C# .NET 3.5 (Win7) Windows Forms Application with source code
CryptPad-SourceOnly.zip
CryptPad
CryptPad (read-only).lnk
CryptPad.suo
CryptPad
CryptPad.csproj.user
CryptPad.ico
CryptPad.png
Properties
Settings.settings
hack.safe
Script.CryptPad
SendTo.lnk
Settings.lnk
CryptPad.png
CryptPad.zip
CryptPad (read-only).lnk
CryptPad.suo
CryptPad.csproj.user
CryptPad.ico
CryptPad.png
Settings.settings
hack.safe
Script.CryptPad
SendTo.lnk
Settings.lnk
CryptPad.exe
CryptPad.png
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 ();
		}

		private void Replace_Load (object sender, EventArgs e)
		{
			Crypt_Pad = (CryptPad) this.Owner;
			this.textFind.Text = Crypt_Pad.find_string;
			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.find_string = this.textFind.Text;
			Crypt_Pad.replace_string = this.textReplace.Text;
			Crypt_Pad.checkBoxMatchCase = this.checkBoxMatchCase.Checked;
			Crypt_Pad.checkBoxRegEx (this.checkBoxRegEx.Checked);
			Crypt_Pad.FindNext (true);
			Crypt_Pad.Position (this);
		}

		private void buttonReplace_Click (object sender, EventArgs e)
		{
			Crypt_Pad.find_string = this.textFind.Text;
			Crypt_Pad.replace_string = this.textReplace.Text;
			Crypt_Pad.checkBoxMatchCase = this.checkBoxMatchCase.Checked;
			Crypt_Pad.checkBoxRegEx (this.checkBoxRegEx.Checked);

			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 str = Crypt_Pad.find_regex.Replace (Crypt_Pad.txtBody.SelectedText, str_replace);
					Crypt_Pad.txtBody.Paste (str);
					Crypt_Pad.txtBody.SelectionStart -= str.Length;
					Crypt_Pad.txtBody.SelectionLength = str.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.find_string = this.textFind.Text;
			Crypt_Pad.replace_string = this.textReplace.Text;
			Crypt_Pad.checkBoxMatchCase = this.checkBoxMatchCase.Checked;
			Crypt_Pad.checkBoxRegEx (this.checkBoxRegEx.Checked);

			// first we determine if there are zero, one, or more matches
			string str = 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 (str);
				if (!match.Success)
				{
					Crypt_Pad.RestoreCaretPosition ();
					Crypt_Pad.NoFind ();
					return;
				}
				int index = match.Groups[0].Index;
				int length = match.Groups[0].Value.Length;
				str = match.Groups[0].Value;
				match = match.NextMatch ();
				if (!match.Success)
				{
					str = Crypt_Pad.find_regex.Replace (str, str_replace);
					ReplaceSelect (index, length, str);
					return;
				}
				str = Crypt_Pad.find_regex.Replace (Crypt_Pad.txtBody.Text, str_replace);
				Crypt_Pad.txtBody.SelectAll ();
				Crypt_Pad.txtBody.Paste (str);
				Crypt_Pad.RestoreCaretPosition ();
				return;
			}
			// simple string replace
			string str_find = Crypt_Pad.Unescape (Crypt_Pad.find_string);
			if (checkBoxMatchCase.Checked)
			{
				i = str.IndexOf (str_find);
				j = str.LastIndexOf (str_find);
			}
			else
			{
				i = str.IndexOf (str_find, StringComparison.CurrentCultureIgnoreCase);
				j = str.LastIndexOf (str_find, StringComparison.CurrentCultureIgnoreCase);
			}
			// zero matches so give the error Notepad doesn't
			if (0 > i)
			{
				Crypt_Pad.RestoreCaretPosition ();
				Crypt_Pad.NoFind ();
				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
				str = str.Replace (str_find, str_replace);
			}
			else
			{
				// harder to do our own
				j = str_find.Length;

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

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

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

		private void contextMenuStripFind_Opening (object sender, CancelEventArgs e)
		{
			if (textFind.CanUndo)
				toolStripMenuUndo.Enabled = true;
			else
				toolStripMenuUndo.Enabled = false;
			if (Clipboard.ContainsText ())
				toolStripMenuPaste.Enabled = true;
			else
				toolStripMenuPaste.Enabled = false;
			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 str = textFind.SelectedText;
			str = Crypt_Pad.Unescape (str);
			Clipboard.SetText (str);
			textFind.Paste ("");
		}

		private void toolStripMenuCopy_Click (object sender, EventArgs e)
		{
			string str = textFind.SelectedText;
			str = Crypt_Pad.Unescape (str);
			Clipboard.SetText (str);
		}

		private void toolStripMenuPaste_Click (object sender, EventArgs e)
		{
			string str = Clipboard.GetText ();
			str = Crypt_Pad.Escape (str);
			textFind.Paste (str);
		}

		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)
		{
			if (textReplace.CanUndo)
				toolStripReplaceMenuUndo.Enabled = true;
			else
				toolStripReplaceMenuUndo.Enabled = false;
			if (Clipboard.ContainsText ())
				toolStripReplaceMenuPaste.Enabled = true;
			else
				toolStripReplaceMenuPaste.Enabled = false;
			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)
		{
			textFind.Undo ();
		}

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

		private void toolStripReplaceMenuCopy_Click (object sender, EventArgs e)
		{
			string str = textReplace.SelectedText;
			str = Crypt_Pad.Unescape (str);
			Clipboard.SetText (str);
		}

		private void toolStripReplaceMenuPaste_Click (object sender, EventArgs e)
		{
			string str = Clipboard.GetText ();
			str = Crypt_Pad.Escape (str);
			textReplace.Paste (str);
		}

		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)

Share

About the Author

Frank T. Clark
Systems Engineer Three Angels Broadcasting Network
United States United States
I am a 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 employed as Information Technology Staff in the religious satellite broadcasting industry.

| Advertise | Privacy | Mobile
Web01 | 2.8.141029.1 | Last Updated 28 Jul 2014
Article Copyright 2013 by Frank T. Clark
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid