namespace Metoder { public partial class Form1 : Form { public Form1() { InitializeComponent(); } // button binds private void btn_find_Click(object sender, EventArgs e) { string pattern = txtbx_find.Text; Highlight(pattern); } private void btn_replace_Click(object sender, EventArgs e) { string pattern = txtbx_find.Text; string replacement = txtbx_replace.Text; ReplaceFound(pattern, replacement); } private void btn_clear_Click(object sender, EventArgs e) { lbl_found.Visible = false; } private void btn_sort_Click(object sender, EventArgs e) { SortWords(); } // methods void UpdateFindResult(int foundCount) { lbl_found.Visible = true; string suffix; // correct grammar if (foundCount == 1) suffix = " item found"; else suffix = " items found"; lbl_found.Text = foundCount.ToString() + suffix; } Dictionary GetMatchesIndices(string pattern, string text) { Dictionary dict = new Dictionary(); if (pattern == "") return dict; // avoid errors, infinite loop int index = 0; // find and save start index and length of each occurance while ((index = text.IndexOf(pattern, index, StringComparison.OrdinalIgnoreCase)) != -1) { dict[index] = pattern.Length; index += pattern.Length; } return dict; } void Highlight(string pattern) { RichTextBox richTextBox = txtbx_text; string text = richTextBox.Text; // reset old highlight richTextBox.SelectAll(); richTextBox.SelectionBackColor = richTextBox.BackColor; int found = 0; int caret = richTextBox.SelectionStart; // set cursor to start foreach (KeyValuePair occurance in GetMatchesIndices(pattern, text)) { richTextBox.Select(occurance.Key, occurance.Value); // select pattern occurance richTextBox.SelectionBackColor = Color.LightBlue; // highlight found++; } // reset cursor richTextBox.Select(caret, 0); // update find label UpdateFindResult(found); } void ReplaceFound(string pattern, string replacement) { string text = txtbx_text.Text; Dictionary matches = GetMatchesIndices(pattern, text); if (matches.Count == 0) return; string finalString = ""; // or string.Empty int currentIndex = 0; foreach (KeyValuePair occurrence in matches) { // add text before occurrence if (occurrence.Key > currentIndex) { finalString += text.Substring(currentIndex, occurrence.Key - currentIndex); } // add replacement to result finalString += replacement; // move past occurrence currentIndex = occurrence.Key + occurrence.Value; } // add remaining text if (currentIndex < text.Length) { finalString += text.Substring(currentIndex); } txtbx_text.Text = finalString; } void SortWords() { List words = GetSortedWords(GetWords()); string finalString = ""; // add each sorted word separated by spaces foreach (string word in words) { finalString += word; finalString += " "; } txtbx_text.Text = finalString; } List GetSortedWords(List words) { words.Sort(); return words; } List GetWords() // get list of words separated by spaces { string text = txtbx_text.Text; string[] words = text.Split((char[])null, StringSplitOptions.RemoveEmptyEntries); // split by spaces return words.ToList(); } } }