I’m having a problem where I have created a custom control that inherits from TextBox where I need to override the Text property and the OnKeyPress or OnKeyUp event.
- The text property is being overriden to display a different value than the actual value of the text box (for unit conversions)
- The OnKeyPress/OnKeyUp I am currently trying to add to only allow for numerical values to be entered.
When the KeyPress event is triggered, the text that gets updated is base.Text instead of my override… any ideas on how I overcome this problem?
public override string Text
{
get
{
return base.Text;
}
set
{
displayValue = parse;
base.Text = this.ContainsFocus ? displayValue.ToString() : displayValue.ToString("N3");
}
}
protected override void OnKeyUp(KeyEventArgs e)
{
e.Handled = true;
if ((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) ||
(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9) ||
e.KeyCode == Keys.Decimal)
{
e.Handled = false;
}
//Code to only accept numerical buttons
base.OnKeyUp(e);
}
All it needed in the end was
I realised the reason why this was the case is that base.OnKeyPress(e) must handle the base.Text which means that there would be no way for it to know about my override