Repository navigation
Expand file tree
/
Copy pathfrmAddTask.cs
More file actions
410 lines (347 loc) · 14.9 KB
/
Copy pathfrmAddTask.cs
File metadata and controls
410 lines (347 loc) · 14.9 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
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
/// <file>frmAddTask.cs</file>
/// <author>Laurent Barraud, David Rossy and Julien Terrapon</author>
/// <version>1.8.3</version>
/// <date>July 5th, 2026</date>
using System;
using System.Drawing;
namespace LifeProManager
{
public partial class frmAddTask : Form
{
// Dictionary to map buttons to their base resource names for hover effect
private readonly Dictionary<Button, string> _buttonBaseResourceNames = new Dictionary<Button, string>();
private DBConnection dbConn => Program.DbConn;
// Declaration of the type of main form
private frmMain? _frmMain = null;
private Tasks? task;
public frmAddTask(Form? callingForm, Tasks? task)
{
// Allows to re-use the methods of frmMain
_frmMain = callingForm as frmMain;
this.task = task;
InitializeComponent();
ApplyStoredColors();
LoadLocalizedStrings();
// Original images path mapping
_buttonBaseResourceNames[cmdValidate] = "validate";
_buttonBaseResourceNames[cmdCancel] = "cancel";
// Hover events for all buttons
cmdValidate.MouseEnter += Button_MouseEnter;
cmdValidate.MouseLeave += Button_MouseLeave;
cmdCancel.MouseEnter += Button_MouseEnter;
cmdCancel.MouseLeave += Button_MouseLeave;
}
/// <summary>
/// Loads the topics and priorities in the combo boxes, then selects the first topic, lower priority and today's date automatically
/// </summary>
private void frmAddTask_Load(object? sender, EventArgs? e)
{
// Loads the topics in the combo box
cboTopics.Items.Clear();
foreach (Lists topic in dbConn.ReadTopics())
{
cboTopics.Items.Add(topic);
cboTopics.DisplayMember = "Title";
cboTopics.ValueMember = "Id";
}
cboTopics.SelectedIndex = 0;
numYear.Maximum = DateTime.Now.Year;
txtTitle.Focus();
if (_frmMain != null && _frmMain.CopyLastTaskValues && task != null)
{
// Marks the task as important if priority is 1 or 3
chkImportant.Checked = (task.Priorities_id == 1 || task.Priorities_id == 3);
// Marks the task as repeatable if priority is 2 or 3
chkRepeatable.Checked = (task.Priorities_id == 2 || task.Priorities_id == 3);
// Birthday tasks (priority 4)
if (task.Priorities_id == 4)
{
chkBirthday.Checked = true;
// Birthday tasks default to next year
dtpDeadline.Value = DateTime.Today.AddYears(1);
// Extracts the birth year only if the description is exactly 4 digits long
// and is a valid year, otherwise defaults to the current year
numYear.Value = ExtractBirthYear(task.Description);
}
else
{
// All other tasks default to tomorrow
dtpDeadline.Value = DateTime.Today.AddDays(1);
}
// Copies the title and description from the last task
txtTitle.Text = task.Title;
txtDescription.Text = task.Description;
// Sets the topic of the new task to the same topic as the last task
cboTopics.Text = dbConn.ReadTopicName(task.Lists_id);
}
}
/// <summary>
/// Applies the stored theme colors by reading the hex values from application settings
/// and updating the controls of this window accordingly.
/// </summary>
internal void ApplyStoredColors()
{
Color secondaryWindowsBackgroundColor = ColorTranslator.FromHtml(Properties.Settings.Default.colorOfSecondaryWindowsBackground);
Color inputFieldsBackground = ColorTranslator.FromHtml(Properties.Settings.Default.colorOfInputFieldsBackground);
Color inputFieldsForeground = ColorTranslator.FromHtml(Properties.Settings.Default.colorOfInputFieldsText);
Color taskTitlesTextColor = ColorTranslator.FromHtml(Properties.Settings.Default.colorOfTaskTitlesText);
Color panelsBackgroundColor = ColorTranslator.FromHtml(Properties.Settings.Default.colorOfPanelsBackground);
// Background of the window
this.BackColor = secondaryWindowsBackgroundColor;
void ApplyThemeToControl(Control ctrl)
{
if (ctrl is TextBox || ctrl is ComboBox || ctrl is NumericUpDown)
{
ctrl.BackColor = inputFieldsBackground;
ctrl.ForeColor = inputFieldsForeground;
}
else if (ctrl is Label || ctrl is CheckBox)
{
ctrl.ForeColor = taskTitlesTextColor;
}
else if (ctrl is Panel)
{
ctrl.BackColor = panelsBackgroundColor;
}
foreach (Control childCtrl in ctrl.Controls)
{
ApplyThemeToControl(childCtrl);
}
}
foreach (Control ctrl in this.Controls)
{
ApplyThemeToControl(ctrl);
}
this.Invalidate();
}
/// <summary>
/// Changes the button's background image to the hover version
/// when the mouse enters the button area.
/// </summary>
private void Button_MouseEnter(object? sender, EventArgs? e)
{
if (sender is Button btn)
{
UIHover.HandleMouseEnter(btn, _buttonBaseResourceNames);
}
}
/// <summary>
/// Changes back the button's background image to the normal version
/// when the mouse leaves the button area.
/// </summary>
private void Button_MouseLeave(object? sender, EventArgs? e)
{
if (sender is Button btn)
{
UIHover.HandleMouseLeave(btn, _buttonBaseResourceNames);
}
}
/// <summary>
/// If the birthday checkbox is checked, the description field, the important and repeatable checkboxes are hidden
/// and the year numeric up down control is shown.
/// The title field changes to "First name" and its maximum length is reduced to 20 characters.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chkBirthday_CheckedChanged(object? sender, EventArgs? e)
{
if (chkBirthday.Checked)
{
txtDescription.Visible = false;
lblDescription.Visible = false;
chkImportant.Visible = false;
chkRepeatable.Visible = false;
lblYear.Visible = true;
numYear.Visible = true;
lblPriority.Top += 36;
txtTitle.MaxLength = 20;
txtTitle.Width = 150;
lblTitle.Text = LocalizationManager.GetString("firstName");
}
else
{
txtDescription.Visible = true;
lblDescription.Visible = true;
chkImportant.Visible = true;
chkRepeatable.Visible = true;
lblYear.Visible = false;
numYear.Visible = false;
lblPriority.Top -= 36;
txtTitle.MaxLength = 70;
txtTitle.Width = 206;
lblTitle.Text = LocalizationManager.GetString("title");
}
}
/// <summary>
/// Closes the form without any change
/// </summary>
private void cmdCancel_Click(object? sender, EventArgs? e)
{
this.Close();
}
/// <summary>
/// Adds the task specified in the textboxes for the date specified in the comboboxes into the database
/// </summary>
private void cmdValidate_Click(object? sender, EventArgs? e)
{
// Checks if the task's title is empty
if (txtTitle.Text == "")
{
MessageBox.Show(LocalizationManager.GetString("youMustGiveATitleToYourTask"), LocalizationManager.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
// Escapes single quotes to prevent SQL errors
if (txtTitle.Text.Contains("'"))
{
txtTitle.Text = txtTitle.Text.Replace("'", "''");
}
// Converts the deadline to the database format
string deadline = dtpDeadline.Value.ToString("yyyy-MM-dd");
// Retrieves the selected topic
Lists? currentTopic = cboTopics.SelectedItem as Lists;
int priorityChosen;
if (chkBirthday.Checked)
{
// Birthday tasks always have priority 4
priorityChosen = 4;
}
else if (chkImportant.Checked && chkRepeatable.Checked)
{
priorityChosen = 3;
}
else if (chkRepeatable.Checked)
{
priorityChosen = 2;
}
else if (chkImportant.Checked)
{
priorityChosen = 1;
}
else
{
// Default priority (no checkbox selected)
priorityChosen = 0;
}
// Inserts the task into the database
if (priorityChosen == 4)
{
// Birthday tasks store the year instead of a description
dbConn.InsertTask(txtTitle.Text, numYear.Value.ToString(),
deadline, priorityChosen, currentTopic!.Id, 1);
}
else
{
dbConn.InsertTask(txtTitle.Text, txtDescription.Text, deadline,
priorityChosen, currentTopic!.Id, 1);
}
_frmMain?.LoadTasks();
this.Close();
}
}
/// <summary>
/// Extracts a valid birth year from a string.
/// Accepts only a strict 4-digit format like "1984".
/// Returns the current year if the format is invalid or out of range.
/// </summary>
private int ExtractBirthYear(string description)
{
if (string.IsNullOrWhiteSpace(description))
{
return DateTime.Now.Year;
}
// Accepts only a pure 4-digit string
if (description.Length == 4 && int.TryParse(description, out int year))
{
// Ensures the year is not in the future
if (year <= DateTime.Now.Year)
{
return year;
}
}
// Default fallback: current year
return DateTime.Now.Year;
}
/// <summary>
/// Prevents unwanted copying of values when adding a new task after editing a task.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmAddTask_FormClosing(object sender, FormClosingEventArgs e)
{
if (_frmMain != null && _frmMain.CopyLastTaskValues)
{
_frmMain.CopyLastTaskValues = false;
}
}
/// <summary>
/// Loads all the localized strings for the UI elements based on the current language setting.
/// </summary>
public void LoadLocalizedStrings()
{
// --- Window title ---
this.Text = LocalizationManager.GetString("AddTask");
// --- Labels ---
lblDescription.Text = LocalizationManager.GetString("lblDescriptionText");
lblPriority.Text = LocalizationManager.GetString("lblPriorityText");
lblDeadline.Text = LocalizationManager.GetString("lblDeadlineText");
lblTitle.Text = LocalizationManager.GetString("lblTitleText");
lblTopic.Text = LocalizationManager.GetString("lblTopicText");
lblYear.Text = LocalizationManager.GetString("lblYearText");
// --- Checkboxes ---
chkImportant.Text = LocalizationManager.GetString("chkImportantText");
chkRepeatable.Text = LocalizationManager.GetString("chkRepeatableText");
chkBirthday.Text = LocalizationManager.GetString("chkBirthdayText");
}
/// <summary>
/// Handles Enter key behavior for the form.
/// If the active control is a multiline TextBox, Enter inserts a newline.
/// Otherwise, Enter triggers the validation button.
/// </summary>
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter)
{
// If the active control is a multiline TextBox, allows newline insertion
if (this.ActiveControl is TextBox tb && tb.Multiline)
{
return false; // Lets the TextBox handle Enter normally
}
cmdValidate.PerformClick();
return true; // Prevents default beep
}
else if (keyData == Keys.Escape)
{
cmdCancel.PerformClick();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
/// <summary>
/// If the user types certain characters in the title of a task,
/// it automatically checks the corresponding priority checkboxes
/// and moves the focus to the description field.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtTitle_TextChanged(object? sender, EventArgs? e)
{
if (txtTitle.TextLength == txtTitle.MaxLength)
{
txtDescription.Focus();
}
// If the user types an exclamation mark in the title of a task and the important checkbox isn't checked
else if (txtTitle.Text.Contains("!") && chkImportant.Checked == false)
{
chkImportant.Checked = true;
txtDescription.Focus();
}
// If the user types a question mark in the title of a task and the repeatable checkbox isn't checked
else if (txtTitle.Text.Contains("?") && chkRepeatable.Checked == false)
{
chkRepeatable.Checked = true;
txtDescription.Focus();
}
}
}
}