-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathForm1.cs
More file actions
237 lines (193 loc) · 7.85 KB
/
Form1.cs
File metadata and controls
237 lines (193 loc) · 7.85 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
using System;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
namespace ThingsToDoPRO
{
public partial class Form1 : Form
{
// Declare and initialize filePath correctly
private readonly string filePath;
private readonly string imagePath;
public Form1()
{
InitializeComponent();
// Set filePath to the path in the Documents folder
filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "tasks.txt");
imagePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "background.txt");
// Load tasks and background when the form loads
LoadTasksFromFile();
LoadBackgroundImage();
// Start the timer to check due tasks
dueDateTimer.Start();
}
private void CheckDueTasks(object sender, EventArgs e)
{
DateTime now = DateTime.Now;
foreach (ListViewItem item in taskList.Items)
{
// Split the task text to get the due date and time
string[] taskParts = item.Text.Split(new[] { " | Due " }, StringSplitOptions.None);
if (taskParts.Length == 2)
{
string dueDateString = taskParts[1];
// Parse the due date and time
if (DateTime.TryParse(dueDateString, out DateTime dueDateTime))
{
// Check if the task is due now or overdue and unchecked
if (!item.Checked && dueDateTime <= now)
{
// Show notification for the due task
notifyIcon1.BalloonTipTitle = "Task Due Reminder";
notifyIcon1.BalloonTipText = $"The task \"{taskParts[0]}\" is due now!";
notifyIcon1.ShowBalloonTip(30000); // Show the notification for 30 seconds
}
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
string task = textBox1.Text.Trim();
string date = addDueDate.Value.ToShortDateString();
string time = addDueTime.Value.ToShortTimeString();
if (!string.IsNullOrEmpty(task) && (!string.IsNullOrEmpty(date)))
{
// Combine the date and time for saving and displaying
string dueDateTime = $"{date} {time}";
taskList.Items.Add($"{task} | Due {dueDateTime}");
textBox1.Clear();
// Save tasks after adding a new one
SaveTasksToFile();
}
else
{
MessageBox.Show("Please enter a task and due date.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void clearButton_Click(object sender, EventArgs e)
{
taskList.Items.Clear();
// Save the empty list to the file
SaveTasksToFile();
}
private void deleteSelected_Click(object sender, EventArgs e)
{
if (taskList.SelectedItems.Count > 0)
{
taskList.Items.Remove(taskList.SelectedItems[0]);
// Save tasks after deleting one
SaveTasksToFile();
}
else
{
MessageBox.Show("Please select a task to delete.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
// Optional: Handle text changes if needed
}
private void taskList_SelectedIndexChanged(object sender, EventArgs e)
{
// Optional: Handle selection changes if needed
}
private void listView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete && taskList.SelectedItems.Count > 0)
{
taskList.Items.Remove(taskList.SelectedItems[0]);
SaveTasksToFile();
}
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
string task = textBox1.Text.Trim();
if (!string.IsNullOrEmpty(task))
{
taskList.Items.Add(task);
textBox1.Clear();
SaveTasksToFile();
}
}
}
private void SaveTasksToFile()
{
using (StreamWriter writer = new StreamWriter(filePath))
{
foreach (ListViewItem item in taskList.Items)
{
string[] taskParts = item.Text.Split('|');
writer.WriteLine($"{taskParts[0].Trim()}|{item.Checked}|{taskParts[1].Trim()}"); // Save task, status, and due date/time
}
}
}
private void LoadTasksFromFile()
{
try
{
taskList.Items.Clear();
string[] lines = File.ReadAllLines(filePath);
foreach (string line in lines)
{
string[] parts = line.Split('|');
if (parts.Length == 3)
{
ListViewItem item = new ListViewItem(parts[0].Trim())
{
Checked = bool.Parse(parts[1].Trim())
};
item.SubItems.Add($"| Due {parts[2].Trim()}");
taskList.Items.Add(item);
}
}
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred loading the task list from the file: {ex.Message}", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void changeBackground_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string selectedImagePath = openFileDialog1.FileName;
try
{
this.BackgroundImage = Image.FromFile(selectedImagePath);
this.BackgroundImageLayout = ImageLayout.Stretch;
// Save the path of the selected image
File.WriteAllText(imagePath, selectedImagePath);
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred setting the background image: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void LoadBackgroundImage()
{
try
{
if (File.Exists(imagePath))
{
string loadedImagePath = File.ReadAllText(imagePath);
this.BackgroundImage = Image.FromFile(loadedImagePath);
this.BackgroundImageLayout = ImageLayout.Stretch;
}
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred loading the background image: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
SaveTasksToFile();
// Dispose of NotifyIcon
notifyIcon1.Dispose();
}
}
}