using System; using System.Drawing; using System.Windows.Forms; using Microsoft.Win32; namespace Common { public partial class FormFind : Form { public RichTextBoxEx richTextBox; public string regSubKey; public FormFind() { InitializeComponent(); regSubKey = "Find"; } public int FindText(string text, int start, int selectionLength, bool reverse = false) { int result = -1; int end = richTextBox.Text.Length; richTextBox.UndoUnknownActions(); if ((start >= 0) && (end >= start)) { { RichTextBoxFinds options = RichTextBoxFinds.None; if (checkBoxMatchCase.Checked) options |= RichTextBoxFinds.MatchCase; if (checkBoxMatchWholeWordOnly.Checked) options |= RichTextBoxFinds.WholeWord; if (reverse) { end = start; start = 0; options |= RichTextBoxFinds.Reverse; } else { if (selectionLength > 0) start = start + selectionLength; } result = richTextBox.Find(text, start, end, options); } } return result; } private void textBoxWhat_TextChanged(object sender, EventArgs e) { UpdateInterface(); } private void SearchText(bool reverse = false) { comboBoxWhat.UpdateHistory(); while (true) { int newPos = FindText(comboBoxWhat.Text.Trim(), richTextBox.SelectionStart, richTextBox.SelectionLength, reverse); if (newPos >= 0) { richTextBox.SelectionColor = Color.Red; richTextBox.Select(newPos, comboBoxWhat.Text.Length); richTextBox.ScrollToCaret(); } else { if (MessageBox.Show("No other occurence found. Continue from start?", "Text search", MessageBoxButtons.YesNo) == DialogResult.Yes) { if (reverse) richTextBox.SelectionStart = richTextBox.Text.Length; else richTextBox.SelectionStart = 0; continue; } } break; } } private void buttonFindNext_Click(object sender, EventArgs e) { SearchText(); } private void buttonFindPrevious_Click(object sender, EventArgs e) { SearchText(true); } private void FormFind_Load(object sender, EventArgs e) { Theme.UseTheme(this); DpiScaling.Apply(this); Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath); new FormDimensions().Load(this, Owner); comboBoxWhat.Focus(); comboBoxWhat.regSubKey = regSubKey + "\\History"; LoadFromRegistry(); UpdateInterface(); } private void buttonCancel_Click(object sender, EventArgs e) { Close(); } private void FormFind_FormClosing(object sender, FormClosingEventArgs e) { SaveToRegistry(); richTextBox.FormFind = null; new FormDimensions().Save(this, Owner); } private void textBoxWhat_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { buttonFindNext.PerformClick(); } else if (e.KeyChar == 27) { Close(); } } private void UpdateInterface() { buttonFindNext.Enabled = comboBoxWhat.Text.Length > 0; buttonFindPrevious.Enabled = comboBoxWhat.Text.Length > 0; } public void LoadFromRegistry() { RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey); if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey); checkBoxMatchWholeWordOnly.Checked = regKey.GetValueBool("MatchWholeWordOnly", false); checkBoxMatchCase.Checked = regKey.GetValueBool("MatchCase", false); comboBoxWhat.LoadFromRegistry(); } public void SaveToRegistry() { RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey, true); if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey); regKey.SetValue("MatchWholeWordOnly", checkBoxMatchWholeWordOnly.Checked); regKey.SetValue("MatchCase", checkBoxMatchCase.Checked); comboBoxWhat.SaveToRegistry(); } private void comboBoxWhat_SelectedIndexChanged(object sender, EventArgs e) { } private void label1_Click(object sender, EventArgs e) { } private void checkBoxMatchWholeWordOnly_CheckedChanged(object sender, EventArgs e) { } private void checkBoxMatchCase_CheckedChanged(object sender, EventArgs e) { } } }