views:

261

answers:

2

I am working on windows form based application.

I want to validate textbox values. User enter only Numeric values in the textbox , now i am able to validate keypressevent, but i want validate copied value should be numeric then only paste(Mouse right click paste or Ctrl+v) textbox.

+1  A: 

The answer will depend on the level of feedback you want. If you want to give user feedback, I'd recommend using the Validating event and an ErrorProvider.

Here's an example: http://www.java2s.com/Tutorial/CSharp/0460__GUI-Windows-Forms/ErrorProvidernumbermustbeinarange.htm

Otherwise, just bind to the KeyDown or TextChanged events, and strip out any input you didn't want to be there. Depending on your exact validation requirements, you might also find a MaskedTextBox useful: http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.aspx

Ubiquitous Che
A: 

When the user puts focus on a textbox (assuming a value has been copied from somewhere), you could check the last value copied to the Clipboard and either disable/remove focus from the control depending on your validation criteria.

Example of using the Clipboard in C#: http://www.codeproject.com/KB/shell/clipboard01.aspx

Mr. Smith