TimeAndDate.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using Terminal.Gui;
  3. namespace UICatalog {
  4. [ScenarioMetadata (Name: "Time And Date", Description: "Illustrates TimeField and time & date handling")]
  5. [ScenarioCategory ("Controls")]
  6. [ScenarioCategory ("Bug Repro")] // Issue #246
  7. class TimeAndDate : Scenario {
  8. Label lblOldTime;
  9. Label lblNewTime;
  10. Label lblTimeFmt;
  11. Label lblOldDate;
  12. Label lblNewDate;
  13. Label lblDateFmt;
  14. public override void Setup ()
  15. {
  16. var longTime = new TimeField (DateTime.Now.TimeOfDay) {
  17. X = Pos.Center (),
  18. Y = 2,
  19. IsShortFormat = false,
  20. ReadOnly = false,
  21. };
  22. longTime.TimeChanged += TimeChanged;
  23. Win.Add (longTime);
  24. var shortTime = new TimeField (DateTime.Now.TimeOfDay) {
  25. X = Pos.Center (),
  26. Y = Pos.Bottom (longTime) + 1,
  27. IsShortFormat = true,
  28. ReadOnly = false,
  29. };
  30. shortTime.TimeChanged += TimeChanged;
  31. Win.Add (shortTime);
  32. var shortDate = new DateField (DateTime.Now) {
  33. X = Pos.Center (),
  34. Y = Pos.Bottom (shortTime) + 1,
  35. IsShortFormat = true,
  36. ReadOnly = true,
  37. };
  38. shortDate.DateChanged += DateChanged;
  39. Win.Add (shortDate);
  40. var longDate = new DateField (DateTime.Now) {
  41. X = Pos.Center (),
  42. Y = Pos.Bottom (shortDate) + 1,
  43. IsShortFormat = false,
  44. ReadOnly = true,
  45. };
  46. longDate.DateChanged += DateChanged;
  47. Win.Add (longDate);
  48. lblOldTime = new Label ("Old Time: ") {
  49. X = Pos.Center (),
  50. Y = Pos.Bottom (longDate) + 1,
  51. TextAlignment = TextAlignment.Centered,
  52. Width = Dim.Fill(),
  53. };
  54. Win.Add (lblOldTime);
  55. lblNewTime = new Label ("New Time: ") {
  56. X = Pos.Center (),
  57. Y = Pos.Bottom (lblOldTime) + 1,
  58. TextAlignment = TextAlignment.Centered,
  59. Width = Dim.Fill (),
  60. };
  61. Win.Add (lblNewTime);
  62. lblTimeFmt = new Label ("Time Format: ") {
  63. X = Pos.Center (),
  64. Y = Pos.Bottom (lblNewTime) + 1,
  65. TextAlignment = TextAlignment.Centered,
  66. Width = Dim.Fill (),
  67. };
  68. Win.Add (lblTimeFmt);
  69. lblOldDate = new Label ("Old Date: ") {
  70. X = Pos.Center (),
  71. Y = Pos.Bottom (lblTimeFmt) + 2,
  72. TextAlignment = TextAlignment.Centered,
  73. Width = Dim.Fill (),
  74. };
  75. Win.Add (lblOldDate);
  76. lblNewDate = new Label ("New Date: ") {
  77. X = Pos.Center (),
  78. Y = Pos.Bottom (lblOldDate) + 1,
  79. TextAlignment = TextAlignment.Centered,
  80. Width = Dim.Fill (),
  81. };
  82. Win.Add (lblNewDate);
  83. lblDateFmt = new Label ("Date Format: ") {
  84. X = Pos.Center (),
  85. Y = Pos.Bottom (lblNewDate) + 1,
  86. TextAlignment = TextAlignment.Centered,
  87. Width = Dim.Fill (),
  88. };
  89. Win.Add (lblDateFmt);
  90. var swapButton = new Button ("Swap Long/Short & Read/Read Only") {
  91. X = Pos.Center (),
  92. Y = Pos.Bottom (Win) - 5,
  93. };
  94. swapButton.Clicked += () => {
  95. longTime.ReadOnly = !longTime.ReadOnly;
  96. shortTime.ReadOnly = !shortTime.ReadOnly;
  97. longTime.IsShortFormat = !longTime.IsShortFormat;
  98. shortTime.IsShortFormat = !shortTime.IsShortFormat;
  99. longDate.ReadOnly = !longDate.ReadOnly;
  100. shortDate.ReadOnly = !shortDate.ReadOnly;
  101. longDate.IsShortFormat = !longDate.IsShortFormat;
  102. shortDate.IsShortFormat = !shortDate.IsShortFormat;
  103. };
  104. Win.Add (swapButton);
  105. }
  106. private void TimeChanged (DateTimeEventArgs<TimeSpan> e)
  107. {
  108. lblOldTime.Text = $"Old Time: {e.OldValue}";
  109. lblNewTime.Text = $"New Time: {e.NewValue}";
  110. lblTimeFmt.Text = $"Time Format: {e.Format}";
  111. }
  112. private void DateChanged (DateTimeEventArgs<DateTime> e)
  113. {
  114. lblOldDate.Text = $"Old Date: {e.OldValue}";
  115. lblNewDate.Text = $"New Date: {e.NewValue}";
  116. lblDateFmt.Text = $"Date Format: {e.Format}";
  117. }
  118. }
  119. }