source: Common/FormFind.cs

Last change on this file was 15, checked in by chronos, 13 months ago
  • Modified: Updated files.
File size: 4.7 KB
Line 
1using System;
2using System.Drawing;
3using System.Windows.Forms;
4using Microsoft.Win32;
5
6namespace Common
7{
8 public partial class FormFind : FormEx
9 {
10 public RichTextBoxEx richTextBox;
11 public string regSubKey;
12
13 public FormFind()
14 {
15 InitializeComponent();
16 regSubKey = "Find";
17 }
18
19 public int FindText(string text, int start, int selectionLength, bool reverse = false)
20 {
21 int result = -1;
22 int end = richTextBox.Text.Length;
23
24 richTextBox.UndoUnknownActions();
25
26 if ((start >= 0) && (end >= start))
27 {
28 {
29 RichTextBoxFinds options = RichTextBoxFinds.None;
30 if (checkBoxMatchCase.Checked) options |= RichTextBoxFinds.MatchCase;
31 if (checkBoxMatchWholeWordOnly.Checked) options |= RichTextBoxFinds.WholeWord;
32 if (reverse)
33 {
34 end = start;
35 start = 0;
36 options |= RichTextBoxFinds.Reverse;
37 } else
38 {
39 if (selectionLength > 0) start = start + selectionLength;
40 }
41 result = richTextBox.Find(text, start, end, options);
42 }
43 }
44 return result;
45 }
46
47 private void textBoxWhat_TextChanged(object sender, EventArgs e)
48 {
49 UpdateInterface();
50 }
51
52 private void SearchText(bool reverse = false)
53 {
54 comboBoxWhat.UpdateHistory();
55
56 while (true)
57 {
58 int newPos = FindText(comboBoxWhat.Text.Trim(), richTextBox.SelectionStart, richTextBox.SelectionLength, reverse);
59 if (newPos >= 0)
60 {
61 richTextBox.SelectionColor = Color.Red;
62 richTextBox.Select(newPos, comboBoxWhat.Text.Length);
63 richTextBox.ScrollToCaret();
64 }
65 else
66 {
67 if (MessageBox.Show("No other occurence found. Continue from start?", "Text search", MessageBoxButtons.YesNo) == DialogResult.Yes)
68 {
69 if (reverse) richTextBox.SelectionStart = richTextBox.Text.Length;
70 else richTextBox.SelectionStart = 0;
71 continue;
72 }
73 }
74 break;
75 }
76 }
77
78 private void buttonFindNext_Click(object sender, EventArgs e)
79 {
80 SearchText();
81 }
82
83 private void buttonFindPrevious_Click(object sender, EventArgs e)
84 {
85 SearchText(true);
86 }
87
88 private void FormFind_Load(object sender, EventArgs e)
89 {
90 comboBoxWhat.Focus();
91 comboBoxWhat.RegSubKey = regSubKey + "\\History";
92 LoadFromRegistry();
93 UpdateInterface();
94 }
95
96 private void buttonCancel_Click(object sender, EventArgs e)
97 {
98 Close();
99 }
100
101 private void FormFind_FormClosing(object sender, FormClosingEventArgs e)
102 {
103 SaveToRegistry();
104 richTextBox.FormFind = null;
105 }
106
107 private void textBoxWhat_KeyPress(object sender, KeyPressEventArgs e)
108 {
109 if (e.KeyChar == 13)
110 {
111 buttonFindNext.PerformClick();
112 }
113 }
114
115 private void UpdateInterface()
116 {
117 buttonFindNext.Enabled = comboBoxWhat.Text.Length > 0;
118 buttonFindPrevious.Enabled = comboBoxWhat.Text.Length > 0;
119 }
120
121 public void LoadFromRegistry()
122 {
123 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey);
124 if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey);
125
126 checkBoxMatchWholeWordOnly.Checked = regKey.GetValueBool("MatchWholeWordOnly", false);
127 checkBoxMatchCase.Checked = regKey.GetValueBool("MatchCase", false);
128
129 comboBoxWhat.LoadFromRegistry();
130 }
131
132 public void SaveToRegistry()
133 {
134 RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey, true);
135 if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey);
136
137 regKey.SetValue("MatchWholeWordOnly", checkBoxMatchWholeWordOnly.Checked);
138 regKey.SetValue("MatchCase", checkBoxMatchCase.Checked);
139
140 comboBoxWhat.SaveToRegistry();
141 }
142 }
143}
Note: See TracBrowser for help on using the repository browser.