/
Form1.cs
412 lines (349 loc) · 14.1 KB
/
Form1.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
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
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.Windows.Forms;
using System.IO;
namespace StudentsList
{
public partial class Form1 : Form
{
private StudentsContainer students, deductedStudents;
private FileContainer inFileContainer, outFileContainer;
private int CurrentRowIndex = -1;
private IniFile settingsFile = new IniFile("settings.ini");
private Form2 form2;
public Form1()
{
InitializeComponent();
LoadSettings();
}
private void LoadSettings()
{
if (settingsFile.KeyExists("RestoreState"))
{
tbInFileName.Text = settingsFile.Read("InputFile");
tbOutFileName.Text = settingsFile.Read("OutputFile");
if (tbInFileName.Text != "") {
LoadInputFile(tbInFileName.Text);
// CurrentRowIndex = dgwStudents.CurrentRow.Index;
}
if (tbOutFileName.Text != "")
{
outFileContainer = new FileContainer(tbOutFileName.Text);
}
cbRestoreState.Checked = true;
cbCyclicScrolling.Checked = settingsFile.Read("CyclicScrolling") == "True";
cbEnableDuplicateName.Checked = settingsFile.Read("EnableDuplicateName") == "True";
cbShowDeducted.Checked = settingsFile.Read("ShowDeducted") == "True";
}
}
private void SaveSettings()
{
settingsFile.Write("InputFile", tbInFileName.Text);
settingsFile.Write("OutputFile", tbOutFileName.Text);
if (cbRestoreState.Checked)
{
settingsFile.Write("RestoreState", "1");
} else if (settingsFile.KeyExists("RestoreState"))
{
settingsFile.DeleteKey("RestoreState");
}
settingsFile.Write("CyclicScrolling", cbCyclicScrolling.Checked.ToString());
settingsFile.Write("EnableDuplicateName", cbEnableDuplicateName.Checked.ToString());
settingsFile.Write("ShowDeducted", cbShowDeducted.Checked.ToString());
}
private void fileToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void showDeletedToolStripMenuItem_Click(object sender, EventArgs e)
{
cbShowDeducted.Checked = !cbShowDeducted.Checked;
}
private void btInFileSelect_Click(object sender, EventArgs e)
{
openFileDialogIn.DefaultExt = "*.txt";
openFileDialogIn.FileName = "";
openFileDialogIn.Filter = "Текстовые файлы (*.txt)|*.txt|Батовые файлы (*.dat)|*.dat|Бинарные файлы (*.bin)|*.bin";
openFileDialogIn.ShowDialog();
}
private void btOutFileSelect_Click(object sender, EventArgs e)
{
saveFileDialogOut.DefaultExt = "*.txt";
saveFileDialogOut.FileName = "";
saveFileDialogOut.Filter = "Текстовые файлы (*.txt)|*.txt|Батовые файлы (*.dat)|*.dat|Бинарные файлы (*.bin)|*.bin";
saveFileDialogOut.ShowDialog();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void openFileDialogIn_FileOk(object sender, CancelEventArgs e)
{
tbInFileName.Text = openFileDialogIn.FileName;
LoadInputFile(tbInFileName.Text);
LoadStudentsInGrid();
}
private void LoadInputFile(string filename)
{
students = new StudentsContainer();
deductedStudents = new StudentsContainer();
inFileContainer = new FileContainer(filename);
inFileContainer.Load(ref students, ref deductedStudents);
LoadStudentsInGrid();
}
private void LoadStudentsInGrid()
{
dgwStudents.Rows.Clear();
foreach (Student student in students)
{
dgwStudents.Rows.Add(new string[] { student.Name, student.Group, student.Subject, student.Mark, ""});
}
if (CurrentRowIndex == -1 && students.Count > 0)
{
CurrentRowIndex = 0;
}
if (cbShowDeducted.Checked)
{
foreach (Student student in deductedStudents)
{
dgwStudents.Rows.Add(new string[] { student.Name, student.Group, student.Subject, student.Mark, "Отчислен" });
}
if (CurrentRowIndex == -1 && deductedStudents.Count + students.Count > 0)
{
CurrentRowIndex = 0;
}
}
}
private bool IsEmptyRow(DataGridViewRow Row)
{
return (Row.Cells[0].Value == null || Row.Cells[0].Value.ToString().Equals(""))
&& (Row.Cells[1].Value == null || Row.Cells[1].Value.ToString().Equals(""))
&& (Row.Cells[2].Value == null || Row.Cells[2].Value.ToString().Equals(""))
&& (Row.Cells[3].Value == null || Row.Cells[3].Value.ToString().Equals(""));
}
private Student getCurrentStudent()
{
Student student;
if (dgwStudents.CurrentRow.Index < students.Count)
{
student = (Student)students[dgwStudents.CurrentRow.Index];
}
else
{
student = (Student)deductedStudents[dgwStudents.CurrentRow.Index - students.Count];
}
return student;
}
private bool isUniqueName(string name) {
foreach (Student student in students)
{
if (student.Name == name)
{
return false;
}
}
foreach (Student student in deductedStudents)
{
if (student.Name == name)
{
return false;
}
}
return true;
}
private void dgwStudents_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (!(students is null))
{
Student student = getCurrentStudent();
if (isUniqueName(dgwStudents.CurrentRow.Cells[0].Value?.ToString()) || cbEnableDuplicateName.Checked)
{
student.Name = dgwStudents.CurrentRow.Cells[0].Value?.ToString();
} else
{
// MessageBox.Show("Студент с таким ФИО уже существует");
dgwStudents.CurrentRow.Cells[0].Value = student.Name;
}
student.Group = dgwStudents.CurrentRow.Cells[1].Value?.ToString();
student.Subject = dgwStudents.CurrentRow.Cells[2].Value?.ToString();
student.Mark = dgwStudents.CurrentRow.Cells[3].Value?.ToString();
if (student.Mark != "")
{
int mark;
try
{
if (int.TryParse(student.Mark, out mark))
{
if (mark > 5 || mark < 2)
{
throw new MarkValueException();
}
}
else
{
throw new MarkValueException();
}
}
catch (MarkValueException)
{
MessageBox.Show("Оценка может быть в пределах от 2 до 5");
student.Mark = "";
dgwStudents.CurrentRow.Cells[3].Value = "";
}
}
}
}
private void btClear_Click(object sender, EventArgs e)
{
dgwStudents.CurrentRow.Cells[0].Value = "";
dgwStudents.CurrentRow.Cells[1].Value = "";
dgwStudents.CurrentRow.Cells[2].Value = "";
dgwStudents.CurrentRow.Cells[3].Value = "";
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
SaveSettings();
}
private void btFirstRecord_Click(object sender, EventArgs e)
{
dgwStudents.CurrentCell = dgwStudents.Rows[0].Cells[0];
}
private void btLastRecord_Click(object sender, EventArgs e)
{
dgwStudents.CurrentCell = dgwStudents.Rows[dgwStudents.Rows.Count - 1].Cells[0];
}
private void btPrevRecord_Click(object sender, EventArgs e)
{
if (dgwStudents.CurrentRow.Index > 0)
{
dgwStudents.CurrentCell = dgwStudents.Rows[dgwStudents.CurrentRow.Index - 1].Cells[0];
} else if (cbCyclicScrolling.Checked)
{
dgwStudents.CurrentCell = dgwStudents.Rows[dgwStudents.Rows.Count - 1].Cells[0];
}
}
private void btNextRecord_Click(object sender, EventArgs e)
{
if (dgwStudents.CurrentRow.Index < dgwStudents.Rows.Count - 1)
{
dgwStudents.CurrentCell = dgwStudents.Rows[dgwStudents.CurrentRow.Index + 1].Cells[0];
}
else if (cbCyclicScrolling.Checked)
{
dgwStudents.CurrentCell = dgwStudents.Rows[0].Cells[0];
}
}
private void btCreateRecord_Click(object sender, EventArgs e)
{
students.Add(new Student());
LoadStudentsInGrid();
dgwStudents.CurrentCell = dgwStudents.Rows[students.Count-1].Cells[0];
}
private void btDeduct_Click(object sender, EventArgs e)
{
Student student = getCurrentStudent();
if (student.IsDeducted == false) {
student.IsDeducted = true;
deductedStudents.Add(student);
students.RemoveAt(CurrentRowIndex);
outFileContainer.Save(ref students, ref deductedStudents);
LoadStudentsInGrid();
}
if (form2 != null && form2.Visible)
{
form2.LoadStudentsInGrid();
}
}
public void ReturnStudent(int index)
{
Student student = (Student) deductedStudents[index];
student.IsDeducted = false;
students.Add(student);
deductedStudents.RemoveAt(index);
outFileContainer.Save(ref students, ref deductedStudents);
LoadStudentsInGrid();
}
public void TotalDeductStudent(int index)
{
deductedStudents.RemoveAt(index);
outFileContainer.Save(ref students, ref deductedStudents);
LoadStudentsInGrid();
}
private void cbShowDeducted_CheckedChanged(object sender, EventArgs e)
{
LoadStudentsInGrid();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
outFileContainer.Save(ref students, ref deductedStudents);
}
private void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
btInFileSelect_Click(sender, e);
}
private void miAddRecord_Click(object sender, EventArgs e)
{
btCreateRecord_Click(sender, e);
}
private void miDeduct_Click(object sender, EventArgs e)
{
btDeduct_Click(sender, e);
}
private void miClear_Click(object sender, EventArgs e)
{
btClear_Click(sender, e);
}
private void btShowDeducted_Click(object sender, EventArgs e)
{
form2 = new Form2(students, deductedStudents);
form2.Owner = this;
form2.Show();
}
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Данная программа предназначена для управления списками студентов.\n" +
"Студенты могут находится в списке на отчисление, из которго их можно либо отчислить " +
"окончательно, либо восстановить.");
}
private void dgwStudents_SelectionChanged(object sender, EventArgs e)
{
if (!(outFileContainer is null) && CurrentRowIndex != -1 && CurrentRowIndex < dgwStudents.Rows.Count && dgwStudents.CurrentRow.Index != CurrentRowIndex)
{
if (IsEmptyRow(dgwStudents.Rows[CurrentRowIndex]))
{
if (CurrentRowIndex < students.Count)
{
students.RemoveAt(CurrentRowIndex);
}
else
{
deductedStudents.RemoveAt(CurrentRowIndex - students.Count);
}
CurrentRowIndex -= 1;
outFileContainer.Save(ref students, ref deductedStudents);
LoadStudentsInGrid();
if (CurrentRowIndex != -1)
{
dgwStudents.CurrentCell = dgwStudents.Rows[CurrentRowIndex].Cells[0];
}
}
else
{
outFileContainer.Save(ref students, ref deductedStudents);
}
}
CurrentRowIndex = dgwStudents.CurrentRow != null ? dgwStudents.CurrentRow.Index : -1;
}
private void saveFileDialogOut_FileOk(object sender, CancelEventArgs e)
{
tbOutFileName.Text = saveFileDialogOut.FileName;
outFileContainer = new FileContainer(saveFileDialogOut.FileName);
}
}
public class MarkValueException : Exception { };
}