Capturing key presses in a TextBox subclass

  • Thread starter Thread starter Andrew Ducker
  • Start date Start date
A

Andrew Ducker

I have a subclass of TextBox, and I want to check that whenever the
text is changed the information in there is still a valid number.

I thought that overriding KeyPress with the following would work:

string oldText = this.Text;
base.OnKeyPress (e);
try
{
decimal.Parse(this.Text);
}
catch
{
this.Text = oldText;
}

but OnKeyPress doesn't seem to be where the change occurs. Any
suggestions as to which method actually updates the .Text property?

Thanks,

Andy D
 
Overriding OnTextChanged doesn't cover that 'moment', and the
TextChanged event doesn't seem to have any way in it of rolling back
the last change if it's invalid.

I'll do this the hard way if I have to, but if there's a built in
method that'd make this easy I'd appreciate a hint.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top