Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to make sure your TextBox entries are valid

0.00/5 (No votes)
22 Sep 2004 2  
How to add all Cut/Paste/Undo cases easily to your TextBox validation.

Sample image

Introduction

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()
        MyBase.New()
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)
        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.

License

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