-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathBCityControl.cs
301 lines (295 loc) · 9.71 KB
/
BCityControl.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BMap.NET.WindowsForm.FunctionalControls;
namespace BMap.NET.WindowsForm
{
/// <summary>
/// 城市切换控件
/// </summary>
partial class BCityControl : UserControl
{
public event SelectedCityChangedEventHandler SelectedCityChanged;
private string _currentCity = "";
/// <summary>
/// 当前城市
/// </summary>
public string CurrentCity
{
get
{
return _currentCity;
}
set
{
_currentCity = value;
lbl_current_city.Text = "当前城市:" + _currentCity;
}
}
/// <summary>
/// 构造方法
/// </summary>
public BCityControl()
{
InitializeComponent();
}
/// <summary>
/// 控件加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BCityControl_Load(object sender, EventArgs e)
{
LoadCities();
Init();
}
/// <summary>
/// 初始化城市列表
/// </summary>
private void LoadCities()
{
Dictionary<string, List<string>> data_pinyin = new Dictionary<string, List<string>>(); //拼音数据源
CityNode root = new CityNode("中国", "1", null, "0"); //组织结构数据源
((Action)delegate()
{
string areas = Properties.BMap.baidu_citys;
string[] cities = areas.Split(new string[] { "\r\n" }, StringSplitOptions.None);
foreach (string city in cities)
{
string[] items = city.Split('|');
if (items.Length == 4)
{
if (items[3] != "0" && items[3] != "1") //忽略国家 省份
{
string pinyin = GetSpellCodeAt(items[0][0].ToString()); //首汉字拼音
if (!data_pinyin.ContainsKey(pinyin))
{
data_pinyin.Add(pinyin, new List<string> { items[0] });
}
data_pinyin[pinyin].Add(items[0]);
}
if (items[3] != "0") //忽略国家
{
if (items[2] == root.CityCode) //省份、直辖市
{
if (root.Nexts == null)
{
root.Nexts = new List<CityNode>();
}
root.Nexts.Add(new CityNode(items[0], items[1], items[2], items[3]));
}
else //地级市 直辖市中的县区
{
foreach (CityNode province in root.Nexts)
{
if (province.CityCode == items[2])
{
if (province.Nexts == null)
{
province.Nexts = new List<CityNode>();
}
province.Nexts.Add(new CityNode(items[0], items[1], items[2], items[3]));
break;
}
}
}
}
}
}
this.Invoke((Action)delegate()
{
cityList1.DataByOrganization = root;
cityList1.DataByPinyin = data_pinyin;
cityList1.Mode = CityListMode.Pinyin;
cityList1.RefreshList();
});
}).BeginInvoke(null, null);
}
/// <summary>
/// 获取汉字拼音开头字母
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
private static string GetSpellCodeAt(string s)
{
long iCnChar;
byte[] ZW = System.Text.Encoding.Default.GetBytes(s);
if (ZW.Length == 1)
{
return s.ToUpper();
}
int i1 = (short)ZW[0];
int i2 = (short)ZW[1];
iCnChar = i1 * 256 + i2;
if ((iCnChar >= 45217) && (iCnChar <= 45252))
{
return "A";
}
else if ((iCnChar >= 45253) && (iCnChar <= 45760))
{
return "B";
}
else if ((iCnChar >= 45761) && (iCnChar <= 46317))
{
return "C";
}
else if ((iCnChar >= 46318) && (iCnChar <= 46825))
{
return "D";
}
else if ((iCnChar >= 46826) && (iCnChar <= 47009))
{
return "E";
}
else if ((iCnChar >= 47010) && (iCnChar <= 47296))
{
return "F";
}
else if ((iCnChar >= 47297) && (iCnChar <= 47613))
{
return "G";
}
else if ((iCnChar >= 47614) && (iCnChar <= 48118))
{
return "H";
}
else if ((iCnChar >= 48119) && (iCnChar <= 49061))
{
return "J";
}
else if ((iCnChar >= 49062) && (iCnChar <= 49323))
{
return "K";
}
else if ((iCnChar >= 49324) && (iCnChar <= 49895))
{
return "L";
}
else if ((iCnChar >= 49896) && (iCnChar <= 50370))
{
return "M";
}
else if ((iCnChar >= 50371) && (iCnChar <= 50613))
{
return "N";
}
else if ((iCnChar >= 50614) && (iCnChar <= 50621))
{
return "O";
}
else if ((iCnChar >= 50622) && (iCnChar <= 50905))
{
return "P";
}
else if ((iCnChar >= 50906) && (iCnChar <= 51386))
{
return "Q";
}
else if ((iCnChar >= 51387) && (iCnChar <= 51445))
{
return "R";
}
else if ((iCnChar >= 51446) && (iCnChar <= 52217))
{
return "S";
}
else if ((iCnChar >= 52218) && (iCnChar <= 52697))
{
return "T";
}
else if ((iCnChar >= 52698) && (iCnChar <= 52979))
{
return "W";
}
else if ((iCnChar >= 52980) && (iCnChar <= 53640))
{
return "X";
}
else if ((iCnChar >= 53689) && (iCnChar <= 54480))
{
return "Y";
}
else if ((iCnChar >= 54481) && (iCnChar <= 55289))
{
return "Z";
}
else return ("?");
}
/// <summary>
/// 初始化工作
/// </summary>
private void Init()
{
string[] hot_cities = new string[] { "北京", "天津", "上海", "重庆", "广州", "武汉", "成都", "南京", "深圳", "合肥", "厦门" };
foreach (string city in hot_cities)
{
LinkLabel lkb = new LinkLabel();
lkb.Text = city;
lkb.AutoSize = true;
lkb.VisitedLinkColor = Color.Blue;
lkb.Click += new EventHandler(lkb_Click);
flp_hot_cities.Controls.Add(lkb);
}
}
/// <summary>
/// 点击热门城市
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void lkb_Click(object sender, EventArgs e)
{
CurrentCity = (sender as LinkLabel).Text;
if (SelectedCityChanged != null)
{
SelectedCityChanged((sender as LinkLabel).Text);
}
}
/// <summary>
/// 排列方式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void rdo_CheckedChanged(object sender, EventArgs e)
{
if (rdo_by_pinyin.Checked)
{
cityList1.Mode = CityListMode.Pinyin;
}
else
{
cityList1.Mode = CityListMode.Organization;
}
rdo_by_pinyin.Enabled = false;
rdo_by_province.Enabled = false;
cityList1.RefreshList();
rdo_by_province.Enabled = true;
rdo_by_pinyin.Enabled = true;
}
/// <summary>
/// 选择城市变化
/// </summary>
/// <param name="cityName"></param>
private void cityList1_SelectedCityChanged(string cityName)
{
CurrentCity = cityName;
if (SelectedCityChanged != null)
{
SelectedCityChanged(cityName);
}
}
/// <summary>
/// 关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pic_close_Click(object sender, EventArgs e)
{
Visible = false;
}
}
}