-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
362 lines (330 loc) · 13.9 KB
/
MainForm.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
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Windows.Forms;
using RNC.Entities;
using RNC.Implementors;
using RNC.Tools;
using RNC.Reporting;
using Excel;
namespace RNC
{
public partial class MainForm : Form
{
private List<Member> selectedMembers;
private List<Member> uploadedMembers;
private List<Member> activeViewMembers;
private Dictionary<int, Member> memberDict;
private bool triggerSelectEvent = true;
private MemberImpl memberImpl = new MemberImpl();
public bool TriggerSelectEvent
{
get { return triggerSelectEvent; }
set { triggerSelectEvent = value; }
}
public Dictionary<int, Member> MemberDict
{
get { return memberDict; }
set { memberDict = value; }
}
public List<Member> SelectedMembers
{
get { return selectedMembers; }
set { selectedMembers = value; }
}
public List<Member> UploadedMembers
{
get { return uploadedMembers; }
set { uploadedMembers = value; }
}
public List<Member> ActiveViewMembers
{
get { return activeViewMembers; }
set { activeViewMembers = value; }
}
public MainForm()
{
InitializeComponent();
selectedMembers = new List<Member>();
this.ActiveViewMembers = new List<Member>();
memberDict = new Dictionary<int, Member>();
}
private void Form1_Load(object sender, EventArgs e)
{
display(this.memberImpl.getAllMembers());
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void addMemberToolStripMenuItem_Click(object sender, EventArgs e)
{
this.btnAdd.PerformClick();
}
//This is called everytime the DataGridView has to be changed!!!
public void display(List<Member> members)
{
this.ActiveViewMembers = members;
this.dataGridView.Rows.Clear();
if (members != null && members.Count != 0)
{
this.dataGridView.Rows.Add(members.Count);
//this.dataGridView.Rows[0].Selected = false;
memberDict.Clear();
for (int i = 0; i < members.Count; i++)
{
Member member = members[i];
this.dataGridView.Rows[i].Cells[0].Value = i + 1;
this.memberDict.Add(i + 1, member);
this.dataGridView.Rows[i].Cells[1].Value = member.Lastname;
this.dataGridView.Rows[i].Cells[2].Value = member.Firstname;
this.dataGridView.Rows[i].Cells[3].Value = member.Sex;
this.dataGridView.Rows[i].Cells[4].Value = member.City;
this.dataGridView.Rows[i].Cells[5].Value = member.Email;
this.dataGridView.Rows[i].Cells[6].Value = member.Phone;
this.dataGridView.Rows[i].Cells[7].Value = member.Cardnum;
//this.dataGridView.Rows[i].ContextMenuStrip = this.cms_memberOptions;
}
this.dataGridView.Rows[0].Selected = false;
}
else
{
this.dataGridView.Rows.Clear();
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.btnExit.PerformClick();
}
private void selectionChanged(object sender, EventArgs e)
{
selectedMembers.Clear();
if (this.MemberDict.Count > 0 && this.triggerSelectEvent)
{
foreach (DataGridViewRow row in this.dataGridView.SelectedRows)
{
//I used Cells[0].Value because it holds the numero!
Member member = memberDict[int.Parse(row.Cells[0].Value.ToString())];
row.ContextMenuStrip = this.cms_memberOptions;
selectedMembers.Add(member);
}
}
}
private void btnEdit_Click(object sender, EventArgs e)
{
if (this.selectedMembers.Count == 0)
{
ErrorMessageBox errorMessageBox = new ErrorMessageBox(this, "No selection made");
errorMessageBox.ShowDialog();
}
else
{
Form_Edit_New form = new Form_Edit_New(this, this.selectedMembers);
form.ShowDialog();
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (this.selectedMembers.Count > 0)
{
//ConfirmationBox conf = new ConfirmationBox();
//conf.ShowDialog();
}
else if (this.selectedMembers.Count == 0)
{
ErrorMessageBox errorMessageBox = new ErrorMessageBox(this, "No selection made");
errorMessageBox.ShowDialog();
}
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox aboutBox = new AboutBox(this);
aboutBox.ShowDialog();
}
private void easySelectionToolStripMenuItem_Click(object sender, EventArgs e)
{
EasySelect easySelect = new EasySelect(this);
easySelect.ShowDialog();
}
private void btnClearSelection_Click(object sender, EventArgs e)
{
this.TriggerSelectEvent = false;
display(this.memberImpl.getAllMembers());
this.TriggerSelectEvent = true;
}
private void toolStripMenuEdit_Click(object sender, EventArgs e)
{
Form_Edit_New form = new Form_Edit_New(this, this.selectedMembers);
form.ShowDialog();
}
private void toolStripMenuDelete_Click(object sender, EventArgs e)
{
this.btnDelete.PerformClick();
}
private void rowRightClick(object sender, DataGridViewCellMouseEventArgs e)
{
//This is not working....
//if (e.RowIndex >= 0 && e.Button == System.Windows.Forms.MouseButtons.Right) {
// this.dataGridView.Rows[e.RowIndex].ContextMenuStrip = this.cms_memberOptions;
// this.dataGridView.Rows[e.RowIndex].Selected = true;
// this.dataGridView.Rows[e.RowIndex].ContextMenuStrip.Show();
//}
}
private void rowDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex >= 0)
{
this.dataGridView.Rows[e.RowIndex].Selected = true;
Form_Edit_New form = new Form_Edit_New(this, this.selectedMembers);
form.ShowDialog();
}
}
private void btnSendMail_Click(object sender, EventArgs e)
{
MailSender mailSender = new MailSender(this);
mailSender.ShowDialog();
}
private void textSearchChanged(object sender, EventArgs e)
{
if (txtSearchToken.Text.Length == 0)
{
btnClearSelection.PerformClick();
this.txtSearchToken.BackColor = System.Drawing.Color.White;
}
else
{
List<Member> searchReturn = new List<Member>();
try
{
if (this.cbxSearch.Text.Equals("Nom/Prénom".Trim()))
{
searchReturn.AddRange(this.memberImpl.getMembersByFirstOrLastName_Like(txtSearchToken.Text));
}
else if (this.cbxSearch.Text.Equals("Sèxe".Trim()))
{
searchReturn.AddRange(this.memberImpl.getMembersBySex(new Converter().stringToGender(txtSearchToken.Text)));
}
else if (this.cbxSearch.Text.Equals("Ville(Province)".Trim()))
{
searchReturn.AddRange(this.memberImpl.getMembersByProvince(new Converter().stringToCity(txtSearchToken.Text)));
}
else if (this.cbxSearch.Text.Equals("Téléphone".Trim()))
{
searchReturn.AddRange(this.memberImpl.getMembersByPhone_Like(this.txtSearchToken.Text));
}
else if (this.cbxSearch.Text.Equals("Numéro Carte".Trim()))
{
searchReturn.AddRange(this.memberImpl.getMemberByCardnum_Like(int.Parse(this.txtSearchToken.Text)));
}
else if (this.cbxSearch.Text.Length == 0)
{
searchReturn.AddRange(this.memberImpl.getMembersByFirstOrLastName_Like(txtSearchToken.Text));
searchReturn.AddRange(this.memberImpl.getMembersBySex(new Converter().stringToGender(txtSearchToken.Text)));
searchReturn.AddRange(this.memberImpl.getMembersByProvince(new Converter().stringToCity(txtSearchToken.Text)));
searchReturn.AddRange(this.memberImpl.getMembersByPhone_Like(this.txtSearchToken.Text));
searchReturn.AddRange(this.memberImpl.getMemberByCardnum_Like(int.Parse(this.txtSearchToken.Text)));
}
}
catch (Exception)
{
//Iyo byananiranye...
}
if (searchReturn.Count == 0)
{
this.txtSearchToken.BackColor = System.Drawing.Color.OrangeRed;
}
else
{
this.txtSearchToken.BackColor = System.Drawing.Color.PaleGreen;
}
this.triggerSelectEvent = false;
this.display(searchReturn);
this.triggerSelectEvent = true;
}
}
private void btnPrint_Click(object sender, EventArgs e)
{
EasySelectPrint easySelectPrint = new EasySelectPrint(this);
easySelectPrint.ShowDialog();
}
private void btnAdd_Click(object sender, EventArgs e)
{
Form_Edit_New form = new Form_Edit_New(this, null);
form.ShowDialog();
}
private void searchCbxTextChanged(object sender, EventArgs e)
{
}
private void loadDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
{
this.openFileDialog.Title = "Select Excel File";
this.openFileDialog.Multiselect = false;
this.openFileDialog.Filter = "MS Office Excel File(*.xls, *xlsx)|*.xls; *xlsx";
Converter converter = new Converter();
if (this.openFileDialog.ShowDialog() == DialogResult.OK) {
string dirName = Path.GetFullPath(this.openFileDialog.FileName);
FileStream stream = File.Open(dirName, FileMode.Open, FileAccess.Read);
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//excelReader.IsFirstRowAsColumnNames = true;
//DataSet result = excelReader.AsDataSet();
if (excelReader.ResultsCount > 0)
{
List<Member> newMembers = new List<Member>();
while (excelReader.Read())
{
Member member = new Member();
member.IsValid = true;
member.Lastname = excelReader.GetString(0);
member.Firstname = excelReader.GetString(1);
if (member.Lastname.Length == 0 || member.Firstname.Length == 0) {
member.IsValid = false;
}
string thisMemberSex = excelReader.GetString(2).ToLower();
try
{
member.Sex = converter.stringToGender(thisMemberSex);
}
catch (Exception) {
member.Sex = Gender.Unspecified;
member.IsValid = false;
}
string thisMemberCity = excelReader.GetString(3).ToLower();
try
{
member.City = converter.stringToCity(thisMemberCity);
}
catch (Exception) {
member.City = Province.Unspecified;
member.IsValid = false;
}
member.Email = excelReader.GetString(4).ToLower();
if (!(member.Email.Contains("@") || member.Email.Contains(".") || member.Email.IndexOf("@") > 0 || member.Email.LastIndexOf(".") > member.Email.IndexOf("@")))
{
member.IsValid = false;
}
member.Phone = excelReader.GetString(5);
try
{
member.Cardnum = excelReader.GetInt16(6);
}
catch (FormatException) {
member.IsValid = false;
}
newMembers.Add(member);
}
this.uploadedMembers = newMembers;
//Create a file upload manager...
ExcelFileUploader fileUploader = new ExcelFileUploader(this);
fileUploader.ShowDialog();
}
excelReader.Close();
}
}
private void managementToolStripMenuItem_Click(object sender, EventArgs e)
{
Management management = new Management(this);
management.ShowDialog();
}
}
}