123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- #nullable enable
- using System;
- using Terminal.Gui;
- namespace UICatalog.Scenarios;
- [ScenarioMetadata ("NumericUpDown", "Demonstrates the NumericUpDown View")]
- [ScenarioCategory ("Controls")]
- public class NumericUpDownDemo : Scenario
- {
- public override void Main ()
- {
- Application.Init ();
- Window app = new ()
- {
- Title = GetQuitKeyAndName (),
- };
- var editor = new AdornmentsEditor
- {
- X = 0,
- Y = 0,
- AutoSelectViewToEdit = true,
- };
- app.Add (editor);
- NumericUpDownEditor<int> intEditor = new ()
- {
- X = Pos.Right (editor),
- Y = 0,
- Title = "int",
- };
- app.Add (intEditor);
- NumericUpDownEditor<float> floatEditor = new ()
- {
- X = Pos.Right (intEditor),
- Y = 0,
- Title = "float",
- };
- app.Add (floatEditor);
- app.Initialized += AppInitialized;
- void AppInitialized (object? sender, EventArgs e)
- {
- floatEditor!.NumericUpDown!.Increment = 0.1F;
- floatEditor!.NumericUpDown!.Format = "{0:0.0}";
- }
- editor.AutoSelectSuperView = app;
- intEditor.SetFocus ();
- Application.Run (app);
- app.Dispose ();
- Application.Shutdown ();
- }
- }
- internal class NumericUpDownEditor<T> : View where T : notnull
- {
- private NumericUpDown<T>? _numericUpDown;
- internal NumericUpDown<T>? NumericUpDown
- {
- get => _numericUpDown;
- set
- {
- if (value == _numericUpDown)
- {
- return;
- }
- _numericUpDown = value;
- if (_numericUpDown is { } && _value is { })
- {
- _value.Text = _numericUpDown.Text;
- }
- }
- }
- private TextField? _value;
- private TextField? _format;
- private TextField? _increment;
- internal NumericUpDownEditor ()
- {
- _numericUpDown = null;
- Title = "NumericUpDownEditor";
- BorderStyle = LineStyle.Single;
- Width = Dim.Auto (DimAutoStyle.Content);
- Height = Dim.Auto (DimAutoStyle.Content);
- TabStop = TabBehavior.TabGroup;
- CanFocus = true;
- Initialized += NumericUpDownEditorInitialized;
- return;
- void NumericUpDownEditorInitialized (object? sender, EventArgs e)
- {
- Label label = new ()
- {
- Title = "_Value: ",
- Width = 12,
- };
- label.TextFormatter.Alignment = Alignment.End;
- _value = new ()
- {
- X = Pos.Right (label),
- Y = Pos.Top (label),
- Width = 8,
- Title = "Value",
- };
- _value.Accepted += ValuedOnAccept;
- void ValuedOnAccept (object? sender, EventArgs e)
- {
- if (_numericUpDown is null)
- {
- return;
- }
- try
- {
- if (string.IsNullOrEmpty (_value.Text))
- {
- // Handle empty or null text if needed
- _numericUpDown.Value = default!;
- }
- else
- {
- // Parse _value.Text and then convert to type T
- _numericUpDown.Value = (T)Convert.ChangeType (_value.Text, typeof (T));
- }
- _value.ColorScheme = SuperView!.ColorScheme;
- }
- catch (System.FormatException)
- {
- _value.ColorScheme = Colors.ColorSchemes ["Error"];
- }
- catch (InvalidCastException)
- {
- _value.ColorScheme = Colors.ColorSchemes ["Error"];
- }
- finally
- {
- }
- }
- Add (label, _value);
- label = new ()
- {
- Y = Pos.Bottom (_value),
- Width = 12,
- Title = "_Format: ",
- };
- label.TextFormatter.Alignment = Alignment.End;
- _format = new ()
- {
- X = Pos.Right (label),
- Y = Pos.Top (label),
- Title = "Format",
- Width = Dim.Width (_value),
- };
- _format.Accepted += FormatOnAccept;
- void FormatOnAccept (object? o, EventArgs eventArgs)
- {
- if (_numericUpDown is null)
- {
- return;
- }
- try
- {
- // Test format to ensure it's valid
- _ = string.Format (_format.Text, _value);
- _numericUpDown.Format = _format.Text;
- _format.ColorScheme = SuperView!.ColorScheme;
- }
- catch (System.FormatException)
- {
- _format.ColorScheme = Colors.ColorSchemes ["Error"];
- }
- catch (InvalidCastException)
- {
- _format.ColorScheme = Colors.ColorSchemes ["Error"];
- }
- finally
- {
- }
- }
- Add (label, _format);
- label = new ()
- {
- Y = Pos.Bottom (_format),
- Width = 12,
- Title = "_Increment: ",
- };
- label.TextFormatter.Alignment = Alignment.End;
- _increment = new ()
- {
- X = Pos.Right (label),
- Y = Pos.Top (label),
- Title = "Increment",
- Width = Dim.Width (_value),
- };
- _increment.Accepted += IncrementOnAccept;
- void IncrementOnAccept (object? o, EventArgs eventArgs)
- {
- if (_numericUpDown is null)
- {
- return;
- }
- try
- {
- if (string.IsNullOrEmpty (_value.Text))
- {
- // Handle empty or null text if needed
- _numericUpDown.Increment = default!;
- }
- else
- {
- // Parse _value.Text and then convert to type T
- _numericUpDown.Increment = (T)Convert.ChangeType (_increment.Text, typeof (T));
- }
- _increment.ColorScheme = SuperView!.ColorScheme;
- }
- catch (System.FormatException)
- {
- _increment.ColorScheme = Colors.ColorSchemes ["Error"];
- }
- catch (InvalidCastException)
- {
- _increment.ColorScheme = Colors.ColorSchemes ["Error"];
- }
- finally
- {
- }
- }
- Add (label, _increment);
- _numericUpDown = new ()
- {
- X = Pos.Center (),
- Y = Pos.Bottom (_increment) + 1,
- Increment = (dynamic)1,
- };
- _numericUpDown.ValueChanged += NumericUpDownOnValueChanged;
- void NumericUpDownOnValueChanged (object? o, EventArgs<T> eventArgs)
- {
- _value.Text = _numericUpDown.Text;
- }
- _numericUpDown.IncrementChanged += NumericUpDownOnIncrementChanged;
- void NumericUpDownOnIncrementChanged (object? o, EventArgs<T> eventArgs)
- {
- _increment.Text = _numericUpDown.Increment.ToString ();
- }
- Add (_numericUpDown);
- _value.Text = _numericUpDown.Text;
- _format.Text = _numericUpDown.Format;
- _increment.Text = _numericUpDown.Increment.ToString ();
- }
- }
- }
|