-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
147 lines (118 loc) · 4.79 KB
/
MainForm.cs
File metadata and controls
147 lines (118 loc) · 4.79 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
using CheckDuplicatesInCSV.lib;
using System.Diagnostics;
namespace CheckExample
{
public partial class MainForm : Form
{
private static List<string> _records = [];
private static int _selectedCol;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public MainForm()
{
InitializeComponent();
}
private void Main_Load(object sender, EventArgs e)
{
Console.WriteLine("Loading MainForm");
}
private void btnReadCSV_Click(object sender, EventArgs e)
{
try
{
// 1) Check if CSV file exists
Utils.CSVFileExsists(txtPath);
// 2) Get CSV configuration
(string delimiter, _selectedCol) = Utils.GetCSVConfiguration(txtDelimiter, cboSelectedCol);
// 3) Read CSV file
_records = CSVCore.ReadCSV(
path: txtPath.Text,
delimiter: delimiter,
hasHeader: rbtnHeader.Checked,
colNumber: _selectedCol);
lblReadOutcome.Text = $"{_records.Count} records read";
// 4) Load grid with list of CSV records
gridReadRecords.DataSource = Utils.DataTableFromList(
lists: new List<List<string>>() { _records },
indexes: new string[] { $"Col #{_selectedCol}" });
// Resize grid
gridReadRecords.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
}
catch (Exception ex)
{
MessageBox.Show(
ex.Message,
"Errore - Read CSV",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void btnFindDuplicates_Click(object sender, EventArgs e)
{
try
{
// 1) Get list of duplicates
// Start timer
var stopwatch = new Stopwatch();
stopwatch.Start();
Dictionary<string, string> duplicateXCount = CSVCore.GetDuplicatesFromList(_records);
// Stop timer
stopwatch.Stop();
double elapsedSeconds = stopwatch.Elapsed.TotalSeconds;
lblTimer.Text = elapsedSeconds.ToString();
// 2) Number of repetitions and duplicates
int numItemsDupls = duplicateXCount.Keys.Count;
int numOfReps = 0;
foreach (var entry in duplicateXCount)
{
// number of ',' in the string = number of places in the column where value is repteated (+1 as initial place)
numOfReps += entry.Value.Count(c => c == ',') + 1;
}
// 3) Show duplicates on the grid
if (numItemsDupls > 0)
{
txtNumReps.ForeColor = Color.Red; // Number of duplicates in red
// Load grid with table of 2 columns, 2 indexes
gridFindDuplicates.DataSource = Utils.DataTableFromList
(
lists: new List<List<string>>()
{
duplicateXCount.Keys.ToList(), // 1° column
duplicateXCount.Values.ToList() // 2° column
},
indexes: new string[]
{
"Path",
"Indexes of repetition"
}
);
// Resize grid
gridFindDuplicates.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
else
{
txtNumReps.ForeColor = Color.Green; // 0 duplicates -> green '0'
}
// Set TextBox(s)
txtNumReps.Text = numOfReps.ToString();
txtItemRepetitions.Text = numItemsDupls.ToString();
}
catch (Exception ex)
{
MessageBox.Show(
ex.Message,
"Errore - Find duplicates",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void btnBrowseFile_Click(object sender, EventArgs e)
{
// Pick .csv file
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
txtPath.Text = openFileDialog.FileName;
}
}
}
}