-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReplaceBox.cs
381 lines (332 loc) · 13.2 KB
/
ReplaceBox.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class ReplaceBox : Form
{
public CardSearchParams searchParams; // this needs to be defined outside because we're using Show() to display this dialog, and when it's closed it purges it from memory...
Form1 form1;
bool bSetNumericAcceptors = false;
bool bInComboBoxMode = false;
bool bStringMatchWholeLastState = false;
public ReplaceBox(CardSearchParams inSearchParams, Form1 inForm1)
{
InitializeComponent();
//searchParams = new CardSearchParams();
searchParams = inSearchParams;
form1 = inForm1;
}
void SaveCheckboxes()
{
searchParams.bSearchDescription = radioButtonDescription.Checked;
searchParams.bSearchName = radioButtonName.Checked;
searchParams.bSearchKind = radioButtonKind.Checked;
searchParams.bSearchLevel = radioButtonLevel.Checked;
searchParams.bSearchATK = radioButtonATK.Checked;
searchParams.bSearchDEF = radioButtonDEF.Checked;
searchParams.bSearchType = radioButtonType.Checked;
searchParams.bSearchAttr = radioButtonAttr.Checked;
searchParams.bSearchIcon = radioButtonIcon.Checked;
searchParams.bSearchRarity = radioButtonRarity.Checked;
searchParams.bSearchPassword = radioButtonPassword.Checked;
searchParams.bSearchCardExists = radioButtonCardExists.Checked;
searchParams.bMatchWhole = checkBoxMatch.Checked;
searchParams.bMatchCase = checkBoxCase.Checked;
}
void LoadCheckboxes()
{
radioButtonDescription.Checked = searchParams.bSearchDescription;
radioButtonName.Checked = searchParams.bSearchName;
radioButtonKind.Checked = searchParams.bSearchKind;
radioButtonLevel.Checked = searchParams.bSearchLevel;
radioButtonATK.Checked = searchParams.bSearchATK;
radioButtonDEF.Checked = searchParams.bSearchDEF;
radioButtonType.Checked = searchParams.bSearchType;
radioButtonAttr.Checked = searchParams.bSearchAttr;
radioButtonIcon.Checked = searchParams.bSearchIcon;
radioButtonRarity.Checked = searchParams.bSearchRarity;
radioButtonPassword.Checked = searchParams.bSearchPassword;
radioButtonCardExists.Checked = searchParams.bSearchCardExists;
checkBoxMatch.Checked = searchParams.bMatchWhole;
checkBoxCase.Checked = searchParams.bMatchCase;
}
bool IsANumericBoxTicked()
{
return radioButtonLevel.Checked
|| radioButtonATK.Checked
|| radioButtonDEF.Checked
|| radioButtonPassword.Checked;
}
void SetSearchContext() // since there can only be one for replace action, we can define it here
{
if (radioButtonName.Checked)
searchParams.SearchContext = CardProps.Name;
if (radioButtonDescription.Checked)
searchParams.SearchContext = CardProps.Description;
if (radioButtonKind.Checked)
searchParams.SearchContext = CardProps.Kind;
if (radioButtonLevel.Checked)
searchParams.SearchContext = CardProps.Level;
if (radioButtonATK.Checked)
searchParams.SearchContext = CardProps.ATK;
if (radioButtonDEF.Checked)
searchParams.SearchContext = CardProps.DEF;
if (radioButtonType.Checked)
searchParams.SearchContext = CardProps.Type;
if (radioButtonAttr.Checked)
searchParams.SearchContext = CardProps.Attr;
if (radioButtonIcon.Checked)
searchParams.SearchContext = CardProps.Icon;
if (radioButtonRarity.Checked)
searchParams.SearchContext = CardProps.Rarity;
if (radioButtonPassword.Checked)
searchParams.SearchContext = CardProps.Password;
if (radioButtonCardExists.Checked)
searchParams.SearchContext = CardProps.CardExists;
}
private bool Int32TextAcceptor(string oldText, string newText, string input, int offset, int length)
{
int value = 0;
return Int32.TryParse(newText, out value); ;
}
void SetAcceptors()
{
fastTextBoxFind.TextAcceptor = Int32TextAcceptor;
fastTextBoxReplace.TextAcceptor = Int32TextAcceptor;
}
void UnSetAcceptors()
{
fastTextBoxFind.TextAcceptor = null;
fastTextBoxReplace.TextAcceptor = null;
}
private void ReplaceBox_Load(object sender, EventArgs e)
{
fastTextBoxFind.Text = searchParams.SearchString;
fastTextBoxReplace.Text = searchParams.ReplaceString;
LoadCheckboxes();
// start pos at the corner of the parent window
Location = new Point(Owner.Location.X + 40, Owner.Location.Y + 40);
}
private void button2_Click(object sender, EventArgs e)
{
SaveCheckboxes();
SetSearchContext();
DialogResult = DialogResult.Cancel;
Close();
}
void CheckFindString()
{
string str;
if (bInComboBoxMode)
str = comboBoxFind.Text;
else
str = fastTextBoxFind.Text;
if ((radioButtonName.Checked || radioButtonDescription.Checked) && bSetNumericAcceptors)
{
UnSetAcceptors();
bSetNumericAcceptors = false;
}
if (IsANumericBoxTicked() && !bSetNumericAcceptors)
{
SetAcceptors();
if (!string.IsNullOrEmpty(fastTextBoxFind.Text))
{
if (!Int32TextAcceptor(null, fastTextBoxFind.Text, null, 0, 0))
fastTextBoxFind.Text = "";
}
bSetNumericAcceptors = true;
}
if (string.IsNullOrEmpty(str))
{
button1.Enabled = false;
buttonReplace.Enabled = false;
buttonReplaceAll.Enabled = false;
}
else
{
button1.Enabled = true;
buttonReplace.Enabled = true;
buttonReplaceAll.Enabled = true;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
CheckFindString();
// CheckReplaceString();
}
private void button1_Click(object sender, EventArgs e)
{
SaveCheckboxes();
SetSearchContext();
DialogResult = DialogResult.OK;
form1.InitiateCardSearch(searchParams);
}
private void buttonReplace_Click(object sender, EventArgs e)
{
SaveCheckboxes();
SetSearchContext();
DialogResult = DialogResult.OK;
form1.InitiateReplacing(searchParams);
}
private void buttonReplaceAll_Click(object sender, EventArgs e)
{
SaveCheckboxes();
SetSearchContext();
DialogResult = DialogResult.OK;
form1.InitiateReplaceAll(searchParams);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
searchParams.SearchString = fastTextBoxFind.Text;
}
private void ReplaceBox_Activated(object sender, EventArgs e)
{
fastTextBoxFind.Focus();
}
private void fastTextBoxReplace_TextChanged(object sender, EventArgs e)
{
searchParams.ReplaceString = fastTextBoxReplace.Text;
}
private void SwitchToTextBox(object sender, EventArgs e) // defined in events for text inputs...
{
if ((radioButtonDescription.Checked || radioButtonName.Checked || IsANumericBoxTicked()) && bInComboBoxMode)
{
comboBoxFind.Visible = false;
comboBoxReplace.Visible = false;
fastTextBoxFind.Visible = true;
fastTextBoxReplace.Visible = true;
checkBoxMatch.Checked = bStringMatchWholeLastState;
bInComboBoxMode = false;
fastTextBoxFind.Text = "";
fastTextBoxReplace.Text = "";
searchParams.SearchString = "";
searchParams.ReplaceString = "";
}
if (IsANumericBoxTicked())
{
checkBoxCase.Enabled = false;
fastTextBoxFind.Text = "";
fastTextBoxReplace.Text = "";
searchParams.SearchString = "";
searchParams.ReplaceString = "";
}
else
{
checkBoxCase.Enabled = true;
checkBoxMatch.Enabled = true;
}
}
private void SwitchToComboBox()
{
fastTextBoxFind.Visible = false;
fastTextBoxReplace.Visible = false;
checkBoxCase.Enabled = false;
checkBoxMatch.Enabled = false;
bStringMatchWholeLastState = checkBoxMatch.Checked;
checkBoxMatch.Checked = true;
comboBoxFind.Visible = true;
comboBoxReplace.Visible = true;
bInComboBoxMode = true;
comboBoxFind.Text = "";
comboBoxReplace.Text = "";
searchParams.SearchString = "";
searchParams.ReplaceString = "";
}
private void GenerateComboBox(Type EnumType)
{
var enumcount = Enum.GetNames(EnumType).Length;
comboBoxFind.Items.Clear();
comboBoxReplace.Items.Clear();
for (int i = 0; i < enumcount; i++)
{
comboBoxFind.Items.Add(TypeDescriptor.GetConverter(EnumType).ConvertToString(i));
comboBoxReplace.Items.Add(TypeDescriptor.GetConverter(EnumType).ConvertToString(i));
}
comboBoxFind.SelectedIndex = 0;
comboBoxReplace.SelectedIndex = 0;
searchParams.SearchComboBoxIndex = 0;
searchParams.ReplaceComboBoxIndex = 0;
}
private void radioButtonKind_CheckedChanged(object sender, EventArgs e)
{
if (radioButtonKind.Checked)
{
SwitchToComboBox();
GenerateComboBox(typeof(CardKinds));
}
}
private void radioButtonType_CheckedChanged(object sender, EventArgs e)
{
if (radioButtonType.Checked)
{
SwitchToComboBox();
GenerateComboBox(typeof(CardTypes));
}
}
private void radioButtonAttr_CheckedChanged(object sender, EventArgs e)
{
if (radioButtonAttr.Checked)
{
SwitchToComboBox();
GenerateComboBox(typeof(CardAttributes));
}
}
private void radioButtonIcon_CheckedChanged(object sender, EventArgs e)
{
if (radioButtonIcon.Checked)
{
SwitchToComboBox();
GenerateComboBox(typeof(CardIcons));
}
}
private void radioButtonRarity_CheckedChanged(object sender, EventArgs e)
{
if (radioButtonRarity.Checked)
{
SwitchToComboBox();
GenerateComboBox(typeof(CardRarity));
}
}
private void radioButtonCardExists_CheckedChanged(object sender, EventArgs e)
{
if (radioButtonCardExists.Checked)
{
SwitchToComboBox();
comboBoxFind.Items.Clear();
comboBoxReplace.Items.Clear();
comboBoxFind.Items.Add("False");
comboBoxFind.Items.Add("True");
comboBoxReplace.Items.Add("False");
comboBoxReplace.Items.Add("True");
comboBoxFind.SelectedIndex = 0;
comboBoxReplace.SelectedIndex = 0;
searchParams.SearchComboBoxIndex = 0;
searchParams.ReplaceComboBoxIndex = 0;
}
}
private void comboBoxFind_TextChanged(object sender, EventArgs e)
{
searchParams.SearchString = comboBoxFind.Text;
}
private void comboBoxReplace_TextChanged(object sender, EventArgs e)
{
searchParams.ReplaceString = comboBoxReplace.Text;
}
private void comboBoxFind_SelectedIndexChanged(object sender, EventArgs e)
{
searchParams.SearchComboBoxIndex = comboBoxFind.SelectedIndex;
}
private void comboBoxReplace_SelectedIndexChanged(object sender, EventArgs e)
{
searchParams.ReplaceComboBoxIndex = comboBoxReplace.SelectedIndex;
}
}
}