TimeAndDate.cs 4.4 KB

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