From fea8e8c559416d7fbf60168dc6d21f2c297f431f Mon Sep 17 00:00:00 2001 From: Jacob Palecki Date: Wed, 12 Aug 2020 18:05:19 -0700 Subject: Nicer decimals, enter press not needed to enter values --- grapher/Field.cs | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) (limited to 'grapher/Field.cs') diff --git a/grapher/Field.cs b/grapher/Field.cs index 5ef057d..d34472b 100644 --- a/grapher/Field.cs +++ b/grapher/Field.cs @@ -10,6 +10,12 @@ namespace grapher { public class Field { + #region Constants + + public const string DefaultFormatString = "#.#########"; + + #endregion Constants + #region Enums public enum FieldState @@ -34,6 +40,7 @@ namespace grapher DefaultData = defaultData; State = FieldState.Undefined; ContainingForm = containingForm; + FormatString = DefaultFormatString; box.KeyDown += new System.Windows.Forms.KeyEventHandler(KeyDown); box.Leave += new System.EventHandler(FocusLeave); @@ -50,6 +57,8 @@ namespace grapher public double Data { get; private set; } + public string FormatString { get; set; } + public string DefaultText { get; } public FieldState State { get; private set; } @@ -163,15 +172,8 @@ namespace grapher { if (State == FieldState.Typing) { - if (PreviousState == FieldState.Default) - { - SetToDefault(); - } - else if (PreviousState == FieldState.Entered) - { - SetToEntered(); - Box.Text = DecimalString(Data); - } + TextToData(); + SetToEntered(); } } @@ -179,15 +181,8 @@ namespace grapher { if (e.KeyCode == Keys.Enter) { - try - { - Data = Convert.ToDouble(((TextBox)sender).Text); - } - catch - { - } - - Box.Text = DecimalString(Data); + TextToData(); + e.Handled = true; e.SuppressKeyPress = true; @@ -201,11 +196,25 @@ namespace grapher } } - private static string DecimalString(double value) + private void TextToData() + { + try + { + Data = Convert.ToDouble(Box.Text); + } + catch + { + } + + Box.Text = DecimalString(Data); + } + + private string DecimalString(double value) { - return value.ToString("N2"); + return value.ToString(FormatString); } + #endregion Methods } } -- cgit v1.2.3