NumericUpDownDemo.cs 8.0 KB

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