CultureDemoForm.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:19k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Globalization;
  8. namespace CultureDemo
  9. {
  10. /// <summary>
  11. /// Summary description for Form1.
  12. /// </summary>
  13. public class CultureDemoForm : System.Windows.Forms.Form
  14. {
  15. private System.Windows.Forms.TreeView treeCultures;
  16. private System.Windows.Forms.Splitter splitter1;
  17. private System.Windows.Forms.Panel panel1;
  18. private System.Windows.Forms.Label label1;
  19. private System.Windows.Forms.Label label2;
  20. private System.Windows.Forms.Label label3;
  21. private System.Windows.Forms.Label label4;
  22. private System.Windows.Forms.Label label5;
  23. private System.Windows.Forms.GroupBox groupSamples;
  24. private System.Windows.Forms.Label label6;
  25. private System.Windows.Forms.Label label7;
  26. private System.Windows.Forms.Label label8;
  27. private System.Windows.Forms.GroupBox groupRegionInformation;
  28. private System.Windows.Forms.Label label9;
  29. private System.Windows.Forms.Label label10;
  30. private System.Windows.Forms.TextBox textName;
  31. private System.Windows.Forms.TextBox textEnglishName;
  32. private System.Windows.Forms.TextBox textNativeName;
  33. private System.Windows.Forms.CheckBox checkIsNeutral;
  34. private System.Windows.Forms.TextBox textCalendar;
  35. private System.Windows.Forms.ListBox listCalendars;
  36. private System.Windows.Forms.TextBox textSampleNumber;
  37. private System.Windows.Forms.TextBox textSampleDate;
  38. private System.Windows.Forms.TextBox textSampleTime;
  39. private System.Windows.Forms.CheckBox checkIsMetric;
  40. private System.Windows.Forms.TextBox textCurrency;
  41. private System.Windows.Forms.TextBox textRegionName;
  42. private System.Windows.Forms.TextBox textCurrencyName;
  43. private System.Windows.Forms.TextBox textBox1;
  44. private System.Windows.Forms.TextBox textBox2;
  45. /// <summary>
  46. /// Required designer variable.
  47. /// </summary>
  48. private System.ComponentModel.Container components = null;
  49. public CultureDemoForm()
  50. {
  51. //
  52. // Required for Windows Form Designer support
  53. //
  54. InitializeComponent();
  55. AddCulturesToTree();
  56. }
  57. /// <summary>
  58. /// Clean up any resources being used.
  59. /// </summary>
  60. protected override void Dispose( bool disposing )
  61. {
  62. if( disposing )
  63. {
  64. if (components != null) 
  65. {
  66. components.Dispose();
  67. }
  68. }
  69. base.Dispose( disposing );
  70. }
  71. #region Windows Form Designer generated code
  72. /// <summary>
  73. /// Required method for Designer support - do not modify
  74. /// the contents of this method with the code editor.
  75. /// </summary>
  76. private void InitializeComponent()
  77. {
  78. this.treeCultures = new System.Windows.Forms.TreeView();
  79. this.splitter1 = new System.Windows.Forms.Splitter();
  80. this.panel1 = new System.Windows.Forms.Panel();
  81. this.groupRegionInformation = new System.Windows.Forms.GroupBox();
  82. this.label9 = new System.Windows.Forms.Label();
  83. this.groupSamples = new System.Windows.Forms.GroupBox();
  84. this.label8 = new System.Windows.Forms.Label();
  85. this.label7 = new System.Windows.Forms.Label();
  86. this.label6 = new System.Windows.Forms.Label();
  87. this.label5 = new System.Windows.Forms.Label();
  88. this.label4 = new System.Windows.Forms.Label();
  89. this.label3 = new System.Windows.Forms.Label();
  90. this.label2 = new System.Windows.Forms.Label();
  91. this.label1 = new System.Windows.Forms.Label();
  92. this.label10 = new System.Windows.Forms.Label();
  93. this.textName = new System.Windows.Forms.TextBox();
  94. this.textEnglishName = new System.Windows.Forms.TextBox();
  95. this.textNativeName = new System.Windows.Forms.TextBox();
  96. this.checkIsNeutral = new System.Windows.Forms.CheckBox();
  97. this.textCalendar = new System.Windows.Forms.TextBox();
  98. this.listCalendars = new System.Windows.Forms.ListBox();
  99. this.textSampleNumber = new System.Windows.Forms.TextBox();
  100. this.textSampleDate = new System.Windows.Forms.TextBox();
  101. this.textSampleTime = new System.Windows.Forms.TextBox();
  102. this.textRegionName = new System.Windows.Forms.TextBox();
  103. this.textCurrency = new System.Windows.Forms.TextBox();
  104. this.textCurrencyName = new System.Windows.Forms.TextBox();
  105. this.checkIsMetric = new System.Windows.Forms.CheckBox();
  106. this.textBox1 = new System.Windows.Forms.TextBox();
  107. this.textBox2 = new System.Windows.Forms.TextBox();
  108. this.panel1.SuspendLayout();
  109. this.groupRegionInformation.SuspendLayout();
  110. this.groupSamples.SuspendLayout();
  111. this.SuspendLayout();
  112. // 
  113. // treeCultures
  114. // 
  115. this.treeCultures.Dock = System.Windows.Forms.DockStyle.Left;
  116. this.treeCultures.ImageIndex = -1;
  117. this.treeCultures.Location = new System.Drawing.Point(0, 0);
  118. this.treeCultures.Name = "treeCultures";
  119. this.treeCultures.SelectedImageIndex = -1;
  120. this.treeCultures.Size = new System.Drawing.Size(168, 454);
  121. this.treeCultures.TabIndex = 0;
  122. this.treeCultures.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.OnSelectCulture);
  123. // 
  124. // splitter1
  125. // 
  126. this.splitter1.Location = new System.Drawing.Point(168, 0);
  127. this.splitter1.Name = "splitter1";
  128. this.splitter1.Size = new System.Drawing.Size(3, 454);
  129. this.splitter1.TabIndex = 1;
  130. this.splitter1.TabStop = false;
  131. // 
  132. // panel1
  133. // 
  134. this.panel1.Controls.Add(this.listCalendars);
  135. this.panel1.Controls.Add(this.textCalendar);
  136. this.panel1.Controls.Add(this.checkIsNeutral);
  137. this.panel1.Controls.Add(this.textNativeName);
  138. this.panel1.Controls.Add(this.textEnglishName);
  139. this.panel1.Controls.Add(this.textName);
  140. this.panel1.Controls.Add(this.groupRegionInformation);
  141. this.panel1.Controls.Add(this.groupSamples);
  142. this.panel1.Controls.Add(this.label5);
  143. this.panel1.Controls.Add(this.label4);
  144. this.panel1.Controls.Add(this.label3);
  145. this.panel1.Controls.Add(this.label2);
  146. this.panel1.Controls.Add(this.label1);
  147. this.panel1.Dock = System.Windows.Forms.DockStyle.Right;
  148. this.panel1.Location = new System.Drawing.Point(168, 0);
  149. this.panel1.Name = "panel1";
  150. this.panel1.Size = new System.Drawing.Size(304, 454);
  151. this.panel1.TabIndex = 1;
  152. // 
  153. // groupRegionInformation
  154. // 
  155. this.groupRegionInformation.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  156. this.groupRegionInformation.Controls.Add(this.checkIsMetric);
  157. this.groupRegionInformation.Controls.Add(this.textCurrencyName);
  158. this.groupRegionInformation.Controls.Add(this.textCurrency);
  159. this.groupRegionInformation.Controls.Add(this.textRegionName);
  160. this.groupRegionInformation.Controls.Add(this.label10);
  161. this.groupRegionInformation.Controls.Add(this.label9);
  162. this.groupRegionInformation.Location = new System.Drawing.Point(19, 320);
  163. this.groupRegionInformation.Name = "groupRegionInformation";
  164. this.groupRegionInformation.Size = new System.Drawing.Size(272, 120);
  165. this.groupRegionInformation.TabIndex = 12;
  166. this.groupRegionInformation.TabStop = false;
  167. this.groupRegionInformation.Text = "Region Information";
  168. // 
  169. // label9
  170. // 
  171. this.label9.Location = new System.Drawing.Point(16, 16);
  172. this.label9.Name = "label9";
  173. this.label9.TabIndex = 0;
  174. this.label9.Text = "Region:";
  175. // 
  176. // groupSamples
  177. // 
  178. this.groupSamples.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  179. this.groupSamples.Controls.Add(this.textSampleTime);
  180. this.groupSamples.Controls.Add(this.textSampleDate);
  181. this.groupSamples.Controls.Add(this.textSampleNumber);
  182. this.groupSamples.Controls.Add(this.label8);
  183. this.groupSamples.Controls.Add(this.label7);
  184. this.groupSamples.Controls.Add(this.label6);
  185. this.groupSamples.Location = new System.Drawing.Point(19, 200);
  186. this.groupSamples.Name = "groupSamples";
  187. this.groupSamples.Size = new System.Drawing.Size(272, 112);
  188. this.groupSamples.TabIndex = 11;
  189. this.groupSamples.TabStop = false;
  190. this.groupSamples.Text = "Samples";
  191. // 
  192. // label8
  193. // 
  194. this.label8.Location = new System.Drawing.Point(16, 72);
  195. this.label8.Name = "label8";
  196. this.label8.TabIndex = 4;
  197. this.label8.Text = "Time:";
  198. // 
  199. // label7
  200. // 
  201. this.label7.Location = new System.Drawing.Point(16, 48);
  202. this.label7.Name = "label7";
  203. this.label7.TabIndex = 2;
  204. this.label7.Text = "Full Date:";
  205. // 
  206. // label6
  207. // 
  208. this.label6.Location = new System.Drawing.Point(16, 24);
  209. this.label6.Name = "label6";
  210. this.label6.TabIndex = 0;
  211. this.label6.Text = "Number:";
  212. // 
  213. // label5
  214. // 
  215. this.label5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  216. this.label5.Location = new System.Drawing.Point(19, 152);
  217. this.label5.Name = "label5";
  218. this.label5.Size = new System.Drawing.Size(112, 23);
  219. this.label5.TabIndex = 9;
  220. this.label5.Text = "Optional Calendars:";
  221. // 
  222. // label4
  223. // 
  224. this.label4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  225. this.label4.Location = new System.Drawing.Point(19, 120);
  226. this.label4.Name = "label4";
  227. this.label4.TabIndex = 7;
  228. this.label4.Text = "Default Calendar:";
  229. // 
  230. // label3
  231. // 
  232. this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  233. this.label3.Location = new System.Drawing.Point(19, 88);
  234. this.label3.Name = "label3";
  235. this.label3.TabIndex = 5;
  236. this.label3.Text = "Native Name:";
  237. // 
  238. // label2
  239. // 
  240. this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  241. this.label2.Location = new System.Drawing.Point(19, 24);
  242. this.label2.Name = "label2";
  243. this.label2.TabIndex = 0;
  244. this.label2.Text = "Culture Name:";
  245. // 
  246. // label1
  247. // 
  248. this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  249. this.label1.Location = new System.Drawing.Point(19, 56);
  250. this.label1.Name = "label1";
  251. this.label1.TabIndex = 3;
  252. this.label1.Text = "English Name:";
  253. // 
  254. // label10
  255. // 
  256. this.label10.Location = new System.Drawing.Point(16, 48);
  257. this.label10.Name = "label10";
  258. this.label10.TabIndex = 2;
  259. this.label10.Text = "Currency:";
  260. // 
  261. // textName
  262. // 
  263. this.textName.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  264. this.textName.Location = new System.Drawing.Point(139, 24);
  265. this.textName.Name = "textName";
  266. this.textName.Size = new System.Drawing.Size(48, 20);
  267. this.textName.TabIndex = 1;
  268. this.textName.Text = "";
  269. // 
  270. // textEnglishName
  271. // 
  272. this.textEnglishName.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  273. this.textEnglishName.Location = new System.Drawing.Point(139, 56);
  274. this.textEnglishName.Name = "textEnglishName";
  275. this.textEnglishName.Size = new System.Drawing.Size(152, 20);
  276. this.textEnglishName.TabIndex = 4;
  277. this.textEnglishName.Text = "";
  278. // 
  279. // textNativeName
  280. // 
  281. this.textNativeName.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  282. this.textNativeName.Location = new System.Drawing.Point(139, 88);
  283. this.textNativeName.Name = "textNativeName";
  284. this.textNativeName.Size = new System.Drawing.Size(152, 20);
  285. this.textNativeName.TabIndex = 6;
  286. this.textNativeName.Text = "";
  287. // 
  288. // checkIsNeutral
  289. // 
  290. this.checkIsNeutral.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  291. this.checkIsNeutral.Location = new System.Drawing.Point(211, 24);
  292. this.checkIsNeutral.Name = "checkIsNeutral";
  293. this.checkIsNeutral.Size = new System.Drawing.Size(80, 24);
  294. this.checkIsNeutral.TabIndex = 2;
  295. this.checkIsNeutral.Text = "Is Neutral";
  296. // 
  297. // textCalendar
  298. // 
  299. this.textCalendar.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  300. this.textCalendar.Location = new System.Drawing.Point(139, 120);
  301. this.textCalendar.Name = "textCalendar";
  302. this.textCalendar.Size = new System.Drawing.Size(152, 20);
  303. this.textCalendar.TabIndex = 8;
  304. this.textCalendar.Text = "";
  305. // 
  306. // listCalendars
  307. // 
  308. this.listCalendars.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  309. this.listCalendars.Location = new System.Drawing.Point(139, 152);
  310. this.listCalendars.Name = "listCalendars";
  311. this.listCalendars.Size = new System.Drawing.Size(152, 43);
  312. this.listCalendars.TabIndex = 10;
  313. // 
  314. // textSampleNumber
  315. // 
  316. this.textSampleNumber.Location = new System.Drawing.Point(128, 16);
  317. this.textSampleNumber.Name = "textSampleNumber";
  318. this.textSampleNumber.Size = new System.Drawing.Size(120, 20);
  319. this.textSampleNumber.TabIndex = 1;
  320. this.textSampleNumber.Text = "";
  321. // 
  322. // textSampleDate
  323. // 
  324. this.textSampleDate.Location = new System.Drawing.Point(128, 48);
  325. this.textSampleDate.Name = "textSampleDate";
  326. this.textSampleDate.Size = new System.Drawing.Size(120, 20);
  327. this.textSampleDate.TabIndex = 3;
  328. this.textSampleDate.Text = "";
  329. // 
  330. // textSampleTime
  331. // 
  332. this.textSampleTime.Location = new System.Drawing.Point(128, 80);
  333. this.textSampleTime.Name = "textSampleTime";
  334. this.textSampleTime.Size = new System.Drawing.Size(120, 20);
  335. this.textSampleTime.TabIndex = 5;
  336. this.textSampleTime.Text = "";
  337. // 
  338. // textRegionName
  339. // 
  340. this.textRegionName.Location = new System.Drawing.Point(128, 16);
  341. this.textRegionName.Name = "textRegionName";
  342. this.textRegionName.Size = new System.Drawing.Size(120, 20);
  343. this.textRegionName.TabIndex = 1;
  344. this.textRegionName.Text = "";
  345. // 
  346. // textCurrency
  347. // 
  348. this.textCurrency.Location = new System.Drawing.Point(128, 48);
  349. this.textCurrency.Name = "textCurrency";
  350. this.textCurrency.Size = new System.Drawing.Size(48, 20);
  351. this.textCurrency.TabIndex = 3;
  352. this.textCurrency.Text = "";
  353. // 
  354. // textCurrencyName
  355. // 
  356. this.textCurrencyName.Location = new System.Drawing.Point(200, 48);
  357. this.textCurrencyName.Name = "textCurrencyName";
  358. this.textCurrencyName.Size = new System.Drawing.Size(48, 20);
  359. this.textCurrencyName.TabIndex = 4;
  360. this.textCurrencyName.Text = "";
  361. // 
  362. // checkIsMetric
  363. // 
  364. this.checkIsMetric.Location = new System.Drawing.Point(136, 80);
  365. this.checkIsMetric.Name = "checkIsMetric";
  366. this.checkIsMetric.TabIndex = 5;
  367. this.checkIsMetric.Text = "Is Metric";
  368. // 
  369. // textBox1
  370. // 
  371. this.textBox1.Location = new System.Drawing.Point(0, 0);
  372. this.textBox1.Name = "textBox1";
  373. this.textBox1.TabIndex = 0;
  374. this.textBox1.Text = "";
  375. // 
  376. // textBox2
  377. // 
  378. this.textBox2.Location = new System.Drawing.Point(0, 0);
  379. this.textBox2.Name = "textBox2";
  380. this.textBox2.TabIndex = 0;
  381. this.textBox2.Text = "";
  382. // 
  383. // CultureDemoForm
  384. // 
  385. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  386. this.ClientSize = new System.Drawing.Size(472, 454);
  387. this.Controls.Add(this.panel1);
  388. this.Controls.Add(this.splitter1);
  389. this.Controls.Add(this.treeCultures);
  390. this.Name = "CultureDemoForm";
  391. this.Text = "Culture Demo";
  392. this.panel1.ResumeLayout(false);
  393. this.groupRegionInformation.ResumeLayout(false);
  394. this.groupSamples.ResumeLayout(false);
  395. this.ResumeLayout(false);
  396. }
  397. #endregion
  398. /// <summary>
  399. /// The main entry point for the application.
  400. /// </summary>
  401. [STAThread]
  402. static void Main() 
  403. {
  404. Application.Run(new CultureDemoForm());
  405. }
  406. // add all cultures to the tree view
  407. public void AddCulturesToTree()
  408. {
  409. CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
  410. TreeNode[] nodes = new TreeNode[cultures.Length];
  411. int i = 0;
  412. TreeNode parent = null;
  413. foreach (CultureInfo ci in cultures) 
  414. {
  415. nodes[i] = new TreeNode();
  416. nodes[i].Text = ci.DisplayName;
  417. nodes[i].Tag = ci;
  418. if (ci.IsNeutralCulture)
  419. {
  420. parent = nodes[i];
  421. treeCultures.Nodes.Add(nodes[i]);
  422. }
  423. else if (ci.ThreeLetterISOLanguageName == CultureInfo.InvariantCulture.ThreeLetterISOLanguageName)
  424. {
  425. this.treeCultures.Nodes.Add(nodes[i]);
  426. }
  427. else
  428. {
  429. parent.Nodes.Add(nodes[i]);
  430. }
  431. i++;
  432. }
  433. }
  434. private void OnSelectCulture(object sender, System.Windows.Forms.TreeViewEventArgs e)
  435. {
  436. ClearTextFields();
  437. // get culture from tree
  438. CultureInfo ci = (CultureInfo)e.Node.Tag;
  439. textName.Text = ci.Name;
  440. textNativeName.Text = ci.NativeName;
  441. textEnglishName.Text = ci.EnglishName;
  442. checkIsNeutral.Checked = ci.IsNeutralCulture;
  443. // default calendar
  444. textCalendar.Text = ci.Calendar.ToString().Remove(0, 21);
  445. // fill optional calendars
  446. listCalendars.Items.Clear();
  447. foreach (Calendar optCal in ci.OptionalCalendars)
  448. {
  449. string calName = optCal.ToString().Remove(0, 21);
  450. // for GregorianCalendar add type information
  451. if (optCal is System.Globalization.GregorianCalendar)
  452. {
  453. GregorianCalendar gregCal = optCal as GregorianCalendar;
  454. calName += " " + gregCal.CalendarType.ToString();
  455. }
  456. listCalendars.Items.Add(calName);
  457. }
  458. // display number and date samples
  459. if (!ci.IsNeutralCulture)
  460. {
  461. groupSamples.Enabled = true;
  462. ShowSamples(ci);
  463. // invariant culture doesn't have a region
  464. if (ci.ThreeLetterISOLanguageName == "IVL")
  465. {
  466. groupRegionInformation.Enabled = false;
  467. }
  468. else
  469. {
  470. groupRegionInformation.Enabled = true;
  471. ShowRegionInformation(ci.LCID);
  472. }
  473. }
  474. else // neutral culture: no region, no number/date formatting
  475. {
  476. groupSamples.Enabled = false;
  477. groupRegionInformation.Enabled = false;
  478. }
  479. }
  480. private void ShowSamples(CultureInfo ci)
  481. {
  482. double number = 9876543.21;
  483. textSampleNumber.Text = number.ToString("N", ci);
  484. DateTime today = DateTime.Today;
  485. textSampleDate.Text = today.ToString("D", ci);
  486. DateTime now = DateTime.Now;
  487. textSampleTime.Text = now.ToString("T", ci);
  488. }
  489. private void ShowRegionInformation(int culture)
  490. {
  491. RegionInfo ri = new RegionInfo(culture);
  492. textRegionName.Text = ri.DisplayName;
  493. textCurrency.Text = ri.CurrencySymbol;
  494. textCurrencyName.Text = ri.ISOCurrencySymbol;
  495. checkIsMetric.Checked = ri.IsMetric;
  496. }
  497. #region Clear Fields
  498. private void ClearTextFields()
  499. {
  500. // samples
  501. textSampleNumber.Text = "";
  502. textSampleDate.Text = "";
  503. textSampleTime.Text = "";
  504. // region information
  505. textRegionName.Text = "";
  506. textCurrency.Text = "";
  507. textCurrencyName.Text = "";
  508. checkIsMetric.Checked = false;
  509. }
  510. #endregion
  511. }
  512. }