-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind.cs
179 lines (165 loc) · 5.35 KB
/
Find.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace Lang
{
public partial class Find : UserControl
{
private int lastFindPos;
private string lastSearchQuery;
public Find()
{
InitializeComponent();
lastFindPos = -1;
lastSearchQuery = "";
this.Width = 197;
this.Height = 27;
}
private void Query_TextChanged(object sender, EventArgs e)
{
if (DockTo != null)
{
if (lastFindPos != -1)
{
DockTo.SelectionStart = lastFindPos;
DockTo.SelectionLength = lastSearchQuery.Length;
DockTo.SelectionColor = SystemColors.WindowText;
}
int pos = DockTo.Text.IndexOf(Query.Text);
if (pos >= 0)
{
Query.ForeColor = SystemColors.WindowText;
DockTo.SelectionStart = pos;
DockTo.SelectionLength = Query.Text.Length;
DockTo.SelectionColor = Color.Red;
}
else
{
Query.ForeColor = Color.Red;
}
if (Query.Text == "")
{
Query.ForeColor = Color.Red;
}
lastFindPos = pos;
lastSearchQuery = Query.Text;
}
}
private void Prev()
{
int pos = DockTo.Text.LastIndexOf(Query.Text, DockTo.SelectionStart);
if (pos == -1)
{
pos = DockTo.Text.LastIndexOf(Query.Text);
if (pos == -1)
{
return;
}
}
if (lastFindPos != -1)
{
DockTo.SelectionStart = lastFindPos;
DockTo.SelectionLength = lastSearchQuery.Length;
DockTo.SelectionColor = SystemColors.WindowText;
}
DockTo.SelectionStart = pos;
DockTo.SelectionLength = Query.Text.Length;
DockTo.SelectionColor = Color.Red;
lastFindPos = pos;
lastSearchQuery = Query.Text;
}
private void PrevButton_Click(object sender, EventArgs e)
{
Prev();
}
private void Next()
{
int pos = DockTo.Text.IndexOf(Query.Text, DockTo.SelectionStart + DockTo.SelectionLength);
if (pos == -1)
{
pos = DockTo.Text.IndexOf(Query.Text);
if (pos == -1)
{
Query.ForeColor = Color.Red;
DockTo.SelectionColor = SystemColors.WindowText;
return;
}
}
else
{
pos += DockTo.SelectionLength + DockTo.SelectionStart;
}
if (lastFindPos != -1)
{
DockTo.SelectionStart = lastFindPos;
DockTo.SelectionLength = lastSearchQuery.Length;
DockTo.SelectionColor = SystemColors.WindowText;
}
DockTo.SelectionStart = pos;
DockTo.SelectionLength = Query.Text.Length;
DockTo.SelectionColor = Color.Red;
lastFindPos = pos;
lastSearchQuery = Query.Text;
}
private void NextButton_Click(object sender, EventArgs e)
{
Next();
}
private void Exit_Click(object sender, EventArgs e)
{
if (lastFindPos != -1)
{
DockTo.SelectionStart = lastFindPos;
DockTo.SelectionLength = lastSearchQuery.Length;
DockTo.SelectionColor = SystemColors.WindowText;
}
this.Visible = false;
DockTo.Focus();
DockTo.SelectionLength = 0;
lastFindPos = -1;
lastSearchQuery = "";
Query.Text = "";
}
public RichTextBox DockTo { get; set; }
private void ReplaceButton_Click(object sender, EventArgs e)
{
if (lastFindPos != -1 && lastFindPos == DockTo.SelectionStart && lastSearchQuery.Length == DockTo.SelectionLength && DockTo.SelectedText == lastSearchQuery)
{
DockTo.SelectionColor = SystemColors.WindowText;
DockTo.SelectedText = Replace.Text;
Next();
}
else
{
Next();
DockTo.SelectedText = Replace.Text;
}
}
private void Find_Load(object sender, EventArgs e)
{
Location = new Point(DockTo.Location.X + DockTo.Width - this.Width - 5, DockTo.Location.Y + 5);
this.Width = 197;
this.Height = 27;
}
private void Query_KeyDown(object sender, KeyEventArgs e)
{
}
private void ShowHideReplace_Click(object sender, EventArgs e)
{
if (Height == 55)
{
Height = 27;
ShowHideReplace.Text = "▼";
}
else
{
Height = 55;
ShowHideReplace.Text = "▲";
}
}
}
}