NumericUpDownDemo.cs 7.6 KB

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