using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Collections; namespace System.Windows.Forms { public partial class NumericBoxBase : UserControl { public NumericBoxBase() { InitializeComponent(); this.Value = (0).ToString(); this.ReadOnly = false; } [Browsable(true)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [EditorBrowsable(EditorBrowsableState.Always)] public override bool AutoSize { get { return base.AutoSize; } set { if (base.AutoSize != value) { base.AutoSize = value; OnAutoSizeChanged(new EventArgs()); } } } protected override void OnAutoSizeChanged(EventArgs e) { base.OnAutoSizeChanged(e); layout_panel.AutoSize = base.AutoSize; } new public Font Font { get { return base.Font; } set { base.Font = value; txtValueBox.Font = value; btnCopy.Font = value; btnPaste.Font = value; } } private string _Value; [Browsable(true)] public string Value { get { return _Value; } set { _Value = value; this.Text = value; DoResize(); } } private bool _ReadOnly; public bool ReadOnly { get { return _ReadOnly; } set { _ReadOnly = value; if (_ReadOnly) { txtValueBox.Enabled = false; } else { txtValueBox.Enabled = true; } } } public override string Text { get { return txtValueBox.Text; } set { txtValueBox.Text = value; } } private void btnCopy_Click(object sender, EventArgs e) { Clipboard.SetData(DataFormats.Text, this.Text); } private void btnPaste_Click(object sender, EventArgs e) { if (this.ReadOnly) return; this.Text = (string)Clipboard.GetData(DataFormats.Text); } protected void CreateStandardKeys(List ValidKeys) { // these are mostly editing/navigation keys ValidKeys.Add(Keys.Delete); ValidKeys.Add(Keys.Back); ValidKeys.Add(Keys.Up); ValidKeys.Add(Keys.Down); ValidKeys.Add(Keys.Left); ValidKeys.Add(Keys.Right); ValidKeys.Add(Keys.Home); ValidKeys.Add(Keys.End); } protected virtual void CreateValidKeys(List ValidKeys) { //ValidKeys.AddRange(Enum.GetValues(typeof(Keys)).Cast().Where(x => x >= Keys.D0 && x <= Keys.D9)); // ValidKeys.AddRange(Enum.GetValues(typeof(Keys)).Cast().Where(x => x >= Keys.NumPad0 && x <= Keys.NumPad9)); } protected List GetValidKeys() { List ValidKeys = new List(); CreateStandardKeys(ValidKeys); CreateValidKeys(ValidKeys); return ValidKeys; } private void txtValueBox_KeyDown(object sender, KeyEventArgs e) { List ValidKeys = this.GetValidKeys(); if (ValidKeys.Contains(e.KeyCode)) { if (txtValueBox.Text.Contains(".") && (e.KeyCode == Keys.Decimal || e.KeyCode == Keys.OemPeriod)) { e.SuppressKeyPress = true; } return; } if(!e.Control && !e.Alt) { e.SuppressKeyPress = true; } } private void txtValueBox_TextChanged(object sender, EventArgs e) { this.Value = this.Text; } private void DoResize() { using (Graphics g = this.CreateGraphics()) { string t = txtValueBox.Text; var size = g.MeasureString(t, this.Font).ToSize(); int width = size.Width; // the minimum size of the textbox txtValueBox.Width = width + 5; this.Width = width; } } } #region NumericBox Extensions public static class NumericBoxExtensions { public static byte ToByte(this NumericBoxBase val) { try { return Convert.ToByte(val.Value); } catch { return 0; } } public static sbyte ToSByte(this NumericBoxBase val) { try { return Convert.ToSByte(val.Value); } catch { return 0; } } public static Int16 ToInt16(this NumericBoxBase val) { try { return Convert.ToInt16(val.Value); } catch { return 0; } } public static Int32 ToInt32(this NumericBoxBase val) { try { return Convert.ToInt32(val.Value); } catch { return 0; } } public static Int64 ToInt64(this NumericBoxBase val) { try { return Convert.ToInt64(val.Value); } catch { return 0; } } public static UInt16 ToUInt16(this NumericBoxBase val) { try { return Convert.ToUInt16(val.Value); } catch { return 0; } } public static UInt32 ToUInt32(this NumericBoxBase val) { try { return Convert.ToUInt32(val.Value); } catch { return 0; } } public static UInt64 ToUInt64(this NumericBoxBase val) { try { return Convert.ToUInt64(val.Value); } catch { return 0; } } public static Single ToSingle(this NumericBoxBase val) { try { return Convert.ToSingle(val.Value); } catch { return 0; } } public static double ToDouble(this NumericBoxBase val) { try { return Convert.ToDouble(val.Value); } catch { return 0; } } public static decimal ToDecimal(this NumericBoxBase val) { try { return Convert.ToDecimal(val.Value); } catch { return 0; } } public static string ToString(this NumericBoxBase val) { try { return Convert.ToString(val.Value); } catch { return "0"; } } } #endregion }