-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathForm1.cs
441 lines (380 loc) · 15.6 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
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
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 MaterialSkin;
using MaterialSkin.Controls;
using System.IO;
namespace Desktop_Flashcards
{
public partial class Form1 : MaterialForm
{
string cardDir = "";//full path of the 'cards' directory
IList<Card>[] collection;//the collection of card groups and their cards
bool continueButtonClicked = false;
//Material skin from Ignace Maes
private readonly MaterialSkinManager materialSkinManager;
/// <summary>
/// Method to run all code upon startup.
/// </summary>
public Form1()
{
InitializeComponent();
Directory.CreateDirectory("cards");//if the cards/ directory is not present, create one
this.cardDir = getCardsDirectory();//set the global variable to the full path
this.collection = makeList();
// Initialize MaterialSkinManager with theme and color scheme
materialSkinManager = MaterialSkinManager.Instance;
materialSkinManager.AddFormToManage(this);
materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT;
materialSkinManager.ColorScheme = new ColorScheme(Primary.Green600, Primary.Green700, Primary.Green200, Accent.Red100, TextShade.WHITE);
//populate the radio button panel under 'Create' tab
populatePanel(readCardPanel);
if (Directory.GetDirectories(this.cardDir).Length == 0)
{
materialLabel2.Text = "Get started by creating a new card group under 'Groups'.";
materialLabel2.Location = new Point(165, 50);
}
else
{
materialLabel2.Text = "Select a card group to read.";
materialLabel2.Location = new Point(258, 50);
}
populatePanel(createCardPanel);
populatePanel(groupPanel);
}
/// <summary>
/// Get the directory of the .exe and return
/// a string containing the "\cards" directory.
/// </summary>
/// <returns></returns>
private string getCardsDirectory()
{
string path = Directory.GetCurrentDirectory() + "\\cards\\";
return path;
}
/// <summary>
/// Make an IList of Card objects to allow for
/// traversing through each card group.
/// </summary>
/// <returns></returns>
private IList<Card>[] makeList()
{
//get card group paths and store it in a string array
string[] folders = Directory.GetDirectories(getCardsDirectory());
//number of card groups the user has
int numGroups = folders.Length;
//Create an array of ILists. The size of the array depends on the number of card groups the user has.
IList<Card>[] iListArray = new IList<Card>[numGroups];
for (int i = 0; i < iListArray.Length; i++)
{
var type = Type.GetType(typeof(List<Card>).AssemblyQualifiedName);
var list = (IList<Card>)Activator.CreateInstance(type);
iListArray[i] = list;
}
//get cards from each card group
for (int i = 0; i < numGroups; i++)
{
string[] fileEntries = System.IO.Directory.GetFiles(folders[i]);//.txt file paths of current card group
int numCards = fileEntries.Length;//number of cards in the folder
for (int j = 0; j < numCards; j++)
{
Card tempCard = makeCardObject(fileEntries[j]);//make a card from the current file
tempCard.belongsTo = folders[i];
try
{
iListArray[i].Add(tempCard);//add the card to the iList array's appropriate index
}
catch (NullReferenceException e)
{
Console.WriteLine("Exception when adding card to IList");
}
}
}
return iListArray;
}
/// <summary>
/// Reads a .txt file, populates
/// a Card object using its content
/// and returns it.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private Card makeCardObject(string path)
{
Card newCard = new Card();
int counter = 1;
string line;
// Read the file and add each line to the Card.
System.IO.StreamReader file = new System.IO.StreamReader(path);
while ((line = file.ReadLine()) != null)
{
newCard.writeToSide(counter, line);
counter++;
}
file.Close();
return newCard;
}
/// <summary>
/// Returns the string of the selected radio
/// button in a given FlowLayoutPanel.
/// </summary>
/// <param name="panel"></param>
/// <returns></returns>
private string getSelectedRadioButton(FlowLayoutPanel panel)
{
var checkedButton = panel.Controls.OfType<MaterialRadioButton>().FirstOrDefault(r => r.Checked);
try
{
return checkedButton.Text;
}
catch (NullReferenceException e)
{
return null;
}
finally { }
}
/// <summary>
/// Populates the panel containing radio buttons in 'Create' tab.
/// </summary>
private void populatePanel(FlowLayoutPanel panel)
{
MaterialRadioButton radioBtn;
string[] cardGroups = Directory.GetDirectories(this.cardDir);
for (int i = 0; i < cardGroups.Length; i++)
{
radioBtn = new MaterialRadioButton();
radioBtn.AutoSize = true;
radioBtn.Depth = 0;
radioBtn.Font = new System.Drawing.Font("Roboto", 10F);
radioBtn.Location = new System.Drawing.Point(3, 3);
radioBtn.Margin = new System.Windows.Forms.Padding(0);
radioBtn.MouseLocation = new System.Drawing.Point(-1, -1);
radioBtn.MouseState = MaterialSkin.MouseState.HOVER;
radioBtn.Name = "radioBtnRadiobutton";
radioBtn.Ripple = true;
radioBtn.Size = new System.Drawing.Size(163, 30);
radioBtn.TabIndex = 0;
radioBtn.TabStop = true;
cardGroups[i] = cardGroups[i].Replace(this.cardDir, "");
radioBtn.Text = cardGroups[i];
radioBtn.UseVisualStyleBackColor = true;
panel.Controls.Add(radioBtn);
}
}
/// <summary>
/// Method to update each radio button panels'
/// content.
/// </summary>
private void repopulateAllPanels()
{
readCardPanel.Controls.Clear();
populatePanel(readCardPanel);
if (Directory.GetDirectories(this.cardDir).Length == 0)
{
materialLabel2.Text = "Get started by creating a new card group under 'Groups'.";
materialLabel2.Location = new Point(165, 50);
}
else
{
materialLabel2.Text = "Select a card group to read.";
materialLabel2.Location = new Point(258, 50);
}
createCardPanel.Controls.Clear();
populatePanel(createCardPanel);
groupPanel.Controls.Clear();
populatePanel(groupPanel);
}
/// <summary>
/// Events when the 'Create Card' button is clicked. The functionality
/// from the console application's 'createCard()' method is included here.
/// </summary>
private void createCardBtn_Click(object sender, EventArgs e)
{
//get selected radio button
string group = getSelectedRadioButton(createCardPanel);
string groupPath;
//error checking for selected radio button and input fields
if (group == null
|| String.IsNullOrWhiteSpace(newCardFront.Text)
|| String.IsNullOrWhiteSpace(newCardBack.Text))
{
MessageBox.Show("Please enter valid input for the card and select a card group.");
newCardFront.Clear();
newCardBack.Clear();
}
else
{
groupPath = this.cardDir + group;
int counter = 1;
//get user's 'front' and 'back' input
string[] cardContent = { "", "" };
cardContent[0] = newCardFront.Text;
cardContent[1] = newCardBack.Text;
//finds an integer that is not already used by another file
while (File.Exists(groupPath + "\\" + counter + ".txt"))
{
counter++;
}
//create and write to new text file
System.IO.File.WriteAllLines(groupPath + "\\" + counter + ".txt", cardContent);
this.collection = makeList();
newCardFront.Clear();
newCardBack.Clear();
MessageBox.Show("New card was created!");
}
}
/// <summary>
/// Events when the 'Create Card Group' button is clicked.
/// The functionality from the console applciation's "createCardGroup()"
/// method is included here.
/// </summary>
private void createGroupBtn_Click(object sender, EventArgs e)
{
//get new card group name and combine with full 'cards/' path
string newGroup = createGroupInput.Text;
string tempPath = this.cardDir + newGroup;
//error checking
if (String.IsNullOrWhiteSpace(newGroup))
{
MessageBox.Show("Please enter a valid card group name.");
}
else if (Directory.Exists(tempPath))
{
MessageBox.Show("This card group already exists!");
}
else
{
//make the card group
Directory.CreateDirectory(tempPath);
MessageBox.Show(newGroup + " was created!");
repopulateAllPanels();
}
}
/// <summary>
/// Events when the 'Delete Card Group' button is clicked.
/// </summary>
private void deleteGroupBtn_Click(object sender, EventArgs e)
{
string group = getSelectedRadioButton(groupPanel);
string groupPath;
if (group == null)
{
MessageBox.Show("Please select a group to delete!");
}
else
{
groupPath = this.cardDir + group;
DialogResult result = MessageBox.Show("Are you sure you want to delete " + group + "? All cards within the group will be lost.",
"WARNING",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if(result == DialogResult.Yes)
{
Directory.Delete(groupPath, true);
MessageBox.Show(group + " was deleted.");
repopulateAllPanels();
}
else
{
MessageBox.Show("Nothing was deleted.",
"Notification",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}
private void readCardBtn_Click(object sender, EventArgs e)
{
string panel = getSelectedRadioButton(readCardPanel);
if (panel == null)
{
MessageBox.Show("Please select a card group to read!");
return;
}
string cardGroupPath = Directory.GetCurrentDirectory() +"\\cards\\" + panel;
int counter = 0;
if (Directory.GetFiles(cardGroupPath).Length == 0)
{
MessageBox.Show("The Card Group is empty");
return;
}
//find the correct IList in the collections array
while (counter < collection.Length - 1)
{
if (collection[counter].Count == 0)
{
}
else
{
IList<Card> temp = collection[counter];
if (temp[0].belongsTo.Equals(cardGroupPath))
{
Console.WriteLine("Found the matching card group.");
break;
}
}
counter++;
}
materialTabSelector1.Visible = false;
//Image image = Image.FromFile(Directory.GetCurrentDirectory() + "\\card.png");
Image image = Properties.Resources.materialCard;
Rectangle rect = new Rectangle(5,10, 700, 345);
IList<Card> toRead = collection[counter];//the card group to be read
panel1.Visible = true;
SolidBrush s = new SolidBrush(Color.Black);
Graphics g = panel1.CreateGraphics();
//FontFamily ff = new FontFamily("Calibri");
System.Drawing.Font font = new System.Drawing.Font("Roboto", 15F);
int numCards = toRead.Count;//number of cards in the group
Random rand = new Random();//random number generator
int read = 0;//number of cards that have been read
while (read < numCards)
{
g.DrawImage(image, rect);
Card card = toRead[rand.Next(0, numCards)];
if (card.viewed == false)
{
g.DrawString(card.sideOne, font, s, new PointF(50, 50));
while (true)
{
Application.DoEvents();
if (continueButtonClicked)
{
break;
}
}
continueButtonClicked = false;
g.DrawString(card.sideTwo, font, s, new PointF(50, 150));
while (true)
{
Application.DoEvents();
if (continueButtonClicked)
{
break;
}
}
continueButtonClicked = false;
read++;
card.viewed = true;
}
g.Clear(Color.White);
}
for (int i = 0; i < numCards;i++)
{
toRead[i].viewed = false;
}
panel1.Visible = false;
materialTabSelector1.Visible = true;
}
private void button1_Click(object sender, EventArgs e)
{
continueButtonClicked = true;
}
}
}