Click here to Skip to main content
11,639,922 members (66,746 online)
Click here to Skip to main content

Filtered Textbox

, 9 Oct 2005 36.7K 400 12
Rate this:
Please Sign up or sign in to vote.
Demonstrates how to impliment a textbox who's input is regulated via. a regular expression.
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->

Introduction

Have you ever needed to create a textbox that only allows you to enter numbers? Or letters? Or strings that don't start with a number? C# 2.0 addresses this point to a certain extent with the masked textbox. However, this does not help if you need to use more complex rules to define the validation.

To this end, I created a FilteredTextBox, that uses a regular expression filter to define what can be entered in the textbox. If it cannot be entered, it will not appear (unlike a post-validation, that would create an error message, or cleanup and remove invalid characters after)

Using the Code

To use the compiled componant, enter a regular expression that for any acceptable string in the textbox will create a match in the Filter property.

Adding Sound

I excluded this to enable the code to work with .NET 1.1 However, if you are using 2.0, or can do something similar in 1.1, it may be a good idea to provide audio feedback when an invalid character is typed. I did this by adding a boolean property of ShouldPlaySound and modiftying the code to match below:
if (!regexFilter.IsMatch(inputText))
{
    if (PlayErrorSound)
        SystemSounds.Beep.Play();
    e.Handled = true;
}

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

[ICR]
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

 
Generaloptimized for filtering every char Pin
jopomo20-Nov-06 4:35
memberjopomo20-Nov-06 4:35 
GeneralBug in filteredTextBox_KeyPress Pin
Perry211-Apr-06 13:10
memberPerry211-Apr-06 13:10 
GeneralWM_PASTE Pin
tonyt9-Oct-05 20:27
susstonyt9-Oct-05 20:27 
GeneralRe: WM_PASTE Pin
[ICR]9-Oct-05 21:20
member[ICR]9-Oct-05 21:20 
GeneralUmm... Pin
The_Mega_ZZTer9-Oct-05 7:45
memberThe_Mega_ZZTer9-Oct-05 7:45 
GeneralRe: Umm... Pin
[ICR]9-Oct-05 7:56
member[ICR]9-Oct-05 7:56 
Its a regular expression applied to the text overall as a whole, not just the character entered. I do, though, see your point. I'm pretty sure it can still be covered with a regex though, but my regex is quite terrible, so I'm not sure how it would be done.
It does solve many situations that a masked textbox doesn't really fit though. Especialy the given example, such as only allowing numbers, letters etc.
It isn't a Microsoft policy of post-validating. Almost all Microsoft, and big name apps I've seen use on the fly validation such as this. The "Upper" and "Lowercase" filters on TextBox's are another example of this. Even the masked textbox's are on the fly validation, just in alot stricter environment, which allows them to have prompts as to what characters you can enter.

-- modified at 14:03 Sunday 9th October, 2005
GeneralRe: Umm... Pin
The_Mega_ZZTer9-Oct-05 7:59
memberThe_Mega_ZZTer9-Oct-05 7:59 
GeneralRe: Umm... Pin
[ICR]9-Oct-05 8:06
member[ICR]9-Oct-05 8:06 
GeneralRe: Umm... Pin
[ICR]9-Oct-05 11:48
member[ICR]9-Oct-05 11:48 

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 | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 9 Oct 2005
Article Copyright 2005 by [ICR]
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid