forked from PavelTorgashov/FastColoredTextBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindForm.cs
More file actions
129 lines (118 loc) · 3.51 KB
/
Copy pathFindForm.cs
File metadata and controls
129 lines (118 loc) · 3.51 KB
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
using System;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace FastColoredTextBoxNS
{
public partial class FindForm : Form
{
bool firstSearch = true;
Place startPlace;
FastColoredTextBox tb;
public FindForm(FastColoredTextBox tb)
{
InitializeComponent();
this.tb = tb;
}
private void btClose_Click(object sender, EventArgs e)
{
Close();
}
private void btFindNext_Click(object sender, EventArgs e)
{
FindNext(tbFind.Text);
}
public virtual void FindNext(string pattern)
{
try
{
RegexOptions opt = cbMatchCase.Checked ? RegexOptions.None : RegexOptions.IgnoreCase;
if (!cbRegex.Checked)
pattern = Regex.Escape(pattern);
if (cbWholeWord.Checked)
pattern = "\\b" + pattern + "\\b";
//
Range range = tb.Selection.Clone();
range.Normalize();
//
if (firstSearch)
{
startPlace = range.Start;
firstSearch = false;
}
//
range.Start = range.End;
if (range.Start >= startPlace)
range.End = new Place(tb.GetLineLength(tb.LinesCount - 1), tb.LinesCount - 1);
else
range.End = startPlace;
//
foreach (var r in range.GetRangesByLines(pattern, opt))
{
tb.Selection = r;
tb.DoSelectionVisible();
tb.Invalidate();
return;
}
//
if (range.Start >= startPlace && startPlace > Place.Empty)
{
tb.Selection.Start = new Place(0, 0);
FindNext(pattern);
return;
}
MessageBox.Show("Not found");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void tbFind_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
btFindNext.PerformClick();
e.Handled = true;
return;
}
if (e.KeyChar == '\x1b')
{
Hide();
e.Handled = true;
return;
}
}
private void FindForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
Hide();
}
this.tb.Focus();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Escape)
{
this.Close();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
protected override void OnActivated(EventArgs e)
{
tbFind.Focus();
ResetSerach();
}
void ResetSerach()
{
firstSearch = true;
}
private void cbMatchCase_CheckedChanged(object sender, EventArgs e)
{
ResetSerach();
}
}
}