Click here to Skip to main content
15,886,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to perform text validation in a text box as you type or edit in it, to refuse certain characters or character sequences. In case of a refusal, I want to leave the box in exactly the same state, i.e. with the caret and selection unchanged.

I know about the standard Validation mechanism, but it only works after typing. I also know about the MaskedTextBoxes, but they are lacking flexibility.

I have implemented a solution that works fine by using a KeyPress handler, where complex checks can be performed, and changes canceled by setting the Handled property of the event to false. It satisfies all my requirements.

I would like to go further and refuse a paste to the box when it would violate the validation logics. But I see no "Pasting" event that I can handle.

I could also live with any event that tells me what the new text would look like after a change, but letting me refuse the change and stay in the same state.

Do you know of a simple solution ?
Posted
Updated 9-Aug-14 12:15pm
v3
Comments
Sergey Alexandrovich Kryukov 9-Aug-14 20:54pm    
It all depends on the type of text box. Which one? Full name, please. The answers would be different for different types.
—SA

1 solution

Of course, this should not be validation (which you can also use), but filtering out some of the input.

Please see my comment to the question: as you did not specify what exactly do you mean by "text box", it's impossible to give one exact answer. I can explain you the idea. Typically, you have to handle the text box's event KeyPress Event arguments of these class has some property to indicate that you cancel the event ("Handled"). Based on the the data passed to your event handler (a character attempted to enter), accept or deny it. It will give you exactly the effect you need.

Don't forget to allow for backspace. By some (historical?) reasons it is considered as a "character", code point 8.

[EDIT]

This case shows how bad was your failure to specify the full type name of the text box class you are using. I have to answer twice.

So, you handle this event: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress%28v=vs.110%29.aspx[^].

And, to deny some of input, set to true this property of event arguments: http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.handled%28v=vs.110%29.aspx[^].

You could easily find it all by yourself, instead of complaining, especially after reading my part of the answer above. How your "not WPF" was relevant? I knew it could be not WPF, so what.

As to capturing the Paste operation, yes, this is a different story: the problem is a bit more difficult. First, you can always catch a raw Windows message WM_PAST. This is how:
http://www.experts-exchange.com/Programming/Languages/.NET/Q_24099882.html[^].

Another solution would be capture the key combinations Ctrl+C and Shift+Ins and replace a context menu where the past operation might me:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.contextmenu(v=vs.110).aspx[^].

—SA
 
Share this answer
 
v3
Comments
YvesDaoust 10-Aug-14 10:13am    
Its a Windows Forms TextBox, not WPF. My question is about handling the paste action, I am comfortable with the keyboard input.
Sergey Alexandrovich Kryukov 10-Aug-14 13:30pm    
Again, you should have told about that in first place; please see my comment to the question. That's why I just gave you the idea.
—SA
YvesDaoust 11-Aug-14 2:37am    
What idea ? Using KeyPress was already described in the question.
Sergey Alexandrovich Kryukov 11-Aug-14 3:21am    
Yes, I can see it now. Please see the second part of my answer about dealing with paste.
—SA
YvesDaoust 11-Aug-14 4:27am    
Thanks for the hints. Capturing the WM_PASTE message raises an annoying complication: as you need to override the WndProc for the control, you need to create a custom control just for that purpose. (Simple derivation of the TextBox class can't be used as it causes the Designer to stop recognizing the code, making the Form impossible to graphically edit :( )

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900