Click here to Skip to main content
15,881,588 members
Articles / Programming Languages / Visual Basic
Article

How to make sure your TextBox entries are valid

Rate me:
Please Sign up or sign in to vote.
3.40/5 (7 votes)
22 Sep 20042 min read 60.9K   524   26   8
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 :).

VB
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


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to Restrict to paste text into a textbox Pin
Ravi Kumar Neelam17-May-06 22:52
Ravi Kumar Neelam17-May-06 22:52 
Generalvb.net web application Pin
illamamatha23-Feb-06 1:19
illamamatha23-Feb-06 1:19 
QuestionWhy not use the Validating event? Pin
mav.northwind22-Sep-04 20:24
mav.northwind22-Sep-04 20:24 
AnswerRe: Why not use the Validating event? Pin
Qualtar23-Sep-04 4:10
Qualtar23-Sep-04 4:10 
GeneralRe: Why not use the Validating event? Pin
mav.northwind23-Sep-04 5:57
mav.northwind23-Sep-04 5:57 
ReHi Smile | :)

I understand what the code does even without a demo, but I intended to raise a design question.

In my opinion it is more user-friendly and far easier to maintain not to restrict the way a user enters data while he's entering it. Should a program really care HOW a text is entered in a textbox, for example, rather than WHAT the final text is?

A good example for bad design with restricted input possibilities bugged me just today (once again):
My bank has a Java applet for online remittance and the amount field only accepts digits and the decimal point. So far so good, but after entering the decimal point and a single digit, the cursor jumps back to _before_ the decimal point. So when you just type "12.34" you'll get "124.30". If you have to enter single cents you'll have to explicitely move the cursor to the end of the field and delete the '0' and type the single cents. Bogus! Wouldn't have happened if the amount would have been checked only when I leave the field.

What do others think about this topic?

mav
GeneralRe: Why not use the Validating event? Pin
Qualtar23-Sep-04 7:39
Qualtar23-Sep-04 7:39 
GeneralRe: Why not use the Validating event? Pin
mp4city19-Dec-05 20:26
mp4city19-Dec-05 20:26 
GeneralRe: Why not use the Validating event? Pin
IamLamb16-Oct-04 21:27
IamLamb16-Oct-04 21: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.