NumericUpDownDemo.cs 7.9 KB

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