Click here to Skip to main content
12,829,115 members (38,831 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 22 Sep 2004

How to make sure your TextBox entries are valid

, 22 Sep 2004
Rate this:
Please Sign up or sign in to vote.
How to add all Cut/Paste/Undo cases easily to your TextBox validation.

Sample image


One major caveat I have seen a lot and even here on CodeProject, when people are talking about validating entries in a TextBox is that, they often forget all sorts of combinations of Cut/Copy/Paste. Ctrl+V is the most often checked for and sometimes they apply custom copy/paste context menus.

One thing I love about the default context menus is that they are language independent, so if you don’t mess with them, they are valid all around the world. The other things I have seen often missed are Ctrl-Insert and Shift-Insert (ctrl – copy, shift - paste) consideration. If you didn’t know try them out. Those are combinations that often work in high profile apps as well even if the coders wanted to shut off copy and paste (even Microsoft in some cases).

You just could use the TextChanged event but that’s real annoying, especially if you bind a data provider of any sort to the text box. So much unnecessary validation…

So what to do, you want to check the entry, you don’t want to lose the nice default context menu and you want to check for any cut, paste, undo (don’t forget undo) and you don’t want to do unnecessary validation. It is so easy you won’t believe it :).

Public Class TextBoxExtended
    Inherits System.Windows.Forms.TextBox
    Private Const WM_PASTE As Integer = &H302
    Private Const WM_CUT As Integer = &H300
    Private Const WM_UNDO As Integer = &H304
    'Public Event DidPaste()
    'Public Event DidCut()
    'Public Event DidUndo()
    Public Event CheckMe()

    Public Sub New()
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_PASTE
                'RaiseEvent DidPaste()
                RaiseEvent CheckMe()
            Case WM_CUT
                'RaiseEvent DidCut()
                RaiseEvent CheckMe()
            Case WM_UNDO
                'RaiseEvent DidUndo()
                RaiseEvent CheckMe()
        End Select

    End Sub
End Class

Now just do your validation in the CheckMe event and the default KeyUp event of the TextBoxExtended.

A small Update

To illustrate why this approach can be interesting (thanks mav.northwind) I updated this article with a small demo. In the demo, a valid entry is something like “xyz = 123” and if it is not of that type, it tells the user what the problem is (hover over the exclamation mark).

It uses a Regex to do the validation and an ErrorProvider to inform the user, so the concept should be easily adaptable to more “Real World” needs.

If you download the source, you have to run the application once before the control shows up in the designer. To simplify it, I changed the generated code.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHow to Restrict to paste text into a textbox Pin
Ravi Kumar Neelam17-May-06 23:52
memberRavi Kumar Neelam17-May-06 23:52 web application Pin
illamamatha23-Feb-06 2:19
memberillamamatha23-Feb-06 2:19 
QuestionWhy not use the Validating event? Pin
mav.northwind22-Sep-04 21:24
membermav.northwind22-Sep-04 21:24 
AnswerRe: Why not use the Validating event? Pin
Qualtar23-Sep-04 5:10
memberQualtar23-Sep-04 5:10 
GeneralRe: Why not use the Validating event? Pin
mav.northwind23-Sep-04 6:57
membermav.northwind23-Sep-04 6:57 
GeneralRe: Why not use the Validating event? Pin
Qualtar23-Sep-04 8:39
memberQualtar23-Sep-04 8:39 
GeneralRe: Why not use the Validating event? Pin
mp4city19-Dec-05 21:26
membermp4city19-Dec-05 21:26 
GeneralRe: Why not use the Validating event? Pin
IamLamb16-Oct-04 22:27
memberIamLamb16-Oct-04 22:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170326.1 | Last Updated 22 Sep 2004
Article Copyright 2004 by Qualtar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid