-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoutineForm.cs
More file actions
447 lines (373 loc) · 14.7 KB
/
Copy pathRoutineForm.cs
File metadata and controls
447 lines (373 loc) · 14.7 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
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
442
443
444
445
446
447
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace BeTimelyProject
{
public delegate void RoutineDataSentHandler(Routine r);
public partial class RoutineForm : Form
{
#region Attributes
private readonly string TaskToStringDetails = "{0, -11}{1, -80}";
public bool NoClosePrompt;
#endregion
#region Controls
private Label Label_RoutineName;
public TextBox TextBox_RoutineName;
private GroupBox GroupBox_Tasks;
private Button Button_Cancel;
private Button Button_CreateRoutine;
private Button Button_UpdateRoutine;
// GroupBox_Tasks
private Label Label_TasksHeader;
public ListBox ListBox_Tasks;
private Button Button_CreateTask;
private Button Button_EditTask;
private Button Button_MoveUpTask;
private Button Button_MoveDownTask;
private Button Button_DeleteTask;
#endregion
#region ChildForms
private TaskForm TaskForm;
#endregion
#region Events
// Send Data to MainForm
public event RoutineDataSentHandler CreateRoutine;
public event RoutineDataSentHandler UpdateRoutine;
// Button_Cancel
private void Button_Cancel_Click(object sender, EventArgs e)
{
// Slightly different implementation of FormClosing.
bool showDialog = false;
// Check if everything is blank
if (!string.IsNullOrWhiteSpace(this.TextBox_RoutineName.Text) ||
this.ListBox_Tasks.Items.Count != 0)
showDialog = true;
if(showDialog == true)
{
DialogResult result = MessageBox.Show(
"You have unsaved fields. Are you sure to cancel?",
"",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
this.NoClosePrompt = true;
this.Hide();
}
}
else
{
this.Hide();
}
}
// Button_CreateRoutine
private void Button_CreateRoutine_Click(object sender, EventArgs e)
{
// Prevents Unsaved Dialog from appearing on FormClosing Event
this.NoClosePrompt = true;
// If Routine Name is blank
if (string.IsNullOrWhiteSpace(this.TextBox_RoutineName.Text))
this.TextBox_RoutineName.Text = "Untitled Routine";
Routine r = new Routine(this.TextBox_RoutineName.Text);
foreach(Task t in this.ListBox_Tasks.Items)
r.Tasks.Add(t);
this.CreateRoutine(r);
}
// Button_UpdateRoutine
private void Button_UpdateRoutine_Click(object sender, EventArgs e)
{
// Prevents Unsaved Dialog from appearing on FormClosing Event
this.NoClosePrompt = true;
// If Routine Name is blank
if (string.IsNullOrWhiteSpace(this.TextBox_RoutineName.Text))
this.TextBox_RoutineName.Text = "Untitled Routine";
Routine r = new Routine(this.TextBox_RoutineName.Text);
foreach (Task t in this.ListBox_Tasks.Items)
r.Tasks.Add(t);
this.UpdateRoutine(r);
}
// ListBox_Tasks
private void ListBox_Tasks_SelectedIndexChanged(object sender, EventArgs e)
{
if(this.ListBox_Tasks.SelectedItem == null)
{
this.Button_EditTask.Enabled = false;
this.Button_MoveUpTask.Enabled = false;
this.Button_MoveDownTask.Enabled = false;
this.Button_DeleteTask.Enabled = false;
}
else
{
this.Button_EditTask.Enabled = true;
this.Button_MoveUpTask.Enabled = true;
this.Button_MoveDownTask.Enabled = true;
this.Button_DeleteTask.Enabled = true;
}
}
// Button_CreateTask
private void Button_CreateTask_Click(object sender, EventArgs e)
{
this.TaskForm.Reset();
this.TaskForm.ShowDialog();
}
// Button_EditTask
private void Button_EditTask_Click(object sender, EventArgs e)
{
this.TaskForm.Reset();
this.TaskForm.PopulateFields((Task) this.ListBox_Tasks.SelectedItem);
this.TaskForm.ShowDialog();
}
// Button_MoveUpTask
private void Button_MoveUpTask_Click(object sender, EventArgs e)
{
int selectedIndex = this.ListBox_Tasks.SelectedIndex;
if (selectedIndex > 0)
{
this.ListBox_Tasks.Items.Insert(selectedIndex - 1, this.ListBox_Tasks.Items[selectedIndex]);
this.ListBox_Tasks.Items.RemoveAt(selectedIndex + 1);
this.ListBox_Tasks.SelectedIndex = selectedIndex - 1;
}
}
// Button_MoveDownTask
private void Button_MoveDownTask_Click(object sender, EventArgs e)
{
int selectedIndex = this.ListBox_Tasks.SelectedIndex;
if (selectedIndex < this.ListBox_Tasks.Items.Count - 1 & selectedIndex != -1)
{
this.ListBox_Tasks.Items.Insert(selectedIndex + 2, this.ListBox_Tasks.Items[selectedIndex]);
this.ListBox_Tasks.Items.RemoveAt(selectedIndex);
this.ListBox_Tasks.SelectedIndex = selectedIndex + 1;
}
}
// Button_DeleteTask
private void Button_DeleteTask_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show(
"Are you sure you want to delete this task?", "",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
int index = this.ListBox_Tasks.SelectedIndex;
this.ListBox_Tasks.ClearSelected();
this.ListBox_Tasks.Items.RemoveAt(index);
if (index == this.ListBox_Tasks.Items.Count) index--;
if (this.ListBox_Tasks.Items.Count != 0)
this.ListBox_Tasks.SelectedIndex = index;
}
}
// TaskForm
private void TaskForm_Shown(object sender, EventArgs e) => this.TaskForm.TextBox_TaskName.Focus();
private void TaskForm_CreateTask(Task t)
{
this.ListBox_Tasks.Items.Add(t);
this.ListBox_Tasks.SelectedIndex = this.ListBox_Tasks.Items.Count - 1;
this.TaskForm.Hide();
}
private void TaskForm_UpdateTask(Task t)
{
this.ListBox_Tasks.Items[this.ListBox_Tasks.SelectedIndex] = t;
this.TaskForm.Hide();
}
private void TaskForm_FormClosing(object sender, FormClosingEventArgs e)
{
bool showDialog = false;
if(this.TaskForm.NoClosePrompt == false)
{
if (!string.IsNullOrWhiteSpace(this.TaskForm.TextBox_TaskName.Text) ||
this.TaskForm.NumericUpDown_Hours.Value != 0 ||
this.TaskForm.NumericUpDown_Minutes.Value != 0
|| this.TaskForm.NumericUpDown_Seconds.Value != 0)
showDialog = true;
}
else
{
this.TaskForm.NoClosePrompt = false;
}
if (showDialog == true)
{
DialogResult result = MessageBox.Show(
"You have unsaved fields. Are you sure to cancel?", "",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
e.Cancel = (result == DialogResult.No);
}
else
{
e.Cancel = false;
}
}
#endregion
public RoutineForm()
{
#region Attributes
this.NoClosePrompt = false;
#endregion
#region Form Properties
this.Size = new Size(480, 440);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.StartPosition = FormStartPosition.CenterParent;
this.TaskForm = new TaskForm();
this.TaskForm.Shown += new EventHandler(this.TaskForm_Shown);
this.TaskForm.CreateTask += this.TaskForm_CreateTask;
this.TaskForm.UpdateTask += this.TaskForm_UpdateTask;
this.TaskForm.FormClosing += this.TaskForm_FormClosing;
#endregion
///
/// Root Controls
///
#region Root Controls
// Label_RoutineName
this.Label_RoutineName = new Label
{
Location = new Point(10, 15),
Text = "Routine Name : "
};
this.Controls.Add(this.Label_RoutineName);
// TextBox_RoutineName;
this.TextBox_RoutineName = new TextBox
{
Location = new Point(120, 10),
Size = new Size(240, 30)
};
this.Controls.Add(this.TextBox_RoutineName);
// GroupBox_Tasks
this.GroupBox_Tasks = new GroupBox
{
Location = new Point(10, 50),
Size = new Size(445, 295),
Text = "Tasks",
};
this.Controls.Add(this.GroupBox_Tasks);
// Button_Cancel;
this.Button_Cancel = new Button
{
Location = new Point(220, 360),
Size = new Size(80, 30),
Text = "Cancel",
};
this.Controls.Add(this.Button_Cancel);
this.Button_Cancel.Click += new EventHandler(this.Button_Cancel_Click);
// Button_CreateRoutine
this.Button_CreateRoutine = new Button
{
Location = new Point(305, 360),
Size = new Size(150, 30),
Text = "Create Routine",
};
this.Controls.Add(this.Button_CreateRoutine);
this.Button_CreateRoutine.Click += new EventHandler(this.Button_CreateRoutine_Click);
// Button_UpdateRoutine
this.Button_UpdateRoutine = new Button
{
Location = new Point(305, 360),
Size = new Size(150, 30),
Text = "Update Routine",
};
this.Controls.Add(this.Button_UpdateRoutine);
this.Button_UpdateRoutine.Click += new EventHandler(this.Button_UpdateRoutine_Click);
#endregion
///
/// GroupBox_Tasks
///
#region GroupBox_Tasks
// Label_TasksHeader
this.Label_TasksHeader = new Label
{
Location = new Point(15, 30),
Size = new Size(300, 15),
Text = string.Format(this.TaskToStringDetails, "Duration", "Task Name [Color]"),
};
this.GroupBox_Tasks.Controls.Add(this.Label_TasksHeader);
// ListBox_Tasks;
this.ListBox_Tasks = new ListBox
{
Location = new Point(15, 50),
Size = new Size(300, 230),
};
this.GroupBox_Tasks.Controls.Add(this.ListBox_Tasks);
this.ListBox_Tasks.SelectedIndexChanged += new EventHandler(this.ListBox_Tasks_SelectedIndexChanged);
// Button_CreateTask
this.Button_CreateTask = new Button
{
Location = new Point(320, 49),
Size = new Size(110, 30),
Text = "Create",
};
this.GroupBox_Tasks.Controls.Add(this.Button_CreateTask);
this.Button_CreateTask.Click += new EventHandler(this.Button_CreateTask_Click);
// Button_EditTask
this.Button_EditTask = new Button
{
Location = new Point(320, 84),
Size = new Size(110, 30),
Text = "Edit",
};
this.GroupBox_Tasks.Controls.Add(this.Button_EditTask);
this.Button_EditTask.Click += new EventHandler(this.Button_EditTask_Click);
// Button_MoveUpTask
this.Button_MoveUpTask = new Button
{
Location = new Point(320, 119),
Size = new Size(110, 30),
Text = "Move Up",
};
this.GroupBox_Tasks.Controls.Add(this.Button_MoveUpTask);
this.Button_MoveUpTask.Click += new EventHandler(this.Button_MoveUpTask_Click);
// Button_MoveDownTask
this.Button_MoveDownTask = new Button
{
Location = new Point(320, 154),
Size = new Size(110, 30),
Text = "Move Down",
};
this.GroupBox_Tasks.Controls.Add(this.Button_MoveDownTask);
this.Button_MoveDownTask.Click += new EventHandler(this.Button_MoveDownTask_Click);
// Button_DeleteTask
this.Button_DeleteTask = new Button
{
Location = new Point(320, 189),
Size = new Size(110, 30),
Text = "Delete",
};
this.GroupBox_Tasks.Controls.Add(this.Button_DeleteTask);
this.Button_DeleteTask.Click += new EventHandler(this.Button_DeleteTask_Click);
#endregion
#region Test
#endregion
//InitializeComponent();
}
#region Methods
public void Reset()
{
// Clear Routine Name
this.TextBox_RoutineName.Clear();
// Clear ListBox_Tasks & Disable List Buttons (Except Create)
this.ListBox_Tasks.Items.Clear();
this.Button_EditTask.Enabled = false;
this.Button_MoveUpTask.Enabled = false;
this.Button_MoveDownTask.Enabled = false;
this.Button_DeleteTask.Enabled = false;
// Hide Button_UpdateRoutine
this.Button_UpdateRoutine.Hide();
this.Button_CreateRoutine.Show();
this.NoClosePrompt = false;
GC.Collect();
}
public void PopulateFields(Routine r)
{
this.TextBox_RoutineName.Text = r.Name;
foreach (Task t in r.Tasks)
ListBox_Tasks.Items.Add(t);
this.Button_CreateRoutine.Hide();
this.Button_UpdateRoutine.Show();
}
#endregion
}
}