TimeAndDate.cs 4.5 KB

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