-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrmMain.cs
192 lines (179 loc) · 4.39 KB
/
frmMain.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
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
#region
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
#endregion
namespace TimeTranslator
{
public partial class frmMain : Form
{
private const string TextBoxDefault = "hh:mm";
private List<TimeZone> listDefault, listCustom;
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
List<List<TimeZone>> list = TimeZone.GetTimeZoneFromXml("TimeZone.xml");
listDefault = list[0];
listCustom = list[1];
foreach (TimeZone zone in listDefault)
cbTimeZone.Items.Add(zone);
foreach (TimeZone zone in listCustom)
cbCustomZone.Items.Add(zone);
cbTransType.SelectedIndex = cbTimeZone.SelectedIndex = 0;
if (cbCustomZone.Items.Count != 0)
cbCustomZone.SelectedIndex = 0;
cbTimeType.SelectedIndex = 0;
dtpMain.Value = DateTime.Today;
cbTimeZone.Focus();
}
private void cbTimeZone_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbTimeZone.SelectedIndex == 1)
{
flpMain.SetFlowBreak(cbTimeZone, false);
cbCustomZone.Visible = true;
}
else
{
flpMain.SetFlowBreak(cbTimeZone, true);
cbCustomZone.Visible = false;
}
}
private void cbTimeType_SelectedIndexChanged(object sender, EventArgs e)
{
dtpMain.Visible = cbTimeType.SelectedIndex == 0;
}
private void txtTime_Enter(object sender, EventArgs e)
{
if (txtTime.Text == TextBoxDefault)
{
txtTime.Text = string.Empty;
txtTime.ForeColor = SystemColors.WindowText;
}
else
{
txtTime.SelectAll();
}
}
private void txtTime_Leave(object sender, EventArgs e)
{
if (txtTime.Text.Length == 0)
{
txtTime.Text = TextBoxDefault;
txtTime.ForeColor = SystemColors.GrayText;
}
}
private void btnTrans_Click(object sender, EventArgs e)
{
bool bContinue = true;
if (cbTimeZone.SelectedIndex > 1)
{
cbTimeZone.BackColor = SystemColors.Window;
}
else if (cbTimeZone.SelectedIndex == 1)
{
cbTimeZone.BackColor = SystemColors.Window;
if (cbCustomZone.SelectedIndex < 0)
{
cbCustomZone.BackColor = Color.Yellow;
bContinue = false;
}
else
{
cbCustomZone.BackColor = SystemColors.Window;
}
}
else
{
cbTimeZone.BackColor = Color.Yellow;
bContinue = false;
}
int hour = 0, minute = 0;
if (txtTime.Text == TextBoxDefault)
{
txtTime.BackColor = Color.Yellow;
bContinue = false;
}
else
{
Match match = Regex.Match(txtTime.Text, @"^(\d+):(\d+)$");
if (match.Success)
{
hour = int.Parse(match.Groups[1].Value);
minute = int.Parse(match.Groups[2].Value);
txtTime.BackColor = SystemColors.Window;
if (hour > 23 || minute > 59)
{
txtTime.BackColor = Color.Red;
bContinue = false;
}
}
else
{
txtTime.BackColor = Color.Red;
bContinue = false;
}
}
if (bContinue)
{
TimeZone zone = (TimeZone)(cbTimeZone.SelectedIndex > 1 ? cbTimeZone.SelectedItem : cbCustomZone.SelectedItem);
TimeSpan diff;
string text;
if (cbTransType.SelectedIndex == 0)
{
// 转换至
diff = TimeZone.GetDiffTimeSpan(TimeZone.defaultZone, zone);
text = "当地时间:";
}
else
{
// 转换自
diff = TimeZone.GetDiffTimeSpan(zone, TimeZone.defaultZone);
text = "本地时间:";
}
if (cbTimeType.SelectedIndex == 0)
{
TimeSpan time = new TimeSpan(hour, minute, 0);
text += (new DateTime(dtpMain.Value.Ticks + time.Ticks)).Add(diff).ToString("yyyy-MM-dd HH:mm");
}
else
{
TimeSpan time = new TimeSpan(1, hour, minute, 0);
DateTime t1 = new DateTime(time.Ticks);
DateTime t2 = t1.Add(diff);
text = t2.ToString("HH:mm");
if (t1.Date < t2.Date)
text += "(后一天)";
else if (t1.Date > t2.Date)
text += "(前一天)";
}
lblResult.Text = text;
}
}
private void btnNow_Click(object sender, EventArgs e)
{
dtpMain.Value = DateTime.Today;
txtTime.Text = DateTime.Now.ToString("HH:mm");
txtTime.ForeColor = SystemColors.WindowText;
}
private void cbTransType_SelectedIndexChanged(object sender, EventArgs e)
{
string text = "";
switch (cbTransType.SelectedIndex)
{
case 0:
text = "转换为当地时间";
break;
case 1:
text = "转换为本地时间";
break;
}
lblTips.Text = text;
}
}
}