diff options
| author | Jacob Palecki <[email protected]> | 2020-07-29 19:35:57 -0700 |
|---|---|---|
| committer | Jacob Palecki <[email protected]> | 2020-07-29 19:35:57 -0700 |
| commit | 621ab8d23d10c892a18c39234b24266fd90b90fa (patch) | |
| tree | 7c46133ef24dc74b02437fb61616ece0163fb64f /grapher/Field.cs | |
| parent | Take all variables through GUI (diff) | |
| download | rawaccel-621ab8d23d10c892a18c39234b24266fd90b90fa.tar.xz rawaccel-621ab8d23d10c892a18c39234b24266fd90b90fa.zip | |
Separate classes into files, add Field class for text box state
Diffstat (limited to 'grapher/Field.cs')
| -rw-r--r-- | grapher/Field.cs | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/grapher/Field.cs b/grapher/Field.cs new file mode 100644 index 0000000..641bb2a --- /dev/null +++ b/grapher/Field.cs @@ -0,0 +1,130 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace grapher +{ + public class Field + { + #region Enums + + public enum FieldState + { + Undefined, + Default, + Typing, + Entered, + Unavailable, + } + + #endregion Enums + + + #region Constructors + + public Field(string defaultText, TextBox box, double defaultData) + { + DefaultText = defaultText; + Box = box; + Data = defaultData; + State = FieldState.Undefined; + box.KeyDown += KeyDown; + + SetToDefault(); + } + + #endregion Constructors + + #region Properties + + TextBox Box { get; } + + public double Data { get; private set; } + + public string DefaultText { get; } + + public FieldState State { get; private set; } + + #endregion Properties + + #region Methods + + public void SetToDefault() + { + if (State != FieldState.Default) + { + Box.BackColor = Color.AntiqueWhite; + Box.ForeColor = Color.Gray; + Box.Text = DefaultText; + State = FieldState.Default; + } + } + + public void SetToTyping() + { + if (State != FieldState.Typing) + { + Box.BackColor = Color.White; + Box.ForeColor = Color.Black; + State = FieldState.Typing; + } + } + + public void SetToEntered() + { + if (State != FieldState.Entered) + { + Box.BackColor = Color.White; + Box.ForeColor = Color.DarkGray; + State = FieldState.Entered; + } + } + + public void SetToUnavailable() + { + if (State != FieldState.Unavailable) + { + Box.BackColor = Color.LightGray; + Box.ForeColor = Color.LightGray; + Box.Text = string.Empty; + } + } + + public void KeyDown(object sender, KeyEventArgs e) + { + if (TryHandleWithEnter(e, sender, out double data)) + { + Data = data; + } + } + + private bool TryHandleWithEnter(KeyEventArgs e, object sender, out double data) + { + bool validEntry = false; + data = 0.0; + + if (e.KeyCode == Keys.Enter) + { + try + { + data = Convert.ToDouble(((TextBox)sender).Text); + validEntry = true; + } + catch + { + } + + e.Handled = true; + e.SuppressKeyPress = true; + } + + return validEntry; + } + + #endregion Methods + } +} |