WPF_CodeExtract.xaml.cs
39.2 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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
using BLL;
using Model;
using SmartScan.SetControl.WPF.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static SmartScan.SetControl.WPF.Model.NeoAlertBox;
using Button = System.Windows.Controls.Button;
using MessageBox = System.Windows.Forms.MessageBox;
using TextBlock = System.Windows.Controls.TextBlock;
using TextBox = System.Windows.Controls.TextBox;
namespace SmartScan.SetControl.WPF
{
/// <summary>
/// WPF_CodeExtract.xaml 的交互逻辑
/// </summary>
public partial class WPF_CodeExtract : Window
{
private readonly int codeID;
private readonly string codeText;
private readonly string codeType;
private readonly Dictionary<Button, ExtractSettingsPanel> matchButton = new Dictionary<Button, ExtractSettingsPanel>();
private readonly Dictionary<Button, UserControl1> userControls = new Dictionary<Button, UserControl1>();
private string[] myStringArray = null;
private MaterialCodeMatch matchShared = new MaterialCodeMatch();
private Button currentSelectedButton = null; // 跟踪当前选中的按钮
public WPF_CodeExtract(string codeText, int codeID, string codeType, List<MaterialCodeMatch> match, string[] strings)
{
InitializeComponent();
codeText = codeText.Replace("\r", "");
this.codeText = codeText.Replace("\n", "");
this.codeID = codeID;
this.codeType = codeType;
// 设置ComboBox默认选项
ChoMatchMiddleType.SelectedIndex = 0;
// 初始化匹配设置
if (match.Count > 0)
{
matchShared = match[0];
}
// 设置匹配选项
ChkMatchingStart.IsChecked = matchShared.MatchStart;
TxtMatchingStartText.Text = matchShared.StartText;
ChkMatchingEnd.IsChecked = matchShared.MatchEnd;
TxtMatchingEndText.Text = matchShared.EndText;
ChkMatchingMiddle.IsChecked = matchShared.MatchMiddle;
TxtMatchingMiddleText.Text = matchShared.MiddleText;
NudMiddleTextCount.Text = matchShared.MiddleTextCount.ToString();
ChkCaseSensitivity.IsChecked = matchShared.CaseSensitive;
ChoMatchMiddleType.SelectedIndex = matchShared.MatchMiddleType + 1;
ChkCheckCodeType.IsChecked = matchShared.CheckCodeType;
Characteristicbarcode.IsChecked = matchShared.Characteristic;
// 设置代码类型显示
CboCodeType.Text = codeType;
// 添加已有的匹配项
for (int i = 0; i < match.Count; i++)
{
AddMatch(match[i]);
}
// 如果没有匹配项,添加一个默认的
if (matchButton.Count == 0)
{
BtnAddMatch_Click(null, null);
}
else if (matchButton.Count > 0)
{
// 选中第一个按钮
KeywordButton_Click(matchButton.Keys.First(), null);
}
myStringArray = strings;
// 确保特征码选项可见
Characteristicbarcode.Visibility = Visibility.Visible;
}
public List<MaterialCodeMatch> CodeMatch { get; private set; }
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// 设置RichTextBox内容并高亮关键字
RichTxtCode.Document.Blocks.Clear();
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(new Run(codeText));
RichTxtCode.Document.Blocks.Add(paragraph);
if (myStringArray != null)
{
ColorizeText(myStringArray);
}
// 初始设置值检查匹配结果
CheckMatch();
this.SizeToContent = SizeToContent.Manual;
string YU = BLLCommon.config.Language;
LanguageWwitchover.LoadPath(FilePath.LANGUAGE_DIR);
LanguageWwitchover.LoadLanguage(YU);
LanguageWwitchover.SetLanguage(this);
}
private void ColorizeText(string[] keywords)
{
TextRange documentRange = new TextRange(RichTxtCode.Document.ContentStart, RichTxtCode.Document.ContentEnd);
documentRange.ClearAllProperties();
foreach (string keyword in keywords)
{
if (string.IsNullOrEmpty(keyword))
continue;
TextPointer current = RichTxtCode.Document.ContentStart;
while (current != null)
{
// 获取当前位置
if (current.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
string textRun = current.GetTextInRun(LogicalDirection.Forward);
int idx = textRun.IndexOf(keyword);
if (idx >= 0)
{
TextPointer startPos = current.GetPositionAtOffset(idx);
TextPointer endPos = startPos.GetPositionAtOffset(keyword.Length);
TextRange range = new TextRange(startPos, endPos);
range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Color.FromArgb(255, 240, 248, 255))); // AliceBlue
range.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
// 从关键词后面开始继续搜索
current = endPos;
}
else
{
// 找不到关键词,移动到下一个文本运行
current = current.GetNextContextPosition(LogicalDirection.Forward);
}
}
else
{
// 不是文本,移动到下一个上下文位置
current = current.GetNextContextPosition(LogicalDirection.Forward);
}
}
}
}
// 添加一个方法来保存当前设置
private void SaveCurrentMatchSettings()
{
if (currentSelectedButton != null && currentSelectedButton.Tag is MaterialCodeMatch match)
{
if (userControls.TryGetValue(currentSelectedButton, out UserControl1 userControl))
{
// 从UserControl获取当前值并保存到匹配对象
if (userControl.ChkSplitBarcode.IsChecked == true && userControl.CboSplitChar.SelectedItem != null)
{
match.MatchSplit = true;
match.SplitText = userControl.CboSplitChar.SelectedItem.ToString();
}
else
{
match.MatchSplit = false;
}
// 保存索引值
match.SplitPart = int.Parse(userControl.TxtSplitIndex.Text);
// 保存位置和长度
match.SubstringStart = int.Parse(userControl.TxtStartPosition.Text);
match.SubstringLength = userControl.ChkExtractToEnd.IsChecked == true ? -1 : int.Parse(userControl.TxtSubstringLength.Text);
// 保存其他验证选项
match.MatchISNumber = userControl.ChkMustBeNumber.IsChecked == true;
match.MatchMinLength = userControl.ChkMinLength.IsChecked == true;
match.MinLength = int.Parse(userControl.TxtMinLength.Text);
match.MatchMaxLength = userControl.ChkMaxLength.IsChecked == true;
match.MaxLength = int.Parse(userControl.TxtMaxLength.Text);
//新加属性
//kmon
//2025-6-24
match.OnlyTakeBeNumber = userControl.OnlyTakeBeNumber.IsChecked == true;
}
}
}
private void AddMatch(MaterialCodeMatch match)
{
// 创建关键字按钮
Button btn = new Button
{
Content = string.Format("[{0}]", match.Keyword),
Style = (Style)FindResource("KeywordButtonStyle"),
Tag = match,
Height = 30
};
btn.Click += KeywordButton_Click;
FlowLayoutKeywords.Children.Add(btn);
// 创建右侧对应的设置面板
ExtractSettingsPanel panel = new ExtractSettingsPanel(codeText, codeType, match);
matchButton.Add(btn, panel);
// 创建并配置UserControl1
UserControl1 userControl = new UserControl1();
LoadUserControlSettings(userControl, match);
userControl.Visibility = Visibility.Collapsed;
userControls.Add(btn, userControl);
}
private void LoadUserControlSettings(UserControl1 userControl, MaterialCodeMatch match)
{
// 配置UserControl中的控件与match对象中的值
// 设置分割相关选项
userControl.ChkSplitBarcode.IsChecked = match.MatchSplit;
// 初始化ComboBox选项
userControl.CboKeyword.Items.Clear();
foreach (var keywordString in BLLCommon.macroKey.ToArray())
{
userControl.CboKeyword.Items.Add(keywordString);
}
if (!string.IsNullOrEmpty(match.Keyword) && userControl.CboKeyword.Items.Contains(match.Keyword))
{
userControl.CboKeyword.SelectedItem = match.Keyword;
}
// 初始化分隔符选项
userControl.CboSplitChar.Items.Clear();
foreach (var splitChar in BLLCommon.CODE_SPLIT)
{
userControl.CboSplitChar.Items.Add(splitChar);
}
// 如果有分隔符设置,选择对应的选项
if (!string.IsNullOrEmpty(match.SplitText))
{
int index = userControl.CboSplitChar.Items.IndexOf(match.SplitText);
userControl.CboSplitChar.SelectedIndex = index >= 0 ? index : 0;
}
else
{
userControl.CboSplitChar.SelectedIndex = 0;
}
// 设置索引值
userControl.TxtSplitIndex.Text = match.SplitPart.ToString();
// 设置位置和长度
userControl.TxtStartPosition.Text = match.SubstringStart.ToString();
userControl.ChkExtractToEnd.IsChecked = match.SubstringLength == -1;
userControl.TxtSubstringLength.Text = (match.SubstringLength > 0) ? match.SubstringLength.ToString() : "1";
// 设置其他验证选项
userControl.ChkMustBeNumber.IsChecked = match.MatchISNumber;
userControl.ChkMinLength.IsChecked = match.MatchMinLength;
userControl.TxtMinLength.Text = match.MinLength.ToString();
userControl.ChkMaxLength.IsChecked = match.MatchMaxLength;
userControl.TxtMaxLength.Text = match.MaxLength.ToString();
//加属性
//kmon
//2025-6-24
userControl.OnlyTakeBeNumber.IsChecked = match.OnlyTakeBeNumber;
// 设置解析结果
CheckMatchForUserControl(match, userControl);
// 添加事件处理
userControl.ChkSplitBarcode.Checked += (s, e) => userControl_SettingChanged(userControl, match);
userControl.ChkSplitBarcode.Unchecked += (s, e) => userControl_SettingChanged(userControl, match);
userControl.CboSplitChar.SelectionChanged += (s, e) => userControl_SettingChanged(userControl, match);
userControl.ChkExtractToEnd.Checked += (s, e) => userControl_SettingChanged(userControl, match);
userControl.ChkExtractToEnd.Unchecked += (s, e) => userControl_SettingChanged(userControl, match);
userControl.ChkMustBeNumber.Checked += (s, e) => userControl_SettingChanged(userControl, match);
userControl.ChkMustBeNumber.Unchecked += (s, e) => userControl_SettingChanged(userControl, match);
userControl.ChkMinLength.Checked += (s, e) => userControl_SettingChanged(userControl, match);
userControl.ChkMinLength.Unchecked += (s, e) => userControl_SettingChanged(userControl, match);
userControl.ChkMaxLength.Checked += (s, e) => userControl_SettingChanged(userControl, match);
userControl.ChkMaxLength.Unchecked += (s, e) => userControl_SettingChanged(userControl, match);
//添加是否只有数字
//kmon
//2025-6-24
userControl.OnlyTakeBeNumber.Checked += (s, e) => userControl_SettingChanged(userControl, match);
userControl.OnlyTakeBeNumber.Unchecked += (s, e) => userControl_SettingChanged(userControl, match);
// 事件挂接
SetupUserControlEvents(userControl, match);
}
private void SetupUserControlEvents(UserControl1 userControl, MaterialCodeMatch match)
{
// 为UserControl中的增减按钮添加事件
userControl.DecreaseValue_Click += (sender, e) => {
Button btn = sender as Button;
string targetName = btn.Tag.ToString();
TextBlock target = userControl.FindName(targetName) as TextBlock;
if (target != null)
{
int value;
if (int.TryParse(target.Text, out value) && value > 0)
{
target.Text = (value - 1).ToString();
CheckMatchForUserControl(match, userControl);
}
}
};
userControl.IncreaseValue_Click += (sender, e) => {
Button btn = sender as Button;
string targetName = btn.Tag.ToString();
TextBlock target = userControl.FindName(targetName) as TextBlock;
if (target != null)
{
int value;
if (int.TryParse(target.Text, out value))
{
target.Text = (value + 1).ToString();
CheckMatchForUserControl(match, userControl);
}
}
};
// 关键字选择变更事件
userControl.CboKeyword.SelectionChanged += (sender, e) => {
if (currentSelectedButton != null && currentSelectedButton.Tag is MaterialCodeMatch currentMatch && userControl.CboKeyword.SelectedItem != null)
{
string selectedKeyword = userControl.CboKeyword.SelectedItem.ToString();
currentMatch.Keyword = selectedKeyword;
currentSelectedButton.Content = $"[{selectedKeyword}]";
CheckMatchForUserControl(currentMatch, userControl);
}
};
}
private void userControl_SettingChanged(UserControl1 userControl, MaterialCodeMatch match)
{
CheckMatchForUserControl(match, userControl);
}
private void KeywordButton_Click(object sender, RoutedEventArgs e)
{
Button clickedButton = sender as Button;
if (clickedButton == null) return;
// 在切换按钮前保存当前按钮的设置
SaveCurrentMatchSettings();
// 更新当前选中按钮
currentSelectedButton = clickedButton;
// 更新视觉选中状态
UpdateButtonSelectionState();
// 获取关联的匹配设置
MaterialCodeMatch match = clickedButton.Tag as MaterialCodeMatch;
if (match != null)
{
// 更新Frame中的UserControl
UpdateFrameContent(clickedButton);
// 触发匹配检查
CheckMatch();
}
}
private void UpdateFrameContent(Button clickedButton)
{
if (userControls.TryGetValue(clickedButton, out UserControl1 userControl))
{
//aa.NavigationService?.RemoveBackEntry();
// 清空Frame内容
aa.Content = null;
// 将对应的UserControl设为可见并添加到Frame
userControl.Visibility = Visibility.Visible;
aa.Content = userControl;
// 更新UserControl的控件状态
UpdateUserControlState(userControl);
}
}
private void UpdateUserControlState(UserControl1 userControl)
{
// 更新UserControl中控件的启用/禁用状态
userControl.CboSplitChar.IsEnabled = userControl.ChkSplitBarcode.IsChecked == true;
userControl.TxtSplitIndex.IsEnabled = userControl.ChkSplitBarcode.IsChecked == true;
bool extractToEndEnabled = !(userControl.ChkExtractToEnd.IsChecked == true);
userControl.TxtSubstringLength.IsEnabled = extractToEndEnabled;
userControl.TxtMinLength.IsEnabled = userControl.ChkMinLength.IsChecked == true;
userControl.TxtMaxLength.IsEnabled = userControl.ChkMaxLength.IsChecked == true;
// 更新匹配结果显示
MaterialCodeMatch match = currentSelectedButton?.Tag as MaterialCodeMatch;
if (match != null)
{
CheckMatchForUserControl(match, userControl);
}
}
private void BtnAddMatch_Click(object sender, RoutedEventArgs e)
{
// 创建新的匹配设置
MaterialCodeMatch newMatch = new MaterialCodeMatch
{
Keyword = " ",
SubstringLength = -1,
// 初始化其他提取设置
MatchISNumber = false,
MatchSplit = false,
SplitPart = 1,
SubstringStart = 0,
MatchMinLength = false,
MinLength = 0,
MatchMaxLength = false,
MaxLength = 0
};
// 添加到UI
AddMatch(newMatch);
// 选中新添加的按钮
if (matchButton.Count > 0)
{
Button lastButton = matchButton.Keys.Last();
KeywordButton_Click(lastButton, null);
}
}
private void List_DelClick(object sender, RoutedEventArgs e)
{
string YU = BLLCommon.config.Language;
// 如果只有一个匹配项,不允许删除
//if (matchButton.Count <= 1)
//{
// if (YU=="English")
// {
// NeoAlertBox.Show("Notice", "At least one keyword must be kept", AlertType.Warning, "NEO SCAN", true);
// }
// else if (YU == "日语")
// {
// NeoAlertBox.Show("通知", "少なくとも1つのキーワードを保持する必要があります", AlertType.Warning, "NEO SCAN", true);
// }
// else
// {
// NeoAlertBox.Show("提示", "至少需要保留一个关键字", AlertType.Warning, "NEO SCAN", true);
// }
// return;
//}
if (currentSelectedButton != null)
{
// 从Frame中移除对应的UserControl
if (userControls.TryGetValue(currentSelectedButton, out UserControl1 userControl))
{
if (aa.Content == userControl)
{
aa.Content = null;
}
userControls.Remove(currentSelectedButton);
}
// 删除匹配项
FlowLayoutKeywords.Children.Remove(currentSelectedButton);
matchButton.Remove(currentSelectedButton);
// 选中剩余的第一个按钮
if (matchButton.Count > 0)
{
currentSelectedButton = matchButton.Keys.First();
KeywordButton_Click(currentSelectedButton, null);
}
else
{
currentSelectedButton = null;
}
}
}
private void MatchTextChanged(object sender, RoutedEventArgs e)
{
// 验证匹配条件
CheckMatch();
}
private bool CheckMatch()
{
// 首先检查 codeText 是否为 null
if (codeText == null)
{
if (currentSelectedButton != null && userControls.TryGetValue(currentSelectedButton, out UserControl1 userControl))
{
userControl.LblMatchRes.Text = "";
userControl.LblMatchRes.Foreground = Brushes.Red;
}
return false;
}
// 现在安全地使用 codeText
string code = ChkCaseSensitivity.IsChecked == true ? codeText : codeText.ToUpper();
bool ismatch = true;
// 验证开头匹配
if (ChkMatchingStart.IsChecked == true)
{
string text = ChkCaseSensitivity.IsChecked == true
? TxtMatchingStartText.Text
: TxtMatchingStartText.Text.ToUpper();
if (string.IsNullOrEmpty(text) || !code.StartsWith(text))
{
ismatch = false;
}
}
// 验证结尾匹配
if (ChkMatchingEnd.IsChecked == true)
{
string text = ChkCaseSensitivity.IsChecked == true
? TxtMatchingEndText.Text
: TxtMatchingEndText.Text.ToUpper();
if (string.IsNullOrEmpty(text) || !code.EndsWith(text))
{
ismatch = false;
}
}
// 验证中间匹配
if (ChkMatchingMiddle.IsChecked == true)
{
string text = ChkCaseSensitivity.IsChecked == true
? TxtMatchingMiddleText.Text
: TxtMatchingMiddleText.Text.ToUpper();
if (string.IsNullOrEmpty(text))
{
ismatch = false;
}
else
{
int count = 0;
int index = 0;
while ((index = code.IndexOf(text, index)) != -1)
{
count++;
index += text.Length;
}
int targetCount;
if (!int.TryParse(NudMiddleTextCount.Text, out targetCount))
{
targetCount = 1;
}
// 根据选择的匹配类型比较
switch (ChoMatchMiddleType.SelectedIndex-1)
{
case -1: // 至多
ismatch = count <= targetCount;
break;
case 0: // 等于
ismatch = count == targetCount;
break;
case 1: // 至少
ismatch = count >= targetCount;
break;
default:
ismatch = count == targetCount;
break;
}
}
}
// 如果当前有选中的按钮和对应的UserControl,更新其解析结果
if (currentSelectedButton != null && userControls.TryGetValue(currentSelectedButton, out UserControl1 currentUserControl))
{
MaterialCodeMatch match = currentSelectedButton.Tag as MaterialCodeMatch;
if (match != null)
{
CheckMatchForUserControl(match, currentUserControl, ismatch);
}
}
return ismatch;
}
private void CheckMatchForUserControl(MaterialCodeMatch match, UserControl1 userControl, bool preMatchResult = true)
{
if (codeText == null)
{
userControl.LblMatchRes.Text = "";
userControl.LblMatchRes.Foreground = Brushes.Red;
return;
}
string matchedText = preMatchResult ? codeText : "NG";
// 如果基本匹配成功,并且启用了提取功能,则进行提取处理
if (preMatchResult)
{
matchedText = ExtractCodeText(codeText, match, userControl);
}
userControl.LblMatchRes.Text = matchedText;
userControl.LblMatchRes.Foreground = matchedText != "NG" && !string.IsNullOrEmpty(matchedText) ? Brushes.White : Brushes.Red;
}
private string ExtractCodeText(string text, MaterialCodeMatch match, UserControl1 userControl)
{
string YU = BLLCommon.config.Language;
try
{
string result = text;
// 条码分割
if (userControl.ChkSplitBarcode.IsChecked == true && userControl.CboSplitChar.SelectedItem != null)
{
string splitChar = userControl.CboSplitChar.SelectedItem.ToString();
string[] parts = result.Split(splitChar.ToCharArray());
int splitIndex = int.Parse(userControl.TxtSplitIndex.Text) - 1; // UI显示从1开始,数组从0开始
if (splitIndex >= 0 && splitIndex < parts.Length)
{
result = parts[splitIndex];
}
else
{
return "NG";
}
}
// 长度限制检查
if (userControl.ChkMinLength.IsChecked == true)
{
int minLength = int.Parse(userControl.TxtMinLength.Text);
if (result.Length < minLength)
{
return "NG";
}
}
if (userControl.ChkMaxLength.IsChecked == true)
{
int maxLength = int.Parse(userControl.TxtMaxLength.Text);
if (result.Length > maxLength)
{
return "NG";
}
}
// 截取内容
int startPos = int.Parse(userControl.TxtStartPosition.Text);
if (startPos >= 0 && startPos < result.Length)
{
if (userControl.ChkExtractToEnd.IsChecked == true)
{
// 截取至结尾
result = result.Substring(startPos);
}
else
{
// 指定长度截取
int length = int.Parse(userControl.TxtSubstringLength.Text);
if (startPos + length <= result.Length)
{
result = result.Substring(startPos, length);
}
else
{
return "NG";
}
}
}
else
{
return "NG";
}
// 数字检查
if (userControl.ChkMustBeNumber.IsChecked == true)
{
if (!result.All(char.IsDigit))
{
return "NG";
}
}
//只为数字
//kmon
//2025-6-24
if (userControl.OnlyTakeBeNumber.IsChecked == true)
{
if (!result.All(char.IsDigit))
{
return "NG";
}
}
return result;
}
catch (Exception ex)
{
return ex.Message;
}
}
private void DecreaseValue_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
string targetName = btn.Tag.ToString();
TextBlock target = FindName(targetName) as TextBlock;
if (target != null)
{
int value;
if (int.TryParse(target.Text, out value) && value > 0)
{
target.Text = (value - 1).ToString();
CheckMatch();
}
}
}
private void IncreaseValue_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
string targetName = btn.Tag.ToString();
TextBlock target = FindName(targetName) as TextBlock;
if (target != null)
{
int value;
if (int.TryParse(target.Text, out value))
{
target.Text = (value + 1).ToString();
CheckMatch();
}
}
}
private void ClearTextBox_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
string targetName = btn.Tag.ToString();
TextBox target = FindName(targetName) as TextBox;
if (target != null)
{
target.Text = "";
CheckMatch();
}
}
private void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}
private void BtnMinimize_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
private void BtnMaximize_Click(object sender, RoutedEventArgs e)
{
if (this.WindowState == WindowState.Maximized)
{
this.WindowState = WindowState.Normal;
}
else
{
this.WindowState = WindowState.Maximized;
}
}
private void BtnClose_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
this.Close();
}
private void BtnOK_Click(object sender, RoutedEventArgs e)
{
// 保存当前设置
SaveCurrentMatchSettings();
string YU = BLLCommon.config.Language;
// 检查所有关键字是否有效
Dictionary<string, int> keyTemp = new Dictionary<string, int>();
foreach (Button btn in matchButton.Keys)
{
MaterialCodeMatch match = btn.Tag as MaterialCodeMatch;
string key = match?.Keyword;
if (string.IsNullOrWhiteSpace(key))
{
if (YU == "English")
{
NeoAlertBox.Show("Notice", "Keyword cannot be empty", AlertType.Warning, "NEO SCAN", true);
}
else if (YU == "日语")
{
NeoAlertBox.Show("通知", "キーワードを空にすることはできません", AlertType.Warning, "NEO SCAN", true);
}
else
{
NeoAlertBox.Show("提示", "关键字不能为空", AlertType.Warning, "NEO SCAN", true);
}
return;
}
if (keyTemp.ContainsKey(key))
{
keyTemp[key]++;
}
else
{
keyTemp.Add(key, 1);
}
}
// 检查关键字是否重复
foreach (string key in keyTemp.Keys)
{
if (keyTemp[key] > 1)
{
if (YU == "English")
{
NeoAlertBox.Show("Notice", $"Keyword [{key}] is duplicated, please modify", AlertType.Warning, "NEO SCAN", true);
}
else if (YU == "日语")
{
NeoAlertBox.Show("通知", $"キーワード [{key}] が重複しています。修正してください", AlertType.Warning, "NEO SCAN", true);
}
else
{
NeoAlertBox.Show("提示", $"关键字 [{key}] 重复,请修改", AlertType.Warning, "NEO SCAN", true);
}
return;
}
}
// 收集所有匹配项
CodeMatch = new List<MaterialCodeMatch>();
foreach (Button btn in matchButton.Keys)
{
MaterialCodeMatch match = btn.Tag as MaterialCodeMatch;
if (match != null)
{
// 更新公共匹配属性
match.MatchStart = ChkMatchingStart.IsChecked == true;
match.StartText = TxtMatchingStartText.Text;
match.MatchEnd = ChkMatchingEnd.IsChecked == true;
match.EndText = TxtMatchingEndText.Text;
match.MatchMiddle = ChkMatchingMiddle.IsChecked == true;
match.MiddleText = TxtMatchingMiddleText.Text;
match.MiddleTextCount = int.Parse(NudMiddleTextCount.Text);
match.CaseSensitive = ChkCaseSensitivity.IsChecked == true;
match.CheckCodeType = ChkCheckCodeType.IsChecked == true;
match.CodeType = codeType;
match.MatchMiddleType = ChoMatchMiddleType.SelectedIndex-1;
match.CodeID = codeID;
match.Characteristic = Characteristicbarcode.IsChecked == true;
// 提取设置已经在SaveCurrentMatchSettings保存
// 检查是否至少选择了一种匹配方式
bool hasmatchselect = match.MatchISNumber || match.CheckCodeType ||
match.MatchEnd || match.MatchMaxLength ||
match.MatchMiddle || match.MatchMinLength ||
match.MatchSplit || match.MatchStart;
if (!hasmatchselect)
{
if (YU == "English")
{
NeoAlertBox.Show("Notice", "Please select at least one matching method", AlertType.Warning, "NEO SCAN", true);
}
else if (YU == "日语")
{
NeoAlertBox.Show("通知", "少なくとも1つの照合方法を選択してください", AlertType.Warning, "NEO SCAN", true);
}
else
{
NeoAlertBox.Show("提示", "请至少选择一种匹配方式", AlertType.Warning, "NEO SCAN", true);
}
return;
}
// 检查必填项
if ((match.MatchEnd && string.IsNullOrEmpty(match.EndText)) ||
(match.MatchMiddle && string.IsNullOrEmpty(match.MiddleText)) ||
(match.MatchStart && string.IsNullOrEmpty(match.StartText)) ||
(match.MatchMaxLength && match.MaxLength == 0))
{
if (YU == "English")
{
NeoAlertBox.Show("Notice", "Matching condition cannot be empty", AlertType.Warning, "NEO SCAN", true);
}
else if (YU == "日语")
{
NeoAlertBox.Show("通知", "一致条件を空にすることはできません", AlertType.Warning, "NEO SCAN", true);
}
else
{
NeoAlertBox.Show("提示", "匹配条件不能为空", AlertType.Warning, "NEO SCAN", true);
}
return;
}
// 检查长度设置矛盾
if (match.MatchMinLength && match.MatchMaxLength && match.MinLength > match.MaxLength)
{
if (YU == "English")
{
NeoAlertBox.Show("Notice", "Minimum length cannot be greater than maximum length", AlertType.Warning, "NEO SCAN", true);
}
else if (YU == "日语")
{
NeoAlertBox.Show("通知", "最小の長さは最大の長さより大きくすることはできません", AlertType.Warning, "NEO SCAN", true);
}
else
{
NeoAlertBox.Show("提示", "最小长度不能大于最大长度", AlertType.Warning, "NEO SCAN", true);
}
return;
}
CodeMatch.Add(match);
}
}
// 关闭对话框
DialogResult = true;
this.Close();
}
/// <summary>
/// 更新按钮选中状态的视觉效果
/// </summary>
private void UpdateButtonSelectionState()
{
foreach (Button btn in matchButton.Keys)
{
if (btn == currentSelectedButton)
{
// 选中的按钮样式
btn.Background = new SolidColorBrush(Color.FromArgb(255, 0, 102, 204));
btn.BorderBrush = new SolidColorBrush(Colors.White);
btn.BorderThickness = new Thickness(2);
}
else
{
// 非选中按钮的标准样式
btn.Background = new SolidColorBrush(Color.FromArgb(255, 0, 120, 215));
btn.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 0, 102, 204));
btn.BorderThickness = new Thickness(1);
}
}
}
private void BtnCancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
this.Close();
}
}
// 提取设置面板类(用于管理每个关键字的特定设置)
public class ExtractSettingsPanel
{
private MaterialCodeMatch match;
public ExtractSettingsPanel(string codeText, string codeType, MaterialCodeMatch match)
{
this.match = match;
}
public MaterialCodeMatch GetMatch()
{
return match;
}
}
}