NumericUpDownDemo.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #nullable enable
  2. using System;
  3. using Terminal.Gui;
  4. namespace UICatalog.Scenarios;
  5. [ScenarioMetadata ("NumericUpDown", "Demonstrates the NumericUpDown View")]
  6. [ScenarioCategory ("Controls")]
  7. public class NumericUpDownDemo : Scenario
  8. {
  9. public override void Main ()
  10. {
  11. Application.Init ();
  12. Window app = new ()
  13. {
  14. Title = GetQuitKeyAndName (),
  15. BorderStyle = LineStyle.None
  16. };
  17. var editor = new AdornmentsEditor
  18. {
  19. X = 0,
  20. Y = 0,
  21. AutoSelectViewToEdit = true,
  22. };
  23. app.Add (editor);
  24. NumericUpDownEditor<int> intEditor = new ()
  25. {
  26. X = Pos.Right (editor),
  27. Y = 0,
  28. Title = "int",
  29. };
  30. app.Add (intEditor);
  31. NumericUpDownEditor<float> floatEditor = new ()
  32. {
  33. X = Pos.Right (intEditor),
  34. Y = 0,
  35. Title = "float",
  36. };
  37. app.Add (floatEditor);
  38. app.Initialized += AppInitialized;
  39. void AppInitialized (object? sender, EventArgs e)
  40. {
  41. floatEditor!.NumericUpDown!.Increment = 0.1F;
  42. floatEditor!.NumericUpDown!.Format = "{0:0.0}";
  43. }
  44. editor.AutoSelectSuperView = app;
  45. intEditor.SetFocus ();
  46. Application.Run (app);
  47. app.Dispose ();
  48. Application.Shutdown ();
  49. }
  50. }
  51. internal class NumericUpDownEditor<T> : View where T : notnull
  52. {
  53. private NumericUpDown<T>? _numericUpDown;
  54. internal NumericUpDown<T>? NumericUpDown
  55. {
  56. get => _numericUpDown;
  57. set
  58. {
  59. if (value == _numericUpDown)
  60. {
  61. return;
  62. }
  63. _numericUpDown = value;
  64. if (_numericUpDown is { } && _value is { })
  65. {
  66. _value.Text = _numericUpDown.Text;
  67. }
  68. }
  69. }
  70. private TextField? _value;
  71. private TextField? _format;
  72. private TextField? _increment;
  73. internal NumericUpDownEditor ()
  74. {
  75. _numericUpDown = null;
  76. Title = "NumericUpDownEditor";
  77. BorderStyle = LineStyle.Single;
  78. Width = Dim.Auto (DimAutoStyle.Content);
  79. Height = Dim.Auto (DimAutoStyle.Content);
  80. TabStop = TabBehavior.TabGroup;
  81. CanFocus = true;
  82. Initialized += NumericUpDownEditorInitialized;
  83. return;
  84. void NumericUpDownEditorInitialized (object? sender, EventArgs e)
  85. {
  86. Label label = new ()
  87. {
  88. Title = "_Value: ",
  89. Width = 12,
  90. };
  91. label.TextFormatter.Alignment = Alignment.End;
  92. _value = new ()
  93. {
  94. X = Pos.Right (label),
  95. Y = Pos.Top (label),
  96. Width = 8,
  97. Title = "Value",
  98. };
  99. _value.Accepting += ValuedOnAccept;
  100. void ValuedOnAccept (object? sender, EventArgs e)
  101. {
  102. if (_numericUpDown is null)
  103. {
  104. return;
  105. }
  106. try
  107. {
  108. if (string.IsNullOrEmpty (_value.Text))
  109. {
  110. // Handle empty or null text if needed
  111. _numericUpDown.Value = default!;
  112. }
  113. else
  114. {
  115. // Parse _value.Text and then convert to type T
  116. _numericUpDown.Value = (T)Convert.ChangeType (_value.Text, typeof (T));
  117. }
  118. _value.ColorScheme = SuperView!.ColorScheme;
  119. }
  120. catch (System.FormatException)
  121. {
  122. _value.ColorScheme = Colors.ColorSchemes ["Error"];
  123. }
  124. catch (InvalidCastException)
  125. {
  126. _value.ColorScheme = Colors.ColorSchemes ["Error"];
  127. }
  128. finally
  129. {
  130. }
  131. }
  132. Add (label, _value);
  133. label = new ()
  134. {
  135. Y = Pos.Bottom (_value),
  136. Width = 12,
  137. Title = "_Format: ",
  138. };
  139. label.TextFormatter.Alignment = Alignment.End;
  140. _format = new ()
  141. {
  142. X = Pos.Right (label),
  143. Y = Pos.Top (label),
  144. Title = "Format",
  145. Width = Dim.Width (_value),
  146. };
  147. _format.Accepting += FormatOnAccept;
  148. void FormatOnAccept (object? o, EventArgs eventArgs)
  149. {
  150. if (_numericUpDown is null)
  151. {
  152. return;
  153. }
  154. try
  155. {
  156. // Test format to ensure it's valid
  157. _ = string.Format (_format.Text, _value);
  158. _numericUpDown.Format = _format.Text;
  159. _format.ColorScheme = SuperView!.ColorScheme;
  160. }
  161. catch (System.FormatException)
  162. {
  163. _format.ColorScheme = Colors.ColorSchemes ["Error"];
  164. }
  165. catch (InvalidCastException)
  166. {
  167. _format.ColorScheme = Colors.ColorSchemes ["Error"];
  168. }
  169. finally
  170. {
  171. }
  172. }
  173. Add (label, _format);
  174. label = new ()
  175. {
  176. Y = Pos.Bottom (_format),
  177. Width = 12,
  178. Title = "_Increment: ",
  179. };
  180. label.TextFormatter.Alignment = Alignment.End;
  181. _increment = new ()
  182. {
  183. X = Pos.Right (label),
  184. Y = Pos.Top (label),
  185. Title = "Increment",
  186. Width = Dim.Width (_value),
  187. };
  188. _increment.Accepting += IncrementOnAccept;
  189. void IncrementOnAccept (object? o, EventArgs eventArgs)
  190. {
  191. if (_numericUpDown is null)
  192. {
  193. return;
  194. }
  195. try
  196. {
  197. if (string.IsNullOrEmpty (_value.Text))
  198. {
  199. // Handle empty or null text if needed
  200. _numericUpDown.Increment = default!;
  201. }
  202. else
  203. {
  204. // Parse _value.Text and then convert to type T
  205. _numericUpDown.Increment = (T)Convert.ChangeType (_increment.Text, typeof (T));
  206. }
  207. _increment.ColorScheme = SuperView!.ColorScheme;
  208. }
  209. catch (System.FormatException)
  210. {
  211. _increment.ColorScheme = Colors.ColorSchemes ["Error"];
  212. }
  213. catch (InvalidCastException)
  214. {
  215. _increment.ColorScheme = Colors.ColorSchemes ["Error"];
  216. }
  217. finally
  218. {
  219. }
  220. }
  221. Add (label, _increment);
  222. _numericUpDown = new ()
  223. {
  224. X = Pos.Center (),
  225. Y = Pos.Bottom (_increment) + 1,
  226. Increment = (dynamic)1,
  227. };
  228. _numericUpDown.ValueChanged += NumericUpDownOnValueChanged;
  229. void NumericUpDownOnValueChanged (object? o, EventArgs<T> eventArgs)
  230. {
  231. _value.Text = _numericUpDown.Text;
  232. }
  233. _numericUpDown.IncrementChanged += NumericUpDownOnIncrementChanged;
  234. void NumericUpDownOnIncrementChanged (object? o, EventArgs<T> eventArgs)
  235. {
  236. _increment.Text = _numericUpDown.Increment.ToString ();
  237. }
  238. Add (_numericUpDown);
  239. _value.Text = _numericUpDown.Text;
  240. _format.Text = _numericUpDown.Format;
  241. _increment.Text = _numericUpDown.Increment.ToString ();
  242. }
  243. }
  244. }