-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMainForm.cs
More file actions
2597 lines (2287 loc) · 100 KB
/
MainForm.cs
File metadata and controls
2597 lines (2287 loc) · 100 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
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace DataSplitPro
{
public partial class MainForm : Form
{
// Windows API for dark mode title bar
[DllImport("dwmapi.dll")]
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
private const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
private const int DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19;
// Removed Windows API calls that were causing crashes
private TextBox txtDelimiter = null!;
private DataGridView dgvData = null!;
private Label lblDelimiter = null!;
private Panel pnlMain = null!;
// Removed lblDeveloper and pnlBottom - info now in Status Bar
private ContextMenuStrip contextMenuStrip = null!;
private OpenFileDialog openFileDialog = null!;
private SaveFileDialog saveFileDialog = null!;
private ProgressBar progressBar = null!;
private Label lblProgress = null!;
private StatusStrip statusStrip = null!;
private ToolStripStatusLabel lblTotal = null!;
private ToolStripStatusLabel lblSelected = null!;
private ToolStripStatusLabel lblBlackout = null!;
private ToolStripStatusLabel lblColumns = null!;
private ToolStripStatusLabel lblSeparator1 = null!;
private ToolStripStatusLabel lblSeparator2 = null!;
private ToolStripStatusLabel lblSeparator3 = null!;
private ToolStripStatusLabel lblLogo = null!;
private ToolStripStatusLabel lblDev = null!;
private ToolStripStatusLabel lblTelegram = null!;
private ToolStripStatusLabel lblChannel = null!;
private ToolStripStatusLabel lblGithub = null!;
private CancellationTokenSource? cancellationTokenSource = null;
// Export functionality controls
private TextBox txtExportFormat = null!;
private Button[] columnButtons = new Button[16]; // Column1 to Column16
private Button btnClearExport = null!;
private Label lblExportFormat = null!;
private Panel pnlExportSeparator = null!;
private Panel pnlDelimiterSeparator = null!;
public MainForm()
{
try
{
Console.WriteLine("MainForm constructor started");
// Set application icon - simplified for stability
try
{
if (File.Exists("hasoftware.ico"))
{
// Simple approach - just load the icon file directly
this.Icon = new Icon("hasoftware.ico");
Console.WriteLine("Icon loaded successfully");
}
else
{
Console.WriteLine("Icon file not found: hasoftware.ico");
}
}
catch (Exception iconEx)
{
Console.WriteLine($"Error loading icon: {iconEx.Message}");
// Continue without icon if there's an error
}
InitializeComponent();
// Enable dark mode for title bar
EnableDarkMode();
Console.WriteLine("MainForm constructor completed successfully");
}
catch (Exception ex)
{
Console.WriteLine($"Error in MainForm constructor: {ex.Message}");
Console.WriteLine($"Stack trace: {ex.StackTrace}");
throw;
}
}
private void EnableDarkMode()
{
try
{
// Only try dark mode if handle is valid
if (this.Handle != IntPtr.Zero)
{
// Enable dark mode for title bar
int darkMode = 1;
int result = DwmSetWindowAttribute(this.Handle, DWMWA_USE_IMMERSIVE_DARK_MODE, ref darkMode, sizeof(int));
if (result != 0)
{
// Fallback for older Windows versions
result = DwmSetWindowAttribute(this.Handle, DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1, ref darkMode, sizeof(int));
}
Console.WriteLine($"Dark mode result: {result}");
}
else
{
Console.WriteLine("Handle not ready, skipping dark mode");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error enabling dark mode: {ex.Message}");
// Continue without dark mode if there's an error
}
}
private void InitializeComponent()
{
try
{
Console.WriteLine("InitializeComponent started");
this.SuspendLayout();
// Form properties
this.Text = "Data Split Pro v1.0 - HASOFTWARE";
this.Size = new Size(1000, 700);
this.StartPosition = FormStartPosition.CenterScreen;
this.MinimumSize = new Size(800, 600);
this.BackColor = Color.FromArgb(45, 45, 48);
this.WindowState = FormWindowState.Maximized; // Start maximized
// Main panel
pnlMain = new Panel
{
Dock = DockStyle.Fill,
Padding = new Padding(20)
};
// Delimiter section (moved down)
lblDelimiter = new Label
{
Text = "Ký tự ngăn cách:",
Font = new Font("Segoe UI", 10),
ForeColor = Color.White,
AutoSize = true,
Location = new Point(20, 180)
};
txtDelimiter = new TextBox
{
Text = "|",
Font = new Font("Consolas", 10),
Location = new Point(140, 180),
Size = new Size(50, 25),
BackColor = Color.FromArgb(60, 60, 63),
ForeColor = Color.White,
BorderStyle = BorderStyle.FixedSingle
};
// Export format section
lblExportFormat = new Label
{
Text = "📊 Data Export",
Font = new Font("Segoe UI", 11, FontStyle.Bold),
ForeColor = Color.FromArgb(100, 200, 255),
AutoSize = true,
Location = new Point(20, 20)
};
txtExportFormat = new TextBox
{
Font = new Font("Consolas", 10),
Location = new Point(20, 45),
Size = new Size(250, 25),
BackColor = Color.FromArgb(60, 60, 63),
ForeColor = Color.White,
BorderStyle = BorderStyle.FixedSingle,
PlaceholderText = "VD: Column1|Column3|Column5..."
};
btnClearExport = new Button
{
Text = "Clear",
Font = new Font("Segoe UI", 9),
Location = new Point(280, 45),
Size = new Size(60, 25),
BackColor = Color.FromArgb(220, 53, 69),
ForeColor = Color.White,
FlatStyle = FlatStyle.Flat,
Cursor = Cursors.Hand
};
btnClearExport.FlatAppearance.BorderSize = 0;
btnClearExport.Click += BtnClearExport_Click;
// Copy Export button
var btnCopyExport = new Button
{
Text = "Copy",
Font = new Font("Segoe UI", 9),
Location = new Point(350, 45),
Size = new Size(60, 25),
BackColor = Color.FromArgb(0, 122, 204),
ForeColor = Color.White,
FlatStyle = FlatStyle.Flat,
Cursor = Cursors.Hand
};
btnCopyExport.FlatAppearance.BorderSize = 0;
btnCopyExport.Click += BtnCopyExport_Click;
// Export to file button
var btnExportFile = new Button
{
Text = "Export",
Font = new Font("Segoe UI", 9),
Location = new Point(420, 45),
Size = new Size(60, 25),
BackColor = Color.FromArgb(40, 167, 69),
ForeColor = Color.White,
FlatStyle = FlatStyle.Flat,
Cursor = Cursors.Hand
};
btnExportFile.FlatAppearance.BorderSize = 0;
btnExportFile.Click += BtnExportFile_Click;
// Export section separator
pnlExportSeparator = new Panel
{
Location = new Point(20, 75),
Size = new Size(460, 2),
BackColor = Color.FromArgb(100, 200, 255)
};
// Column buttons
for (int i = 0; i < 16; i++)
{
columnButtons[i] = new RoundedButton
{
Text = $"Column{i + 1}",
Font = new Font("Segoe UI", 8),
Location = new Point(20 + (i % 8) * 70, 80 + (i / 8) * 30),
Size = new Size(65, 25),
BackColor = Color.White,
ForeColor = Color.FromArgb(0, 122, 204),
Cursor = Cursors.Hand,
Tag = i + 1 // Store column number
};
columnButtons[i].Click += ColumnButton_Click;
}
// Stats label (removed - moved to status bar)
// Progress bar
progressBar = new ProgressBar
{
Location = new Point(20, 220),
Size = new Size(600, 20),
Style = ProgressBarStyle.Marquee,
Visible = false
};
// Progress label
lblProgress = new Label
{
Text = "Đang xử lý dữ liệu...",
Font = new Font("Segoe UI", 9),
ForeColor = Color.FromArgb(100, 200, 100),
AutoSize = true,
Location = new Point(630, 223),
Visible = false
};
// Delimiter section separator
pnlDelimiterSeparator = new Panel
{
Location = new Point(20, 210),
Size = new Size(200, 2),
BackColor = Color.FromArgb(100, 100, 100)
};
// DataGridView
dgvData = new DataGridView
{
Location = new Point(20, 230),
Size = new Size(940, 400),
BackgroundColor = Color.FromArgb(45, 45, 48), // Darker background like the image
ForeColor = Color.White,
BorderStyle = BorderStyle.FixedSingle,
AllowUserToAddRows = false,
AllowUserToDeleteRows = false,
ReadOnly = true,
SelectionMode = DataGridViewSelectionMode.CellSelect,
MultiSelect = true,
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None,
ScrollBars = ScrollBars.Both, // Enable both horizontal and vertical scrollbars
ColumnHeadersDefaultCellStyle = new DataGridViewCellStyle
{
BackColor = Color.FromArgb(63, 63, 70), // Slightly lighter header like the image
ForeColor = Color.White,
Font = new Font("Segoe UI", 10, FontStyle.Bold),
Alignment = DataGridViewContentAlignment.MiddleLeft,
SelectionBackColor = Color.FromArgb(63, 63, 70),
SelectionForeColor = Color.White
},
DefaultCellStyle = new DataGridViewCellStyle
{
BackColor = Color.FromArgb(45, 45, 48), // Darker cell background
ForeColor = Color.White,
Font = new Font("Segoe UI", 9), // Changed to Segoe UI for better readability
SelectionBackColor = Color.FromArgb(0, 120, 215), // Blue selection like the image
SelectionForeColor = Color.White,
Alignment = DataGridViewContentAlignment.MiddleLeft
},
AlternatingRowsDefaultCellStyle = new DataGridViewCellStyle
{
BackColor = Color.FromArgb(50, 50, 53), // Slightly different for alternating rows
ForeColor = Color.White,
SelectionBackColor = Color.FromArgb(0, 120, 215),
SelectionForeColor = Color.White
},
RowHeadersVisible = false,
VirtualMode = false,
EnableHeadersVisualStyles = false,
AllowUserToResizeRows = false,
RowTemplate = { Height = 28 }, // Slightly taller rows
CellBorderStyle = DataGridViewCellBorderStyle.Single, // Both horizontal and vertical lines
ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single,
RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single,
GridColor = Color.FromArgb(38, 38, 38) // Very subtle vertical lines (15% opacity)
};
// Scrollbar customization removed to fix crashes
// Status Strip (VS Code style)
Console.WriteLine("Creating StatusStrip...");
statusStrip = new StatusStrip
{
BackColor = Color.FromArgb(37, 37, 38), // VS Code status bar color
ForeColor = Color.FromArgb(204, 204, 204), // VS Code text color
Font = new Font("Segoe UI", 9),
Height = 22,
SizingGrip = false
};
Console.WriteLine("StatusStrip created successfully");
// Total items label
lblTotal = new ToolStripStatusLabel
{
Text = "Total: 0",
ForeColor = Color.FromArgb(204, 204, 204),
Font = new Font("Segoe UI", 9),
AutoSize = true
};
// Separator 1
lblSeparator1 = new ToolStripStatusLabel
{
Text = "|",
ForeColor = Color.FromArgb(128, 128, 128),
Font = new Font("Segoe UI", 9)
};
// Selected items label
lblSelected = new ToolStripStatusLabel
{
Text = "Selected: 0",
ForeColor = Color.FromArgb(204, 204, 204),
Font = new Font("Segoe UI", 9),
AutoSize = true
};
// Separator 2
lblSeparator2 = new ToolStripStatusLabel
{
Text = "|",
ForeColor = Color.FromArgb(128, 128, 128),
Font = new Font("Segoe UI", 9)
};
// Blackout items label
lblBlackout = new ToolStripStatusLabel
{
Text = "Black out: 0",
ForeColor = Color.FromArgb(204, 204, 204),
Font = new Font("Segoe UI", 9),
AutoSize = true
};
// Separator 3
lblSeparator3 = new ToolStripStatusLabel
{
Text = "|",
ForeColor = Color.FromArgb(128, 128, 128),
Font = new Font("Segoe UI", 9)
};
// Columns label
lblColumns = new ToolStripStatusLabel
{
Text = "Columns: 0",
ForeColor = Color.FromArgb(204, 204, 204),
Font = new Font("Segoe UI", 9),
AutoSize = true
};
// Logo label (VS Code style)
// Load and resize logo to fit status bar height
var logoImage = Image.FromFile("hasoftware.ico");
var resizedLogo = new Bitmap(logoImage, new Size(32, 14)); // Very small to fit status bar
lblLogo = new ToolStripStatusLabel
{
Text = "",
ForeColor = Color.FromArgb(0, 120, 215),
Font = new Font("Segoe UI", 8),
AutoSize = true,
Margin = new Padding(1, 0, 3, 0),
Image = resizedLogo,
ImageScaling = ToolStripItemImageScaling.None // Prevent automatic scaling
};
// Developer info labels
lblDev = new ToolStripStatusLabel
{
Text = "Dev: Trịnh Hoàng Anh",
ForeColor = Color.FromArgb(204, 204, 204),
Font = new Font("Segoe UI", 9),
AutoSize = true,
IsLink = true,
LinkColor = Color.FromArgb(70, 150, 255), // Moderate blue for stability
ActiveLinkColor = Color.FromArgb(100, 180, 255), // Brighter on hover
Margin = new Padding(10, 0, 5, 0)
};
lblTelegram = new ToolStripStatusLabel
{
Text = "Telegram: HoangAnhDev",
ForeColor = Color.FromArgb(204, 204, 204),
Font = new Font("Segoe UI", 9),
AutoSize = true,
IsLink = true,
LinkColor = Color.FromArgb(70, 150, 255), // Moderate blue for stability
ActiveLinkColor = Color.FromArgb(100, 180, 255), // Brighter on hover
Margin = new Padding(5, 0, 5, 0)
};
lblChannel = new ToolStripStatusLabel
{
Text = "Channel: HASOFTWARE",
ForeColor = Color.FromArgb(204, 204, 204),
Font = new Font("Segoe UI", 9),
AutoSize = true,
IsLink = true,
LinkColor = Color.FromArgb(70, 150, 255), // Moderate blue for stability
ActiveLinkColor = Color.FromArgb(100, 180, 255), // Brighter on hover
Margin = new Padding(5, 0, 5, 0)
};
lblGithub = new ToolStripStatusLabel
{
Text = "Github: HASOFTWARE",
ForeColor = Color.FromArgb(204, 204, 204),
Font = new Font("Segoe UI", 9),
AutoSize = true,
IsLink = true,
LinkColor = Color.FromArgb(70, 150, 255), // Moderate blue for stability
ActiveLinkColor = Color.FromArgb(100, 180, 255), // Brighter on hover
Margin = new Padding(5, 0, 10, 0)
};
// Create spacer for right alignment
var spacer = new ToolStripStatusLabel
{
Spring = true,
Text = ""
};
// Add labels to status strip step by step to avoid errors
Console.WriteLine("Adding labels to StatusStrip...");
try
{
statusStrip.Items.Add(lblLogo);
Console.WriteLine("Added lblLogo");
statusStrip.Items.Add(lblTotal);
Console.WriteLine("Added lblTotal");
statusStrip.Items.Add(lblSeparator1);
Console.WriteLine("Added lblSeparator1");
statusStrip.Items.Add(lblSelected);
Console.WriteLine("Added lblSelected");
statusStrip.Items.Add(lblSeparator2);
Console.WriteLine("Added lblSeparator2");
statusStrip.Items.Add(lblBlackout);
Console.WriteLine("Added lblBlackout");
statusStrip.Items.Add(lblSeparator3);
Console.WriteLine("Added lblSeparator3");
statusStrip.Items.Add(lblColumns);
Console.WriteLine("Added lblColumns");
statusStrip.Items.Add(spacer); // Spacer to push developer info to the right
Console.WriteLine("Added spacer");
statusStrip.Items.Add(lblDev);
Console.WriteLine("Added lblDev");
statusStrip.Items.Add(lblTelegram);
Console.WriteLine("Added lblTelegram");
statusStrip.Items.Add(lblChannel);
Console.WriteLine("Added lblChannel");
statusStrip.Items.Add(lblGithub);
Console.WriteLine("Added lblGithub");
Console.WriteLine("All labels added successfully");
}
catch (Exception ex)
{
Console.WriteLine($"Error adding labels to StatusStrip: {ex.Message}");
Console.WriteLine($"Stack trace: {ex.StackTrace}");
throw;
}
// Add click events for links
lblDev.Click += LblDev_Click;
lblTelegram.Click += LblTelegram_Click;
lblChannel.Click += LblChannel_Click;
lblGithub.Click += LblGithub_Click;
// Bottom panel removed - developer info now in Status Bar
// Add controls to panels
var allControls = new List<Control>
{
lblDelimiter, txtDelimiter, pnlDelimiterSeparator,
lblExportFormat, pnlExportSeparator, txtExportFormat, btnClearExport, btnCopyExport, btnExportFile,
progressBar, lblProgress, dgvData
};
// Add column buttons
allControls.AddRange(columnButtons);
pnlMain.Controls.AddRange(allControls.ToArray());
this.Controls.Add(pnlMain);
this.Controls.Add(statusStrip);
// Add keyboard shortcuts
this.KeyPreview = true;
this.KeyDown += MainForm_KeyDown;
// Removed scrollbar theme loading
// Initialize file dialogs
InitializeFileDialogs();
// Initialize context menu
InitializeContextMenu();
// Add event handlers once
dgvData.ColumnHeaderMouseClick += DgvData_ColumnHeaderMouseClick;
dgvData.CellClick += DgvData_CellClick;
dgvData.SelectionChanged += DgvData_SelectionChanged;
this.Resize += MainForm_Resize;
this.ResumeLayout(false);
Console.WriteLine("InitializeComponent completed successfully");
}
catch (Exception ex)
{
Console.WriteLine($"Error in InitializeComponent: {ex.Message}");
Console.WriteLine($"Stack trace: {ex.StackTrace}");
throw;
}
}
private void MainForm_KeyDown(object? sender, KeyEventArgs e)
{
try
{
// Ctrl+A: Select all cells (highlight)
if (e.Control && e.KeyCode == Keys.A)
{
if (dgvData.Rows.Count > 0)
{
dgvData.SelectAll();
e.Handled = true;
}
}
// Ctrl+D: Clear selection (unhighlight)
else if (e.Control && e.KeyCode == Keys.D)
{
dgvData.ClearSelection();
e.Handled = true;
}
// Delete: Delete selected rows
else if (e.KeyCode == Keys.Delete)
{
if (dgvData.SelectedCells.Count > 0)
{
DeleteSelectedRows();
e.Handled = true;
}
}
// Ctrl+C: Copy selected data
else if (e.Control && e.KeyCode == Keys.C)
{
if (dgvData.SelectedCells.Count > 0)
{
CopySelectedData();
e.Handled = true;
}
}
// Space: Toggle checkboxes for selected rows
else if (e.KeyCode == Keys.Space)
{
if (dgvData.SelectedCells.Count > 0 && dgvData.Columns.Count > 1)
{
ToggleCheckboxesForSelectedRows();
e.Handled = true;
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Lỗi khi xử lý phím tắt: {ex.Message}", "Lỗi",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void InitializeFileDialogs()
{
openFileDialog = new OpenFileDialog
{
Title = "Chọn file để import",
Filter = "Text Files (*.txt)|*.txt|CSV Files (*.csv)|*.csv|All Files (*.*)|*.*",
DefaultExt = "txt"
};
saveFileDialog = new SaveFileDialog
{
Title = "Lưu file dữ liệu",
Filter = "Text Files (*.txt)|*.txt|CSV Files (*.csv)|*.csv|All Files (*.*)|*.*",
DefaultExt = "txt"
};
}
private void InitializeContextMenu()
{
contextMenuStrip = new ContextMenuStrip
{
BackColor = Color.FromArgb(45, 45, 48), // VS Code exact background
ForeColor = Color.FromArgb(212, 212, 212), // VS Code exact text color
Font = new Font("Segoe UI", 9F, FontStyle.Regular),
RenderMode = ToolStripRenderMode.System,
ShowImageMargin = false,
ShowCheckMargin = false,
DropShadowEnabled = true,
CanOverflow = false,
Padding = new Padding(0, 0, 0, 0)
};
// Add rounded corners effect
contextMenuStrip.Paint += (s, e) =>
{
var rect = new Rectangle(0, 0, contextMenuStrip.Width - 1, contextMenuStrip.Height - 1);
using (var path = GetRoundedRectanglePath(rect, 8))
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.FillPath(new SolidBrush(Color.FromArgb(45, 45, 48)), path);
e.Graphics.DrawPath(new Pen(Color.FromArgb(60, 60, 60), 1), path);
}
};
// Paste Data submenu
var pasteDataItem = CreateWindows11MenuItem("Paste Data", null);
var pasteClearOldItem = CreateWindows11MenuItem("Xóa data cũ", null, PasteFromClipboard_Click);
var pasteKeepOldItem = CreateWindows11MenuItem("Không xóa data cũ", null, PasteFromClipboardKeepOld_Click);
pasteDataItem.DropDownItems.AddRange(new ToolStripItem[] { pasteClearOldItem, pasteKeepOldItem });
// Import Data từ File submenu
var importDataItem = CreateWindows11MenuItem("Import Data từ File", null);
var importClearOldItem = CreateWindows11MenuItem("Xóa data cũ", null, ImportFile_Click);
var importKeepOldItem = CreateWindows11MenuItem("Không xóa data cũ", null, ImportFileKeepOld_Click);
importDataItem.DropDownItems.AddRange(new ToolStripItem[] { importClearOldItem, importKeepOldItem });
// Separator
var separator1 = new ToolStripSeparator
{
BackColor = Color.FromArgb(85, 85, 85), // VS Code exact separator color
Margin = new Padding(0, 1, 0, 1)
};
// Tích chọn submenu
var selectItem = CreateWindows11MenuItem("Tích chọn", null);
var selectAllItem = CreateWindows11MenuItem("Chọn tất cả", null, SelectAll_Click);
var selectHighlightedItem = CreateWindows11MenuItem("Chỉ chọn dòng bôi đen", null, SelectHighlighted_Click);
selectItem.DropDownItems.AddRange(new ToolStripItem[] { selectAllItem, selectHighlightedItem });
// Bỏ chọn submenu
var deselectItem = CreateWindows11MenuItem("Bỏ chọn", null);
var deselectAllItem = CreateWindows11MenuItem("Bỏ chọn tất cả", null, DeselectAll_Click);
var deselectHighlightedItem = CreateWindows11MenuItem("Chỉ bỏ chọn dòng bôi đen", null, DeselectHighlighted_Click);
deselectItem.DropDownItems.AddRange(new ToolStripItem[] { deselectAllItem, deselectHighlightedItem });
// Separator
var separator2 = new ToolStripSeparator
{
BackColor = Color.FromArgb(85, 85, 85), // VS Code exact separator color
Margin = new Padding(0, 1, 0, 1)
};
// Xóa Data submenu
var deleteDataItem = CreateWindows11MenuItem("Xóa Data", null);
var deleteAllItem = CreateWindows11MenuItem("Xóa toàn bộ", null, DeleteAllData_Click);
var deleteHighlightedItem = CreateWindows11MenuItem("Chỉ xóa dòng bôi đen", null, DeleteHighlighted_Click);
var deleteSelectedItem = CreateWindows11MenuItem("Chỉ xóa dòng tích chọn", null, DeleteSelected_Click);
var deleteNotHighlightedItem = CreateWindows11MenuItem("Chỉ xóa dòng không bôi đen", null, DeleteNotHighlighted_Click);
var deleteNotSelectedItem = CreateWindows11MenuItem("Chỉ xóa dòng không tích chọn", null, DeleteNotSelected_Click);
deleteDataItem.DropDownItems.AddRange(new ToolStripItem[] {
deleteAllItem,
deleteHighlightedItem,
deleteSelectedItem,
deleteNotHighlightedItem,
deleteNotSelectedItem
});
// Add items to context menu
contextMenuStrip.Items.AddRange(new ToolStripItem[]
{
pasteDataItem,
importDataItem,
separator1,
selectItem,
deselectItem,
separator2,
deleteDataItem
});
// Assign context menu to DataGridView
dgvData.ContextMenuStrip = contextMenuStrip;
}
private async void PasteFromClipboard_Click(object? sender, EventArgs e)
{
try
{
if (Clipboard.ContainsText())
{
string clipboardText = Clipboard.GetText();
// Show progress
ShowProgress(true, "Đang xử lý dữ liệu từ clipboard...");
// Process data asynchronously
await ProcessDataAsync(clipboardText, txtDelimiter.Text);
int lineCount = clipboardText.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Length;
// Show message box asynchronously to avoid blocking UI
await Task.Run(() =>
{
if (InvokeRequired)
{
Invoke(new Action(() =>
{
MessageBox.Show($"Đã paste dữ liệu từ clipboard!\nSố dòng: {lineCount}", "Thành công",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}));
}
});
}
else
{
MessageBox.Show("Clipboard không chứa dữ liệu text!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch (Exception ex)
{
MessageBox.Show($"Lỗi khi paste từ clipboard: {ex.Message}", "Lỗi",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
ShowProgress(false);
}
}
private async void ImportFile_Click(object? sender, EventArgs e)
{
try
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// Show progress
ShowProgress(true, "Đang đọc file...");
// Read file asynchronously
string fileContent = await Task.Run(() => File.ReadAllText(openFileDialog.FileName, Encoding.UTF8));
// Process data asynchronously
ShowProgress(true, "Đang xử lý dữ liệu...");
await ProcessDataAsync(fileContent, txtDelimiter.Text);
int lineCount = fileContent.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Length;
// Show message box asynchronously to avoid blocking UI
await Task.Run(() =>
{
if (InvokeRequired)
{
Invoke(new Action(() =>
{
MessageBox.Show($"Đã import file: {Path.GetFileName(openFileDialog.FileName)}\nSố dòng: {lineCount}", "Thành công",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}));
}
});
}
}
catch (Exception ex)
{
MessageBox.Show($"Lỗi khi import file: {ex.Message}", "Lỗi",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
ShowProgress(false);
}
}
private void ClearSelectedRows_Click(object? sender, EventArgs e)
{
try
{
// Count checked rows
int checkedCount = 0;
foreach (DataGridViewRow row in dgvData.Rows)
{
if (row.Cells.Count > 1 && row.Cells[1].Value is bool isChecked && isChecked)
{
checkedCount++;
}
}
if (checkedCount > 0)
{
DialogResult result = MessageBox.Show($"Bạn có chắc muốn xóa {checkedCount} dòng đã tích chọn?",
"Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
// Remove checked rows (from bottom to top to avoid index issues)
for (int i = dgvData.Rows.Count - 1; i >= 0; i--)
{
DataGridViewRow row = dgvData.Rows[i];
if (row.Cells.Count > 1 && row.Cells[1].Value is bool isChecked && isChecked)
{
dgvData.Rows.RemoveAt(i);
}
}
MessageBox.Show($"Đã xóa {checkedCount} dòng đã tích chọn!", "Thành công",
MessageBoxButtons.OK, MessageBoxIcon.Information);
// Update status bar after deletion
UpdateStatusBar();
}
}
else
{
MessageBox.Show("Vui lòng tích chọn dòng cần xóa!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch (Exception ex)
{
MessageBox.Show($"Lỗi khi xóa dòng: {ex.Message}", "Lỗi",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ClearAll_Click(object? sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Bạn có chắc muốn xóa tất cả dữ liệu?",
"Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
dgvData.DataSource = null;
dgvData.Columns.Clear();
UpdateStats(0, 0);
UpdateStatusBar();
}
}
private void CopySelectedData_Click(object? sender, EventArgs e)
{
CopySelectedData();
}
private void BtnCopyExport_Click(object? sender, EventArgs e)
{
ExportSelectedData();
}
private void BtnExportFile_Click(object? sender, EventArgs e)
{
ExportSelectedDataToFile();
}
private void SelectAll_Click(object? sender, EventArgs e)
{
try
{
if (dgvData.Rows.Count > 0 && dgvData.Columns.Count > 1)
{
// Check all checkboxes (column index 1)
foreach (DataGridViewRow row in dgvData.Rows)
{
if (row.Cells.Count > 1)
{
row.Cells[1].Value = true;
}
}
UpdateStatusBar();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error in SelectAll: {ex.Message}");
}
}
private void DeselectAll_Click(object? sender, EventArgs e)
{
try
{
if (dgvData.Rows.Count > 0 && dgvData.Columns.Count > 1)
{
// Uncheck all checkboxes (column index 1)
foreach (DataGridViewRow row in dgvData.Rows)
{
if (row.Cells.Count > 1)
{
row.Cells[1].Value = false;
}
}
UpdateStatusBar();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error in DeselectAll: {ex.Message}");
}
}
private void ToggleCheckboxesForSelectedRows()
{
try
{
if (dgvData.SelectedCells.Count == 0 || dgvData.Columns.Count <= 1)
return;
// Get unique row indices from selected cells
var selectedRowIndices = dgvData.SelectedCells
.Cast<DataGridViewCell>()
.Select(cell => cell.RowIndex)
.Distinct()
.Where(rowIndex => rowIndex >= 0 && rowIndex < dgvData.Rows.Count)
.ToList();
if (selectedRowIndices.Count == 0)
return;
// Count how many selected rows are currently checked
int checkedCount = 0;
foreach (int rowIndex in selectedRowIndices)
{
if (dgvData.Rows[rowIndex].Cells.Count > 1)
{
if (dgvData.Rows[rowIndex].Cells[1].Value is bool isChecked && isChecked)
{
checkedCount++;
}