Click here to Skip to main content
Click here to Skip to main content

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

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 Smile | :) .

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

Share

About the Author

Qualtar

Germany Germany
No Biography provided

Comments and Discussions

 
QuestionHow to Restrict to paste text into a textbox PinmemberRavi Kumar Neelam17-May-06 22:52 
Generalvb.net web application Pinmemberillamamatha23-Feb-06 1:19 
QuestionWhy not use the Validating event? Pinmembermav.northwind22-Sep-04 20:24 
AnswerRe: Why not use the Validating event? PinmemberQualtar23-Sep-04 4:10 
GeneralRe: Why not use the Validating event? Pinmembermav.northwind23-Sep-04 5:57 
GeneralRe: Why not use the Validating event? PinmemberQualtar23-Sep-04 7:39 
GeneralRe: Why not use the Validating event? Pinmembermp4city19-Dec-05 20:26 
GeneralRe: Why not use the Validating event? PinmemberIamLamb16-Oct-04 21:27 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 22 Sep 2004
Article Copyright 2004 by Qualtar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid