-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFinder.cs
180 lines (146 loc) · 3.71 KB
/
Finder.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
180
namespace Menees.Windows.Forms
{
#region Using Directives
using System;
using System.Windows.Forms;
using Menees.Windows.Forms;
#endregion
/// <summary>
/// Used as a base class for a generic "find" operation that supports FindNext and FindPrevious.
/// </summary>
public abstract class Finder
{
#region Private Data Members
// Store a reference to FindData so an external FindData
// instance can be shared with multiple Finder instances.
private FindData? findData;
#endregion
#region Public Properties
/// <summary>
/// The find data.
/// </summary>
public FindData Data
{
get
{
if (this.findData == null)
{
this.findData = new FindData();
}
return this.findData;
}
set
{
this.findData = value;
}
}
#endregion
#region Public Methods
/// <summary>
/// Starts a find operation using the specified find data.
/// </summary>
/// <param name="owner">The dialog owner.</param>
/// <param name="findData">The data to find.</param>
/// <param name="findMode">Whether to find next, previous, or display a dialog.</param>
/// <returns>True if the find text was found and selected. False otherwise.</returns>
public bool Find(IWin32Window owner, FindData findData, FindMode findMode)
{
this.Data = findData;
// Use this.Data instead of findData in case the user passed in null.
using (IFindDialog dlg = this.Data.CreateFindDialog())
{
bool result;
switch (findMode)
{
case FindMode.ShowDialog:
result = this.Find(owner, dlg);
break;
case FindMode.FindPrevious:
result = this.FindPrevious(owner, dlg);
break;
default:
result = this.FindNext(owner, dlg);
break;
}
return result;
}
}
#endregion
#region Protected Overridable Methods
/// <summary>
/// Called to execute the dialog. This allows you to do custom actions before
/// and after the dialog is executed.
/// </summary>
/// <param name="findDialog">The dialog to display.</param>
/// <param name="owner">The dialog owner.</param>
/// <param name="findData">The find data.</param>
/// <returns>True if OK was pressed.</returns>
protected virtual bool OnDialogExecute(IFindDialog findDialog, IWin32Window owner, FindData findData)
{
bool result = findDialog.Execute(owner, findData);
return result;
}
/// <summary>
/// Override to handle find next.
/// </summary>
protected abstract bool OnFindNext();
/// <summary>
/// Override to handle find previous.
/// </summary>
protected abstract bool OnFindPrevious();
#endregion
#region Private Methods
private bool Find(IWin32Window owner, IFindDialog findDialog)
{
Conditions.RequireReference(findDialog, nameof(findDialog));
bool result = false;
if (this.OnDialogExecute(findDialog, owner, this.Data))
{
if (this.Data.SearchUp)
{
result = this.FindPrevious(owner, findDialog);
}
else
{
result = this.FindNext(owner, findDialog);
}
}
return result;
}
private bool FindNext(IWin32Window owner, IFindDialog findDialog)
{
bool result;
if (string.IsNullOrEmpty(this.Data.Text))
{
this.Data.SearchUp = false;
result = this.Find(owner, findDialog);
}
else
{
using (new WaitCursor(owner as Control))
{
result = this.OnFindNext();
}
}
return result;
}
private bool FindPrevious(IWin32Window owner, IFindDialog findDialog)
{
bool result;
if (string.IsNullOrEmpty(this.Data.Text))
{
this.Data.SearchUp = true;
result = this.Find(owner, findDialog);
}
else
{
using (new WaitCursor(owner as Control))
{
result = this.OnFindPrevious();
}
}
return result;
}
#endregion
}
}