-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSearchManager.cs
104 lines (90 loc) · 3.63 KB
/
SearchManager.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
using CommonLibrary.CodeSystem.Controls;
using ScintillaNET;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CommonLibrary.CodeSystem
{
internal class SearchManager
{
private static int m_nLastSearchIndex = 0;
private static bool m_bIsNext = true;
public static CodeEditPage TextArea { get; set; } = null;
public static TextBox SearchBox { get; set; } = null;
public static string LastSearch { get; private set; } = string.Empty;
public static void Find(bool bIsNext)
{
if (TextArea == null || SearchBox == null)
return;
if (!LastSearch.Equals(SearchBox.Text, StringComparison.OrdinalIgnoreCase))
{
LastSearch = SearchBox.Text;
m_nLastSearchIndex = 0;
}
if (LastSearch.Length > 0)
{
TextArea.SearchFlags = SearchFlags.None;
int nTextLength = TextArea.TextLength;
if (!bIsNext.Equals(m_bIsNext))
{
m_bIsNext = bIsNext;
m_nLastSearchIndex = nTextLength - m_nLastSearchIndex + LastSearch.Length;
}
if (bIsNext)
{
//设置搜索范围
TextArea.TargetStart = m_nLastSearchIndex;
TextArea.TargetEnd = nTextLength;
if (TextArea.SearchInTarget(LastSearch) == -1)
{
m_nLastSearchIndex = 0;
TextArea.TargetStart = m_nLastSearchIndex;
TextArea.TargetEnd = nTextLength;
if (TextArea.SearchInTarget(LastSearch) == -1)
{
TextArea.ClearSelections();
return;
}
}
m_nLastSearchIndex = TextArea.TargetEnd;
}
else
{
//反转内容
string strText = new string(TextArea.Text.Reverse().ToArray());
string strSearch = new string(LastSearch.Reverse().ToArray());
int nIndex = strText.IndexOf(strSearch, m_nLastSearchIndex, StringComparison.OrdinalIgnoreCase);
if (nIndex == -1)
{
m_nLastSearchIndex = 0;
nIndex = strText.IndexOf(strSearch, m_nLastSearchIndex, StringComparison.OrdinalIgnoreCase);
if (nIndex == -1)
{
TextArea.ClearSelections();
return;
}
else
{
//反转索引
TextArea.TargetStart = nTextLength - (nIndex + strSearch.Length);
TextArea.TargetEnd = nTextLength - nIndex;
}
}
else
{
TextArea.TargetStart = nTextLength - (nIndex + strSearch.Length);
TextArea.TargetEnd = nTextLength - nIndex;
}
m_nLastSearchIndex = nIndex + strSearch.Length;
}
//设置选择内容
TextArea.SetSelection(TextArea.TargetEnd, TextArea.TargetStart);
TextArea.ScrollCaret();
}
SearchBox.Focus();
}
}
}