public class Int32TextBox : TypedTextBox<int> { }
public class DecimalTextBox : TypedTextBox<double> { }
public class DoubleTextBox : TypedTextBox<decimal> { }
public class TypedTextBox<T> : TextBox
where T : IEquatable<T>
{
T value;
[Category("Appearance")]
public T Value
{
get { return this.value; }
set
{
if (!this.value.Equals(value))
{
this.value = value;
base.Text = this.value.ToString();
}
}
}
[Browsable(false)]
public override string Text
{
get { return base.Text; }
set { base.Text = value; }
}
protected override void OnTextChanged(EventArgs e)
{
try
{
Value = (T)Convert.ChangeType(base.Text, typeof(T));
}
catch (
Exception ex)
{
Undo();
throw new InvalidCastException("bla bla", ex);
}
}
public TypedTextBox()
{
base.Text = this.value.ToString();
}
}