-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCountdownLabel.cs
111 lines (94 loc) · 2.5 KB
/
CountdownLabel.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace KCB2
{
/// <summary>
/// 自動的にカウントダウンされるタイマ
/// </summary>
public class CountdownLabel : Label
{
DateTime _finish;
/// <summary>
/// 終了時刻
/// </summary>
public DateTime FinishTime
{
get { return _finish; }
set
{
_finish = value;
_valid = true;
ShowTime = false;
_timer.Enabled = true;
}
}
Timer _timer = new Timer();
public CountdownLabel()
{
_timer.Interval = 1000;
_timer.Enabled = false;
_timer.Tick += new EventHandler(_timer_Tick);
// FinishTime = DateTime.Now;
Click += new EventHandler(CountdownLabel_Click);
Text = "N/A";
}
bool _valid = false;
/// <summary>
/// 有効かどうか
/// </summary>
public bool Valid
{
get { return _valid; }
set
{
_valid = value;
if (!_valid)
Text = "N/A";
}
}
bool ShowTime = false;
const string _notAvailMsg = "N/A";
void _timer_Tick(object sender, EventArgs e)
{
if (!_valid)
{
Text = _notAvailMsg;
return;
}
TimeSpan diff = _finish - DateTime.Now;
if (diff.TotalMilliseconds < 0)
Text = "00:00:00";
else
{
if (diff.TotalDays >= 1.0)
{
int hours = (int)Math.Floor(diff.TotalHours);
Text = string.Format("{0:D2}:{1:D2}:{2:D2}", hours, diff.Minutes, diff.Seconds);
}
else
Text = diff.ToString(@"hh\:mm\:ss");
}
}
void CountdownLabel_Click(object sender, EventArgs e)
{
if (!_valid)
{
Text = _notAvailMsg;
return;
}
ShowTime = !ShowTime;
_timer.Enabled = ShowTime;
if (!ShowTime)
{
Text = _finish.ToString();
}
else
{
_timer_Tick(null, null);
}
}
}
}