-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
120 lines (111 loc) · 3.94 KB
/
MainForm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace ClockIn
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
DataAccess = new DataAccess();
GetClockStatus();
UpdateFormStatus();
}
private DataAccess DataAccess {get;set;}
private Log Log { get; set; }
private string ClockStatus { get; set; }
private DateTime LastEventTime { get; set; }
private void viewLogToolStripMenuItem_Click(object sender, EventArgs e)
{
Log = new Log(DataAccess);
Log.Show();
}
private void GetClockStatus()
{
//indexoutofrange
try
{
DataAccess.FillDataSet();
//get the last event type, ordered by dt_created
string status = DataAccess.DataSet.Tables["ClockEvents"].Rows[0].ItemArray[1].ToString();
if (status == "Out" || status == "In" || status == "BreakStart" || status == "BreakEnd")
{
ClockStatus = status;
LastEventTime = DateTime.Parse(DataAccess.DataSet.Tables["ClockEvents"].Rows[0].ItemArray[2].ToString());
}
else
{
MessageBox.Show("Invalid Clock status: " + status);
}
}
catch (IndexOutOfRangeException)
{
ClockStatus = "Out";
}
}//GetClockStatus()
private void UpdateFormStatus()
{
GetClockStatus();
//Changes the text and form controls based on the ClockStatus
if (ClockStatus == "Out")
{
btnClock.Enabled = true;
btnBreak.Enabled = false;
btnClock.Text = "Clock In";
lblClockStatus.Text = "You are clocked out.";
}
else if (ClockStatus == "In")
{
btnClock.Enabled = true;
btnBreak.Enabled = true;
btnClock.Text = "Clock Out";
lblClockStatus.Text = "You clocked in at " + LastEventTime;
}
else if (ClockStatus == "BreakStart")
{
btnClock.Enabled = false;
btnBreak.Enabled = true;
lblClockStatus.Text = "You took a break at " + LastEventTime;
btnBreak.Text = "End Break";
}
else if (ClockStatus == "BreakEnd")
{
btnClock.Enabled = true;
btnBreak.Enabled = true;
lblClockStatus.Text = "You are clocked in";
btnBreak.Text = "Start Break";
}
}//UpdateFormStatus()
private void btnClock_Click(object sender, EventArgs e)
{
string eventType;
if (ClockStatus == "Out") { eventType = "In"; }
else { eventType = "Out"; }
DataAccess.InsertEvent(new ClockEvent(eventType, DateTime.Now));
UpdateFormStatus();
}//Click btnClock()
private void btnBreak_Click(object sender, EventArgs e)
{
string eventType;
//Button won't be enabled unless the user can start or end a break
//(i.e. not clocked out)
eventType = (ClockStatus == "BreakStart") ? (eventType = "BreakEnd") : (eventType = "BreakStart");
DataAccess.InsertEvent(new ClockEvent(eventType, DateTime.Now));
UpdateFormStatus();
}
private void clearEntriesToolStripMenuItem_Click(object sender, EventArgs e)
{
DataAccess.ClearTable();
GetClockStatus();
UpdateFormStatus();
}
}
}