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; namespace System.Windows.Forms { public partial class NumericBox : UserControl { public NumericBox() { InitializeComponent(); this.Value = 0; this.ReadOnly = false; } private object _Value; public virtual object Value { get { return _Value; } set { _Value = value; this.Text = value.ToString(); } } private bool _ReadOnly; public virtual 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); } } #region NumericBox Extensions public static class NumericBoxExtensions { public static byte ToByte(this NumericBox val) { try { return Convert.ToByte(val.Value); } catch { return 0; } } public static sbyte ToSByte(this NumericBox val) { try { return Convert.ToSByte(val.Value); } catch { return 0; } } public static Int16 ToInt16(this NumericBox val) { try { return Convert.ToInt16(val.Value); } catch { return 0; } } public static Int32 ToInt32(this NumericBox val) { try { return Convert.ToInt32(val.Value); } catch { return 0; } } public static Int64 ToInt64(this NumericBox val) { try { return Convert.ToInt64(val.Value); } catch { return 0; } } public static UInt16 ToUInt16(this NumericBox val) { try { return Convert.ToUInt16(val.Value); } catch { return 0; } } public static UInt32 ToUInt32(this NumericBox val) { try { return Convert.ToUInt32(val.Value); } catch { return 0; } } public static UInt64 ToUInt64(this NumericBox val) { try { return Convert.ToUInt64(val.Value); } catch { return 0; } } public static Single ToSingle(this NumericBox val) { try { return Convert.ToSingle(val.Value); } catch { return 0; } } public static double ToDouble(this NumericBox val) { try { return Convert.ToDouble(val.Value); } catch { return 0; } } public static decimal ToDecimal(this NumericBox val) { try { return Convert.ToDecimal(val.Value); } catch { return 0; } } public static string ToString(this NumericBox val) { try { return Convert.ToString(val.Value); } catch { return "0"; } } } #endregion }