NumericUpDownDemo.cs 7.6 KB

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