TimeAndDate.cs 3.5 KB

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