TimeAndDate.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 Main ()
  16. {
  17. Application.Init ();
  18. var win = new Window { Title = GetQuitKeyAndName () };
  19. var longTime = new TimeField
  20. {
  21. X = Pos.Center (),
  22. Y = 2,
  23. IsShortFormat = false,
  24. ReadOnly = false,
  25. Time = DateTime.Now.TimeOfDay
  26. };
  27. longTime.TimeChanged += TimeChanged;
  28. win.Add (longTime);
  29. var shortTime = new TimeField
  30. {
  31. X = Pos.Center (),
  32. Y = Pos.Bottom (longTime) + 1,
  33. IsShortFormat = true,
  34. ReadOnly = false,
  35. Time = DateTime.Now.TimeOfDay
  36. };
  37. shortTime.TimeChanged += TimeChanged;
  38. win.Add (shortTime);
  39. var shortDate = new DateField (DateTime.Now)
  40. {
  41. X = Pos.Center (), Y = Pos.Bottom (shortTime) + 1, ReadOnly = true
  42. };
  43. shortDate.DateChanged += DateChanged;
  44. win.Add (shortDate);
  45. var longDate = new DateField (DateTime.Now)
  46. {
  47. X = Pos.Center (), Y = Pos.Bottom (shortDate) + 1, ReadOnly = false
  48. };
  49. longDate.DateChanged += DateChanged;
  50. win.Add (longDate);
  51. _lblOldTime = new()
  52. {
  53. X = Pos.Center (),
  54. Y = Pos.Bottom (longDate) + 1,
  55. TextAlignment = Alignment.Center,
  56. Width = Dim.Fill (),
  57. Text = "Old Time: "
  58. };
  59. win.Add (_lblOldTime);
  60. _lblNewTime = new()
  61. {
  62. X = Pos.Center (),
  63. Y = Pos.Bottom (_lblOldTime) + 1,
  64. TextAlignment = Alignment.Center,
  65. Width = Dim.Fill (),
  66. Text = "New Time: "
  67. };
  68. win.Add (_lblNewTime);
  69. _lblTimeFmt = new()
  70. {
  71. X = Pos.Center (),
  72. Y = Pos.Bottom (_lblNewTime) + 1,
  73. TextAlignment = Alignment.Center,
  74. Width = Dim.Fill (),
  75. Text = "Time Format: "
  76. };
  77. win.Add (_lblTimeFmt);
  78. _lblOldDate = new()
  79. {
  80. X = Pos.Center (),
  81. Y = Pos.Bottom (_lblTimeFmt) + 2,
  82. TextAlignment = Alignment.Center,
  83. Width = Dim.Fill (),
  84. Text = "Old Date: "
  85. };
  86. win.Add (_lblOldDate);
  87. _lblNewDate = new()
  88. {
  89. X = Pos.Center (),
  90. Y = Pos.Bottom (_lblOldDate) + 1,
  91. TextAlignment = Alignment.Center,
  92. Width = Dim.Fill (),
  93. Text = "New Date: "
  94. };
  95. win.Add (_lblNewDate);
  96. _lblDateFmt = new()
  97. {
  98. X = Pos.Center (),
  99. Y = Pos.Bottom (_lblNewDate) + 1,
  100. TextAlignment = Alignment.Center,
  101. Width = Dim.Fill (),
  102. Text = "Date Format: "
  103. };
  104. win.Add (_lblDateFmt);
  105. var swapButton = new Button
  106. {
  107. X = Pos.Center (), Y = Pos.Bottom (win) - 5, Text = "Swap Long/Short & Read/Read Only"
  108. };
  109. swapButton.Accept += (s, e) =>
  110. {
  111. longTime.ReadOnly = !longTime.ReadOnly;
  112. shortTime.ReadOnly = !shortTime.ReadOnly;
  113. longTime.IsShortFormat = !longTime.IsShortFormat;
  114. shortTime.IsShortFormat = !shortTime.IsShortFormat;
  115. longDate.ReadOnly = !longDate.ReadOnly;
  116. shortDate.ReadOnly = !shortDate.ReadOnly;
  117. };
  118. win.Add (swapButton);
  119. Application.Run (win);
  120. win.Dispose ();
  121. Application.Shutdown ();
  122. }
  123. private void DateChanged (object sender, DateTimeEventArgs<DateTime> e)
  124. {
  125. _lblOldDate.Text = $"Old Date: {e.OldValue}";
  126. _lblNewDate.Text = $"New Date: {e.NewValue}";
  127. _lblDateFmt.Text = $"Date Format: {e.Format}";
  128. }
  129. private void TimeChanged (object sender, DateTimeEventArgs<TimeSpan> e)
  130. {
  131. _lblOldTime.Text = $"Old Time: {e.OldValue}";
  132. _lblNewTime.Text = $"New Time: {e.NewValue}";
  133. _lblTimeFmt.Text = $"Time Format: {e.Format}";
  134. }
  135. }