-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
294 lines (245 loc) · 8.51 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WordOfTheDay.Properties;
namespace WordOfTheDay
{
public partial class Form1 : Form
{
private readonly List<string> _availableWordsTotal = new List<string>();
private readonly List<string> _learnedWords = new List<string>();
private string _currentWord = "";
public Form1()
{
InitializeComponent();
CheckIfTextFilesExist();
GetLearnedWords();
GetAllAvailableWords();
if (GetCurrentWord() != "")
{
_currentWord = GetCurrentWord();
UpdateTextToDisplay();
}
else
UpdateTextToDisplay("No word has been chosen yet.");
UpdatePercentage();
}
private void Form1_Load(object sender, EventArgs e)
{
// Set window location
if (Settings.Default.WindowLocation != null)
{
this.Location = Settings.Default.WindowLocation;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void UpdateTextToDisplay()
{
//wordToShow.Text = _currentWord;
string[] firstWord = _currentWord.Split('=');
wordToShow.Text = firstWord[0];
}
private void UpdateTextToDisplay(string text)
{
wordToShow.Text = text;
}
private void button1_Click(object sender, EventArgs e)
{
}
private void GetRandomWord()
{
// TODO: chance of getting an already-learned word
Random random = new Random(DateTime.Now.Millisecond);
int wordCount = _availableWordsTotal.Count();
int index;
int tries = 0;
bool duplicate = false;
// Pick a word that has not been learned yet
index = random.Next(0, wordCount);
if (_availableWordsTotal.Count > 0)
{
// See if word has already been learned
if (_learnedWords.Contains(_availableWordsTotal[index]))
{
while (tries < 500)
{
tries++;
index = random.Next(0, wordCount);
if (!_learnedWords.Contains(_availableWordsTotal[index]))
{
duplicate = false;
break;
}
duplicate = true;
}
}
if (duplicate == true)
{
string duplicateString = "!duplicate_" + _availableWordsTotal[index];
PutInCurrentWord(duplicateString);
}
else
PutInCurrentWord(_availableWordsTotal[index]);
}
else
{
wordToShow.Text = "ERROR - No words in 'AllWords.txt'";
return;
}
// Show only the word and NOT the meaning
string[] split = _currentWord.Split('=');
string firstWord = split[0].TrimEnd();
wordToShow.Text = firstWord;
}
private void GetAllAvailableWords()
{
using (StreamReader reader = new StreamReader("AllWords.txt", Encoding.Default))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (line != "")
_availableWordsTotal.Add(line);
}
}
}
private void GetLearnedWords()
{
using (StreamReader reader = new StreamReader("LearnedWords.txt", Encoding.Default))
{
string line;
while ((line = reader.ReadLine()) != null)
{
_learnedWords.Add(line);
}
}
}
private void PutInCurrentWord(string word)
{
using (StreamWriter writer = new StreamWriter("CurrentWord.txt", false, Encoding.Default))
{
writer.WriteLine(word);
_currentWord = word;
}
}
string GetCurrentWord()
{
using (StreamReader reader = new StreamReader("CurrentWord.txt", Encoding.Default))
{
string line;
if ((line = reader.ReadLine()) != null)
return line;
return "";
}
}
void ClearCurrentWordList()
{
if (_currentWord != "")
{
// No duplicates
if (_currentWord.First() != '!')
{
using (StreamWriter writer = File.AppendText("LearnedWords.txt"))
{
writer.WriteLine(_currentWord);
}
_learnedWords.Add(_currentWord);
}
}
File.WriteAllText("CurrentWord.txt", String.Empty);
_currentWord = "";
UpdateTextToDisplay("No word has been chosen yet.");
}
private void button2_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Sure?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
ClearCurrentWordList();
}
}
private void check_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure that you have used this word enough times?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
ClearCurrentWordList();
GetRandomWord();
UpdatePercentage();
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void CheckIfTextFilesExist()
{
if (!File.Exists("AllWords.txt"))
{
using (StreamWriter writer = File.CreateText("AllWords.txt"))
{
writer.WriteLine("Word1");
writer.WriteLine("Word2");
writer.WriteLine("Word3");
}
}
if (!File.Exists("CurrentWord.txt"))
File.CreateText("CurrentWord.txt");
if (!File.Exists("LearnedWords.txt"))
File.Create("LearnedWords.txt");
}
void UpdatePercentage()
{
if (_learnedWords.Count < _availableWordsTotal.Count)
percentage.Text = _learnedWords.Count + " / " + _availableWordsTotal.Count;
else
percentage.Text = _availableWordsTotal.Count + " / " + _availableWordsTotal.Count;
}
private void toolTip2_Popup(object sender, PopupEventArgs e)
{
}
private void wordToShow_MouseEnter(object sender, EventArgs e)
{
}
private void wordToShow_MouseClick(object sender, MouseEventArgs e)
{
string description = _currentWord.TrimStart();
toolTip2.Show(description, wordToShow);
}
private void wordToShow_MouseHover(object sender, EventArgs e)
{
string description = _currentWord.TrimStart();
toolTip2.Show(description, wordToShow);
}
private void pictureBox1_Click(object sender, EventArgs e)
{
GetRandomWord();
}
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
toolTip2.Show("Postpone this word for later.", pictureBox1);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// Copy window location to app settings
Settings.Default.WindowLocation = this.Location;
// Save settings
Settings.Default.Save();
}
private void Form_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) {}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
}
}