1 | using System;
|
---|
2 | using System.Drawing;
|
---|
3 | using System.Windows.Forms;
|
---|
4 | using Microsoft.Win32;
|
---|
5 |
|
---|
6 | namespace Common
|
---|
7 | {
|
---|
8 | public partial class FormFind : Form
|
---|
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 | Theme.UseTheme(this);
|
---|
91 | DpiScaling.Apply(this);
|
---|
92 | Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
|
---|
93 | new FormDimensions().Load(this, Owner);
|
---|
94 | comboBoxWhat.Focus();
|
---|
95 | comboBoxWhat.regSubKey = regSubKey + "\\History";
|
---|
96 | LoadFromRegistry();
|
---|
97 | UpdateInterface();
|
---|
98 | }
|
---|
99 |
|
---|
100 | private void buttonCancel_Click(object sender, EventArgs e)
|
---|
101 | {
|
---|
102 | Close();
|
---|
103 | }
|
---|
104 |
|
---|
105 | private void FormFind_FormClosing(object sender, FormClosingEventArgs e)
|
---|
106 | {
|
---|
107 | SaveToRegistry();
|
---|
108 | richTextBox.FormFind = null;
|
---|
109 | new FormDimensions().Save(this, Owner);
|
---|
110 | }
|
---|
111 |
|
---|
112 | private void textBoxWhat_KeyPress(object sender, KeyPressEventArgs e)
|
---|
113 | {
|
---|
114 | if (e.KeyChar == 13)
|
---|
115 | {
|
---|
116 | buttonFindNext.PerformClick();
|
---|
117 | } else
|
---|
118 | if (e.KeyChar == 27)
|
---|
119 | {
|
---|
120 | Close();
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | private void UpdateInterface()
|
---|
125 | {
|
---|
126 | buttonFindNext.Enabled = comboBoxWhat.Text.Length > 0;
|
---|
127 | buttonFindPrevious.Enabled = comboBoxWhat.Text.Length > 0;
|
---|
128 | }
|
---|
129 |
|
---|
130 | public void LoadFromRegistry()
|
---|
131 | {
|
---|
132 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey);
|
---|
133 | if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey);
|
---|
134 |
|
---|
135 | checkBoxMatchWholeWordOnly.Checked = regKey.GetValueBool("MatchWholeWordOnly", false);
|
---|
136 | checkBoxMatchCase.Checked = regKey.GetValueBool("MatchCase", false);
|
---|
137 |
|
---|
138 | comboBoxWhat.LoadFromRegistry();
|
---|
139 | }
|
---|
140 |
|
---|
141 | public void SaveToRegistry()
|
---|
142 | {
|
---|
143 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey, true);
|
---|
144 | if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey);
|
---|
145 |
|
---|
146 |
|
---|
147 | regKey.SetValue("MatchWholeWordOnly", checkBoxMatchWholeWordOnly.Checked);
|
---|
148 | regKey.SetValue("MatchCase", checkBoxMatchCase.Checked);
|
---|
149 |
|
---|
150 | comboBoxWhat.SaveToRegistry();
|
---|
151 | }
|
---|
152 |
|
---|
153 | private void comboBoxWhat_SelectedIndexChanged(object sender, EventArgs e)
|
---|
154 | {
|
---|
155 |
|
---|
156 | }
|
---|
157 |
|
---|
158 | private void label1_Click(object sender, EventArgs e)
|
---|
159 | {
|
---|
160 |
|
---|
161 | }
|
---|
162 |
|
---|
163 | private void checkBoxMatchWholeWordOnly_CheckedChanged(object sender, EventArgs e)
|
---|
164 | {
|
---|
165 |
|
---|
166 | }
|
---|
167 |
|
---|
168 | private void checkBoxMatchCase_CheckedChanged(object sender, EventArgs e)
|
---|
169 | {
|
---|
170 |
|
---|
171 | }
|
---|
172 | }
|
---|
173 | } |
---|