diff options
| author | Jacob Palecki <[email protected]> | 2020-08-12 18:05:19 -0700 |
|---|---|---|
| committer | Jacob Palecki <[email protected]> | 2020-08-12 18:05:19 -0700 |
| commit | fea8e8c559416d7fbf60168dc6d21f2c297f431f (patch) | |
| tree | 0945cd71a2f0b12ffafa92a36223499e38c2f849 /grapher/Field.cs | |
| parent | Merge pull request #14 from JacobPalecki/GainCap (diff) | |
| download | rawaccel-fea8e8c559416d7fbf60168dc6d21f2c297f431f.tar.xz rawaccel-fea8e8c559416d7fbf60168dc6d21f2c297f431f.zip | |
Nicer decimals, enter press not needed to enter values
Diffstat (limited to 'grapher/Field.cs')
| -rw-r--r-- | grapher/Field.cs | 49 |
1 files changed, 29 insertions, 20 deletions
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 } } |