-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnterTaskForm.cs
More file actions
115 lines (94 loc) · 2.74 KB
/
EnterTaskForm.cs
File metadata and controls
115 lines (94 loc) · 2.74 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
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using TimeLogger.Properties;
namespace TimeLogger
{
public partial class EnterTaskForm : Form
{
private readonly DateTime _formShown = DateTime.Now;
public EnterTaskForm()
{
InitializeComponent();
}
private void btnOK_Click(object sender, EventArgs e)
{
LogAndClose();
}
private void LogAndClose()
{
try
{
string activity;
if (rbFromList.Checked)
{
activity = lbActivities.Text;
}
else
{
activity = cbCustomActivity.Text;
SetLastActivity(activity);
}
if (string.IsNullOrEmpty(activity))
{
MessageBox.Show("You should specify your activity.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
FileHelper.CreateLogFileIfNotExists();
File.AppendAllText(
Settings.Default.LogFilePath,
string.Format("{0}\t{1}{2}", _formShown, activity, Environment.NewLine),
new UTF8Encoding(true));
}
catch (Exception ex)
{
MessageBox.Show(string.Format(
"Exception occurred during logging to the file.{0}{0}Exception message:{0}{1}",
Environment.NewLine,
ex.Message));
}
Close();
}
private static void SetLastActivity(string activity)
{
const int lastActivitiesToKeep = 5;
int indexInRecent = Settings.Default.LastEnteredActivities.IndexOf(activity);
bool recent = indexInRecent >= 0;
if(!recent && Settings.Default.LastEnteredActivities.Count < lastActivitiesToKeep)
Settings.Default.LastEnteredActivities.Add("");
int shiftFrom = recent
? indexInRecent
: Math.Min(lastActivitiesToKeep - 1, Settings.Default.LastEnteredActivities.Count - 1);
for (int i = shiftFrom; i > 0; i--)
{
Settings.Default.LastEnteredActivities[i] = Settings.Default.LastEnteredActivities[i - 1];
}
Settings.Default.LastEnteredActivities[0] = activity;
Settings.Default.Save();
}
private void cbCustomActivity_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
LogAndClose();
}
private void EnterTaskForm_Load(object sender, EventArgs e)
{
Text = string.Format("Enter your current ({0}) activity", _formShown.ToShortTimeString());
if (!Settings.Default.AllowNotSpecifyAction)
ControlBox = false;
foreach (string activity in Settings.Default.LastEnteredActivities)
cbCustomActivity.Items.Add(activity);
}
private void rbCustom_CheckedChanged(object sender, EventArgs e)
{
cbCustomActivity.Enabled = rbCustom.Checked;
}
private void lbActivities_DoubleClick(object sender, EventArgs e)
{
int itemIndex = lbActivities.IndexFromPoint(lbActivities.PointToClient(Cursor.Position));
if (itemIndex == ListBox.NoMatches) return;
LogAndClose();
}
}
}